• csu 1183 表达式求值


    表达式求值,注意:输入可能有\t!!!!没有考虑到,re到死

    代码:

      1 #include <iostream>
    2 #include <stack>
    3 #include <string.h>
    4 #include <cstdio>
    5 using namespace std;
    6 const int maxx=1000;
    7 int precede(char c)
    8 {
    9 switch(c)
    10 {
    11 case '+':
    12 case '-':return 1;
    13 case '*':
    14 case '/':return 2;
    15 default:return 0;
    16 }
    17 }
    18
    19 double getvalue(char c,double a,double b)
    20 {
    21 switch(c)
    22 {
    23 case '+':return b+a;
    24 case '-':return b-a;
    25 case '*':return b*a;
    26 case '/':return b/a;
    27 }
    28 }
    29
    30 bool isnum(char c)
    31 {
    32 if(c>='0' && c<='9') return 1;
    33 else return 0;
    34 }
    35
    36 double strtonum(char s[],int &i)
    37 {
    38 double a=0.0;
    39 a=(double)(s[i]-'0')*1.0;
    40 i++;
    41 while(isnum(s[i]))
    42 {
    43 a=a*10.0+(double)(s[i]-'0')*1.0;
    44 i++;
    45 }
    46 return a;
    47 }
    48
    49 void change(char c[],char s[])
    50 {
    51 int i,cnt=0,len=strlen(c);
    52 s[0]='#';
    53 for(i=0;i<len;)
    54 {
    55 if((c[i]=='+' || c[i]=='-') && s[cnt]=='#')
    56 {
    57 s[++cnt]='0';
    58 s[++cnt]=c[i];
    59 i++;
    60 }
    61 else if(c[i]=='(')
    62 {
    63 s[++cnt]=c[i];
    64 i++;
    65 while(c[i]==' ') i++;
    66 if(c[i]=='+' || c[i]=='-')
    67 {
    68 s[++cnt]='0';
    69 s[++cnt]=c[i];
    70 i++;
    71 }
    72 }
    73 else if(c[i]==' '||c[i]=='\t') i++;//坑爹啊!!!
    74 else {s[++cnt]=c[i];i++;}
    75 }
    76 s[++cnt]='\0';
    77 }
    78
    79 double result(char s[])
    80 {
    81 stack <char> ope;
    82 stack <double> nd;
    83 char c;
    84 double a,b,ans;
    85 int i,j=0,k,len=strlen(s);
    86 for(i=1;i<len;)
    87 {
    88 if(isnum(s[i]))
    89 {
    90 a=strtonum(s,i);
    91 nd.push(a);
    92
    93 }
    94 else if(s[i]=='('){ ope.push('(');i++;}
    95 else if(s[i]==')')
    96 {
    97 while(ope.top()!='(')
    98 {
    99 c=ope.top();ope.pop();
    100 a=nd.top();nd.pop();
    101 b=nd.top();nd.pop();
    102 ans=getvalue(c,a,b);
    103 nd.push(ans);
    104 }
    105
    106 ope.pop();
    107 i++;
    108 }
    109 else if(s[i]=='+' || s[i]=='-' || s[i]=='*' || s[i]=='/')
    110 {
    111 if(!ope.empty() && precede(s[i])<=precede(ope.top()))//若当前符号优先级比栈里边的小,则一直弹栈
    112 {
    113 c=ope.top();ope.pop();
    114 a=nd.top();nd.pop();
    115 b=nd.top();nd.pop();
    116 ans=getvalue(c,a,b);
    117 nd.push(ans);
    118 }
    119 else
    120 {
    121 ope.push(s[i]);
    122 i++;
    123 }
    124 }
    125 else i++;
    126 }
    127 while(!ope.empty())
    128 {
    129 c=ope.top();ope.pop();
    130 a=nd.top();nd.pop();
    131 b=nd.top();nd.pop();
    132 ans=getvalue(c,a,b);
    133 nd.push(ans);
    134 }
    135
    136 return nd.top();
    137 }
    138
    139
    140 int main()
    141 {
    142 char s[maxx+100];
    143 char c[maxx+100];
    144 double ans;
    145 // freopen("in.txt","r",stdin);
    146 while(gets(c))
    147 {
    148 change(c,s);
    149 ans=result(s);
    150 printf("%.4lf\n",ans);
    151
    152 }
    153 return 0;
    154 }



  • 相关阅读:
    babel缓存 非常实用(8)
    source-map 非常实用(7)
    webpack -HMR-非常实用(6)
    eslint 语法检查(5)
    对css 的处理(4)
    python之再学习----简单的字符串
    windows下安装django的具体步骤和各种问题
    Linux常用命令
    转:程序中得到SVN的版本号
    vue实战(1):准备与资料整理
  • 原文地址:https://www.cnblogs.com/inpeace7/p/2431262.html
Copyright © 2020-2023  润新知