/*描述:对于一个不存在括号的表达式进行计算
输入描述:存在多组数据,每组数据一行,表达式不存在空格
输入:6/2+3+3*4
输出:18*/
#include<stdio.h>
#include<stack>
#include<string>
#include<map>
using namespace std;
int main(){
char str[1000]={0};
map<char,int>priority={
{'\0',0},
{'+',1},{'-',1},
{'*',2},{'/',2}
};
while(scanf("%s",str)!=EOF){
string numStr="";
stack<char>opStack;
stack<double>numStack;
for(int i=0;;i++){
if(str[i]>='0'&&str[i]<='9'){
numStr.push_back(str[i]);
}else{
double num=stod(numStr);//stod 是 C++ 标准库中的一个字符串转浮点数函数
numStr="";
numStack.push(num);
//什么时候弹栈? 栈非空&&新op的优先级 栈顶的优先级
//循环弹栈和计算
while(!opStack.empty()
&&priority[str[i]]<=priority[opStack.top()]) {
double rhs=numStack.top();
numStack.pop();
double lhs=numStack.top();
numStack.pop();
char curOp=opStack.top();
opStack.pop();
if(curOp=='+'){
numStack.push(lhs+rhs);
}else if(curOp=='-'){
numStack.push(lhs-rhs);
}else if(curOp=='*'){
numStack.push(lhs*rhs);
}else if(curOp=='/'){
numStack.push(lhs/rhs);
}
}
//栈为空 或者新op的优先级高于栈顶
if(str[i]=='\0'){
printf("%d\n",(int)numStack.top());
break;
}else{
opStack.push(str[i]);
}
}
}
}
return 0;
}