栈与队列三:后缀式求值
题目描述:
Problem Description
对于一个基于二元运算符的后缀表示式(基本操作数都是一位正整数),求其代表的算术表达式的值。
Input
输入一个算术表达式的后缀式字符串,以‘#’作为结束标志。
Output
求该后缀式所对应的算术表达式的值,并输出之。
Sample Input
59*684/-3*+#
Sample Output
57
Hint
基本操作数都是一位正整数!
Source
思路
1.后缀表达式计算规则
模拟
5
5 9
45
45 6
45 6 8
45 6 8 4
45 6 2
45 4
45 4 3
45 12
57
2.表达式int、char类型转换
运算数是int型,运算符和终止符是char型,如何计算呢?用char型数组读入表达式,若为运算数,则转换成int型入栈,使得操作后输出的也是int型
代码
#include <iostream>
using namespace std;
typedef struct{
int Data[1001];
int top;
} Mystack;
void pop(Mystack *head, int m){
head->Data[++head