表达式求值

来源:互联网 发布:高考知识点软件 编辑:程序博客网 时间:2024/05/17 03:02
#include<algorithm>#include<iostream>#include<cstdio>#include<cstdlib>#include<stack>#include<sstream>#include<algorithm>#include<iostream>using namespace std;/*int map(string s){       int sum = 0;        int t=s.length();        for(int i=t-1; i>=0; i--)        sum+=(s[i]-'0')*(int)pow(10*1.0,t-1-i);        return sum;        }//转化为字符串string convertToString(double x){   ostringstream o;   if(o<<x)   return o.str();   return error; //刚开始没有注意,以为可加可不加}*/  string min(string x, string y)  {     if(x.compare(y)<=0)     return x;     return y;    }  string max(string x, string y)  {     if(x.compare(y)>=0)     return x;     return y;    }  string add(string x, string y)  {     int a=map(x);     int b=map(y);     a=a+b;     return convertToString(a);  }  /*string add(string x, string y)  {     reverse(x.begin(), x.end());    reverse(y.begin(), y.end());     string s;    for(int i=0; i<x.length()&&i<y.length(); i++)    {      s[i]=x[i]+y[i];if(s[i]>=10)      {      s[i+1]=(s[i]-'0')/10+'0';      s[i]=(s[i]-'0')%10+'0';      }      }      if(i<x.length())      s[i++]=x[i++];      if(i<y.length())      s[i++]=y[i++];       reverse(s.begin(), s.end());       return s;      }*/void camp(char *s){   string str;   stack<char> S;   str=s;   int t = str.length();   for(int i=0; i<t; i++)   if(str[i]==')')   {     char ch;     string a, b, c, d;     ch=S.top();     while(ch !=',')     {       a = a+ch;       S.pop();       ch=S.top();       }       S.pop();       ch=S.top();       while(ch!='(')       {          b = b+ch;           S.pop();           ch=S.top();           }           S.pop();           while(S.empty()!=true&&S.top()!=','&&S.top()!='(')           {             ch=S.top();              c = c+ch;              S.pop(); // ch=S.top();              }           reverse(a.begin(),a.end());           reverse(b.begin(),b.end());           reverse(c.begin(),c.end());           if(c.compare("add")==0)           d = add(a, b);           else if(c.compare("max")==0)           d = max(a, b);           else d = min(a, b);           for(int i=0; i<d.length(); i++)           S.push(d[i]);           }           else S.push(str[i]);            string d="";            while(S.empty()!=true)            {              d = d+S.top();              S.pop();              }            reverse(d.begin(), d.end());            cout<<d<<endl;           }    int main()    {       char ss[3000];       int N;       scanf("%d",&N);       getchar();       while(N--)       {         scanf("%s",ss);         camp(ss);         }         system("pause");        return 0;        }