#include <bits/stdc++.h>
#include <stack>
#include <math.h>
using namespace std;
int Postfix(string s)
{
stack<int> str;
for (int i = 0; i < s.length(); i++)
{
if (s[i] >= '0' && s[i] <= '9')
{
str.push(s[i] - '0');
}
else
{
int op2 = str.top();
str.pop();
int op1 = str.top();
str.pop();
switch (s[i])
{
case '+':
str.push(op1 + op2);
break;
case '-':
str.push(op1 - op2);
break;
case '*':
str.push(op1 * op2);
break;
case '/':
str.push(op1 / op2);
break;
case '^':
str.push(pow(op1, op2));
break;
}
}
}
return str.top();
}
int main()
{
cout << Postfix("46+2/5*7+");
return 0;
}
0 Comments