题意:给出两个含未知数的表达式,判断其是否相等。

题解:任意给未知数赋值,看起结果是否相同。

View Code
  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #include<cctype>
  5 using namespace std;
  6 const int N=1000;
  7 char op[8]= {'+','-','*','/','(',')','\0'};
  8 char cp[7][7]=
  9 {
 10     {'>','>','<','<','<','>','>'},
 11     {'>','>','<','<','<','>','>'},
 12     {'>','>','>','>','<','>','>'},
 13     {'>','>','>','>','<','>','>'},
 14     {'<','<','<','<','<','=','@'},
 15     {'>','>','>','>','@','>','>'},
 16     {'<','<','<','<','<','@','='}
 17 };
 18 char st_char[N];
 19 int st_int[N],top_char,top_int;
 20 int cal(int a,char op,int b)
 21 {
 22     switch(op)
 23     {
 24     case '+':
 25         return a+b;
 26         break;
 27     case '-':
 28         return a-b;
 29         break;
 30     case '*':
 31         return a*b;
 32         break;
 33     case '/':
 34         return a/b;
 35         break;
 36     default:
 37         while(1)puts("You Will Output Limit Exceeded\n");
 38     }
 39 }
 40 char comp(char a,char b)
 41 {
 42     int i,j;
 43     for(i=0; i<7; i++)
 44         if(a==op[i])
 45             break;
 46     for(j=0; j<7; j++)
 47         if(b==op[j])
 48             break;
 49     return cp[i][j];
 50 }
 51 char comp10(char x,char ch)
 52 {
 53      if(x=='+'||x=='-')
 54         if(ch=='+'||ch=='-'||ch==')'||ch=='\0') return ('>');
 55         else return ('<');
 56      if(x=='*'||x=='/')
 57         if(ch=='(') return('<');
 58         else return('>');
 59      if(x=='(')
 60         if(ch==')') return('=');
 61         else return('<');
 62      if(x==')')
 63         if(ch!='(') return('>');
 64      if(x=='\0')
 65         if(ch=='\0') return('=');
 66         else return('<');
 67 }
 68 int cac(char s[])
 69 {
 70     top_char=top_int=0;
 71     st_char[top_char++]='\0';
 72     int i=0,v,a,b;
 73     char ch;
 74     bool flag=true;
 75     while(s[i]!='\0'||st_char[top_char-1]!='\0')
 76     {
 77         if(s[i]==' '||s[i]=='\t')
 78         {
 79             i++;
 80             continue;
 81         }
 82         else if(isdigit(s[i]))
 83         {
 84             v=0;
 85             while(isdigit(s[i]))
 86             {
 87                 v=v*10+s[i]-'0';
 88                 i++;
 89             }
 90             st_int[top_int++]=v;
 91         }
 92         else if(isalpha(s[i]))
 93         {
 94             st_int[top_int++]=(int)s[i++];
 95         }
 96         else
 97         {
 98             switch(comp(st_char[top_char-1],s[i]))
 99             {
100             case '<':
101                 st_char[top_char++]=s[i++];
102                 break;
103             case '=':
104                 top_char--;
105                 i++;
106                 break;
107             case '>':
108                 ch=st_char[--top_char];
109                 b=st_int[--top_int];
110                 a=st_int[--top_int];
111                 v=cal(a,ch,b);
112                 st_int[top_int++]=v;
113                 break;
114             }
115         }
116     }
117     return st_int[0];
118 }
119 int main()
120 {
121     int T;
122     for(scanf("%d ",&T); T; T--)
123     {
124         char s1[1000],s2[1000];
125         gets(s1);
126         gets(s2);
127         if(cac(s1)==cac(s2))
128             printf("YES\n");
129         else
130             printf("NO\n");
131     }
132     return 0;
133 }