輸入
後綴表達式。以#號作爲表達式結束標誌。爲了簡單,處理的數據爲0-9的整數。
輸出
計算結果。
樣例輸入
3 6 6 2 / - 3 * +#
樣例輸出
12
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstdlib>
#include<stack>
using namespace std;
typedef int ElemType;
int main()
{
stack<int>s;
char a[1001];
int n;
for (n = 0;; n++)
{
scanf("%c", &a[n]);
if (a[n] == '#')break;
}
for (int i = 0; i < n; i++)
{
if (a[i] >= '0'&&a[i] <= '9')
s.push(a[i] - '0');
else
{
if (a[i] == ' ')continue;
if (a[i] == '-')
{
int b = s.top();
s.pop();
int a = s.top();
s.pop();
int sum = a - b;
s.push(sum);
}
if (a[i] == '+')
{
int b = s.top();
s.pop();
int a = s.top();
s.pop();
int sum = a + b;
s.push(sum);
}
if (a[i] == '/')
{
int b = s.top();
s.pop();
int a = s.top();
s.pop();
int sum = a / b;
s.push(sum);
}
if (a[i] == '*')
{
int b = s.top();
s.pop();
int a = s.top();
s.pop();
int sum = a * b;
s.push(sum);
}
}
}
cout << s.top();
s.pop();
return 0;
}