表达式系列问题

1)仅有加减法(高精度)

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 //#include<cctype> //不知道为什么没有cctype也可以用isdigit()函数 
 6 #deifne N 1511 
 7 using namespace std;
 8 int point=1,a[N],zhongzhuan[N],b[N];
 9 string s;
10 
11 void get_array(){//低位向高位转换 
12     memset(b,0,sizeof(b));
13     b[0]=zhongzhuan[0];//a[0]储存当前位数 
14     for(int i=1;i<=b[0];i++) b[i]=zhongzhuan[b[0]-i+1];
15     memset(zhongzhuan,0,sizeof(zhongzhuan));
16 }
17 
18 int cmp(){//大于返回1    小于返回-1    相等返回0 
19     if(a[0]>b[0]) return 1;//先比较位数 
20     if(a[0]<b[0]) return -1;
21     
22     for(int i=a[0];i>=1;i--){
23         if(a[i]>b[i]) return 1;
24         if(a[i]<b[i]) return -1;
25     }
26     
27     return 0;
28 }
29 
30 void plus_(){//加法 
31     int i,k;
32     k=a[0]>b[0]?a[0]:b[0];
33     for(i=1;i<=k;i++){
34         a[i+1]+=(a[i]+b[i])/10;
35         a[i]=(a[i]+b[i])%10;
36     }
37     if(a[k+1]>0) a[0]=k+1;
38     else a[0]=k;
39     return; 
40 }
41 
42 void gminus_(){//减法 
43     int flag=cmp();
44     if(flag==0) {memset(a,0,sizeof(a));return;}
45     if(flag==1){
46         for(int i=1;i<=a[0];i++){
47             if(a[i]<b[i]){--a[i+1];a[i]+=10;}
48             a[i]=a[i]-b[i];
49         }
50         while(a[a[0]]==0) a[0]--;
51         return;
52     }
53     if (flag==-1){//小于  则用a=b-a,返回-1
54         for(i=1;i<=b[0];i++){
55             if(b[i]<a[i]){ b[i+1]--;b[i]+=10;} //若不够减则向上借一位
56             a[i]=b[i]-a[i];
57         }
58         a[0]=b[0];
59         while(a[a[0]]==0) a[0]--; //修正a的位数
60         return;
61     }
62 }
63 
64 int main(){
65 //    freopen("01.txt","r",stdin);
66     getline(cin,s);
67     int fh=1;//操作符(FuHao) 
68     for(int i=0;i<s.size();i++){
69         if(isdigit(s[i])){//判断是否为数字 
70             zhongzhuan[point]=int(s[i]-'0');//point暂存中转位数 
71             zhongzhuan[0]=point++;//中转[0]暂存中转位数 
72         }
73         else{
74             get_array();
75             point=1;
76             
77             if(fh==1) plus_();    //1为+ 
78             else if(fh==-1) gminus_();//-1为- 
79             
80             fh=(s[i]=='+')?1:-1; 
81         }
82     }
83     
84     if(zhongzhuan[0]!=0){//伪清空栈(因为栈只有一个位置) 
85         get_array();
86         point=1;
87         if(fh==1) plus_();    //1为+ 
88         else if(fh==-1) gminus_();//-1为- 
89     }
90     
91     for(int i=a[0];i>=1;i--) printf("%d",a[i]);
92     puts(" ");
93     
94     return 0;
95 }

详见代码注释

2)加减乘除(注:此代码暂时无法处理负数情况)

//等价表达式问题:http://www.cnblogs.com/radiumlrb/p/5759319.html

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<algorithm>
 5 using namespace std;
 6 long long check,final;
 7 long long num[55],op[55];
 8 string s;
 9 
10 long long power(long long a,long long b)
11 {
12     long long k=1;
13     for(int i=0;i<b;i++)
14         k*=a;
15     return k;
16 }
17 
18 void cul(int p,int op)
19 {
20     if(op==0) num[p-1]=num[p-1]+num[p];
21     if(op==1) num[p-1]=num[p-1]-num[p];
22     if(op==2) num[p-1]=num[p-1]*num[p];
23 //  if(op==3) num[p-1]=num[p-1]/num[p];
24     if(op==5) num[p-1]=power( num[p-1],num[p] );
25 }
26 
27 long long result(string str){
28     int op_nxt;//下一个操作符
29     int len=str.length(),p=-1,q=-1;//p数字栈指针 ,q符号栈指针 
30     long long num_nxt=0;
31     for(int i=0;i<len;i++){
32         if(str[i]=='a') num[++p]=check; 
33         else if(str[i]>='0'&&str[i]<='9') num_nxt=num_nxt*10+(str[i]-'0');
34         else if(str[i]!=' '){
35             if(num_nxt!=0){
36                 num[++p]=num_nxt;
37                 num_nxt=0;
38             }
39             if(str[i]=='+') op_nxt=0;
40             if(str[i]=='-') op_nxt=1;
41             if(str[i]=='*') op_nxt=2;
42 //          if(str[i]=='/') op_nxt=3;
43             if(str[i]=='^') op_nxt=5;
44             if(str[i]=='(') op_nxt=6;
45             if(str[i]==')') op_nxt=7;
46             if(op_nxt==6) op[++q]=op_nxt;
47             else if(op_nxt==7) while(q>=0&&op[q--]!=6) cul(p--,op[q+1]);
48             else {
49                 while(q>=0&&op[q]<=5&&op[q]/2>=op_nxt/2) cul(p--,op[q--]);
50                 op[++q]=op_nxt;
51             }
52         }
53     }
54     //清空堆栈
55     if(num_nxt!=0){ 
56         num[++p]=num_nxt;
57         num_nxt=0;
58     }
59     while(q>=0) cul(p--,op[q--]);
60     return num[0];
61 }
62 
63 int main(){
64     freopen("01.txt","r",stdin);
65     string str1,str2;
66     int n;
67     final=0;
68     getline(cin,str1);
69     cin>>n;
70     getline(cin,str2);
71     while(n--)
72     {
73         bool flag=true;
74         getline(cin,str2);
75         for (int i=10;i<=20;i++)
76         {
77             check=i;
78             if (result(str1)!=result(str2))
79             {
80                 flag=false;
81                 break;
82             }
83         }
84         if (flag) cout<<(char)('A'+final);
85         final++;
86     }
87     cout<<endl;
88     return 0;
89 }

讲一下Line 49,除以2是为了规定优先级

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!
原文地址:https://www.cnblogs.com/radiumlrb/p/5778357.html