欢迎光临散文网 会员登陆 & 注册

shixun

2023-06-25 11:30 作者:-永雏张飞-  | 我要投稿

#include<iostream>

#include<stack>

#include<string>

#include<stdlib.h>

using namespace std;

int main(){

 string s;

 getline(cin,s);

 string temp="";

 int n,i,n1,n2,r;

 stack<int> t;

 n=s.length();

 for(i=n-1;i>=0;i--){

  if(isdigit(s[i])||s[i]=='-')

  temp+=s[i];

  if((isdigit(s[i])||s[i]=='-')&&(s[i-1]=='('||s[i-1]==')'||s[i-1]==',')){

  int num;

  num=atoi(temp.c_str());

  t.push(num);

  temp="";

}

if(isalpha(s[i]))

temp+=s[i];

if(isalpha(s[i])&&!isalpha(s[i-1])){

if(temp=="add"){

n1=t.top();t.pop();

n2=t.top();t.pop();

r=n1+n2;

t.push(r);

}else if(temp=="sub"){

n1=t.top();t.pop();

n2=t.top();t.pop();

r=n1-n2;

t.push(r);

}else if(temp=="div"){

n1=t.top();t.pop();

n2=t.top();t.pop();

r=n1/n2;

t.push(r);

}else if(temp=="mul"){

n1=t.top();t.pop();

n2=t.top();t.pop();

r=n1*n2;

t.push(r);

}

}

 }

 cout<<t.top()<<endl;

 return 0;

}


shixun的评论 (共 条)

分享到微博请遵守国家法律