表达式求值

#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;
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值