Professional Documents
Culture Documents
Câu 3 Chương 4
Câu 3 Chương 4
Câu 3 Chương 4
#include <iostream>
#include <stack>
#include <string>
int main(){
string s;
cin >> s;
return 0;
}
//Baif 4;
#include <iostream>
#include <stack>
#include <sstream>
#include <fstream>
#include <string>
struct Stack
{
int top;
char list[100];
};
void init(Stack &s)
{
s.top = 0;
}
bool Full(Stack s)
{
if (s.top == 1)
return 1;
return 0;
}
bool empty(Stack s)
{
if (s.top == -1)
return 1;
return 0;
}
void push(Stack& s, int x)
{
if (!Full(s))
s.list[++s.top] = x;
}
char top(Stack s)
{
return s.list[s.top];
}
void pop(Stack & s)
{
if (!empty(s))
--s.top;
}
int chuyenChuoiSo(string n)
{
stringstream ss;
ss << n;
int va;
ss >> va;
return va;
}
int str(string s)
{
stringstream ss;
ss << s;
int v;
ss >> v;
return v;
}
bool kt(char c)
{
if (c == '+' || c == '-' || c == '*') return true;
return false;
}
int tinhHauTo(string s)
{
stack<int> st;
for (int i = 0; i < s.size(); i++)
{
if (isdigit(s[i]))
{
string tmp = "";
while (s[i] != ' ' && !kt(s[i]))
{
tmp += s[i++];
}
st.push(chuyenChuoiSo(tmp));
}
else
if (kt(s[i]))
{
switch (s[i])
{
case '+':
{
int a = st.top();
st.pop();
int b = st.top();
st.pop();
st.push(b + a);
break;
}
case '-':
{
int a = st.top();
st.pop();
int b = st.top();
st.pop();
st.push(b - a);
break;
}
case '*':
{
int a = st.top();
st.pop();
int b = st.top();
st.pop();
st.push(b * a);
break;
}
}
}
}
return st.top();
}
int main()
{
string t;
getline(cin, t);
cout << tinhHauTo(t);
return 0;
}