POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑

Problem Description
A math instructor is too lazy to grade a question in the exam papers in which students are supposed to produce a complicated formula for the question asked. Students may write correct answers in different forms which makes grading very hard. So, the instructor needs help from computer programmers and you can help.

You are to write a program to read different formulas and determine whether or not they are arithmetically equivalent.
 

 

Input
The first line of the input contains an integer N (1 <= N <= 20) that is the number of test cases. Following the first line, there are two lines for each test case. A test case consists of two arithmetic expressions, each on a separate line with at most 80 characters. There is no blank line in the input. An expression contains one or more of the following:
  • Single letter variables (case insensitive).
  • Single digit numbers.
  • Matched left and right parentheses.
  • Binary operators +, - and * which are used for addition, subtraction and multiplication respectively.
  • Arbitrary number of blank or tab characters between above tokens.

Note: Expressions are syntactically correct and evaluated from left to right with equal precedence (priority) for all operators. The coefficients and exponents of the variables are guaranteed to fit in 16-bit integers.
 

 

Output
Your program must produce one line for each test case. If input expressions for each test data are arithmetically equivalent, "YES", otherwise "NO" must be printed as the output of the program. Output should be all in upper-case characters.
 

 

Sample Input
3 (a+b-c)*2 (a+a)+(b*2)-(3*c)+c a*2-(a+c)+((a+c+e)*2) 3*a+c+(2*e) (a-b)*(a-b) (a*a)-(2*a*b)-(b*b)
 

 

Sample Output
YES YES NO
这题的数据好水啊,没有带空隔的。下面有15组测式数据
#include<stdio.h>

#include<iostream>

#include<string.h>

#include<math.h>

using namespace std;

typedef struct nn

{

    int pri;//优先级,'(' > '*' > '+' = '-' > ')'

    char cc;//运算符

}node;

int main()

{

  int t,i,j,k,len,top1,top2;

  char ch[2][100];

   __int64  anss[1000],tem,ans[2],as[100],tt,n[2];//anss为数字栈,as中间数字栈,tem,ans[]为中间变量,n[]为最终的两个值进行比较

     node C[1000];//符号栈

    node c,c1;

    scanf("%d",&t);

    getchar();

    while(t--)

    {

        gets(ch[0]); gets(ch[1]);//一定要注意输入

        k=-1;

		for(j=0;j<2;j++)

		{

			top1=top2=0;//分别指向数字栈和符号栈

			len=strlen(ch[j]);

			for(i=0;i<=len;i++)//注意等号

			{

			    if(ch[j][i]==' ')//为空时,不往下运行

                    continue;

				if(ch[j][i]>='0'&&ch[j][i]<='9'||ch[j][i]>='a'&&ch[j][i]<='z')

				{

					if(ch[j][i]>='0'&&ch[j][i]<='9')

						as[++k]=ch[j][i]-'0';

					else

					as[++k]=ch[j][i]-'a'+97;//把a变成ASC码数

				}

				else

				{

					tt=1;tem=0;

					for(;k>=0;k--)//变成一个整数

					{

						tem=tem+as[k]*tt; tt*=10;

					}

					if(tt!=1) anss[++top1]=tem;//当有数时存入数字栈printf("%I64d %d \n",tem,i);}

					if(i==len) break;

					if(ch[j][i]=='(') {c.pri=3;c.cc='(';}

					if(ch[j][i]==')') {c.pri=0;c.cc=')';}

					if(ch[j][i]=='+') {c.pri=1;c.cc='+';}

					if(ch[j][i]=='-') {c.pri=1;c.cc='-';}

					if(ch[j][i]=='*') {c.pri=2;c.cc='*';}



					if(top2!=0&&c.cc!='(')

					{

						c1=C[top2];

						if(c1.cc=='('&&c.cc==')') //可以去掉空括号

						{top2--;continue;}



						if(c1.pri<c.pri||c1.cc=='(') //当前符号的优先级大于栈顶或栈顶为右括号时,当前符号存入符号栈中

                            {C[++top2]=c;continue;}

                        while(top2>0&&c1.pri>=c.pri) //符号栈不为空并且当前的运算符的优先级小于等于栈顶

						{

							top2--; //退一个符号栈顶

							ans[1]=anss[top1];top1--;

							ans[0]=anss[top1];top1--; //printf("%I64d %c %I64d",ans[0],c1.cc,ans[1]);

							if(c1.cc=='+') {ans[0]+=ans[1];anss[++top1]=ans[0];}

							if(c1.cc=='-') {ans[0]-=ans[1];anss[++top1]=ans[0];}

							if(c1.cc=='*') {ans[0]*=ans[1];anss[++top1]=ans[0];}

							c1=C[top2]; //取出,为下一次运算printf("= %I64d\n",ans[0]);

							if(c1.cc=='('||top2==0||c.pri>c1.pri) //在一直往前运算时,有三种情况要退出当前运算,

							{                     //1:一直运算到( ,2:栈为空,3:栈顶符优先级大于当前运算符优先级

								if(c.cc==')') top2--;

								else C[++top2]=c;  //后两种情况时,要把当前的符号放入到符号栈中

                                break;    //跳出循环

							}

						}

					}

					else//当符号栈为空或当前符号为( 时,直接放入符号栈中

					{

						C[++top2]=c; 

					}

				}

			}

			while(top2>0)//运算还没有算完的运算符

			{

				c1=C[top2]; top2--;

				ans[1]=anss[top1];top1--;

                    ans[0]=anss[top1];top1--;

                    if(c1.cc=='+') {ans[0]+=ans[1];anss[++top1]=ans[0];}

                    if(c1.cc=='-') {ans[0]-=ans[1];anss[++top1]=ans[0];}

                    if(c1.cc=='*') {ans[0]*=ans[1];anss[++top1]=ans[0];}

			}

         n[j]=anss[top1];//到这一步时,数字栈一定只有一个 数字(结果)

		}

		printf("%s\n",n[0]==n[1]?"YES":"NO");

    }

}

/*

15

2 *  1-3+(        2    *           5   )

9

8

      ((2    *   4)-7*2+(5-(6   * 7)+   4 )   )

(a)+(((b)))*(a-(2*b))*(1)*(b-2+c+a)*(d+e+a-f+b)

a    *    b    *      a * b * a * a * 9 * b * a * 8 * b * 7 * c * 9 * a

(((a)*a)*a  * b  * (     a-b)*(   a -   b   )*(a-c)*(a+b))*(((b-c)))

b*a*(a*(a*(b-c))*((a+b)*(a-c))*(a-b)*(a-b))

(w-a)+(a-b)-(c+a)*4+d

(a-w)+(b-a)-(a+c)*d+4

a-b*c+a+d*a-c*a+e*a*b*9*z*8*w*7

(a*a*a*a*b*(c-b+1)+(a*b*(a*(a*d-c)+e)))*9*z*8*w*7

(a+b-c)*2

(a+a)+(b*2)-(3*c)+c

a*2-(a+c)+( (a+c+e)*2)

3*a+c+(2*e)

(a-b)*(a-b)

(a*a)-(2*a*b)-(b*b)

(a-b)*(a-b)

(a*a)-(2*a*b)+(b*b)

a*b

b*a

1+a-a

b+1-b

2+1+a+1

1+3+a

(1+a+b)+7  +c+(d)

a+c+b+d +1+1+(1+(2+3))

((((1)+a)+b))+6  +c+(d)

a+c+b+d +1+1+(1+(2+3))

*/



 
  
 
 
  
 

你可能感兴趣的:(struct)