• Lazy Math Instructor


     
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 3721   Accepted: 1290

    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
    题目意思:输入两行公式,判断这两行公式相不相等,如果相等,输出YES,否则输出NO

    解题思路:先将方式变成后缀式,后缀式通过栈实现。(不晓得后缀式是什么,就百度后缀式吧,我也是百度的(⊙﹏⊙)b)
    变成后缀式之后,再通过栈计算他们的值,这里需要将字母转为ASCII码的值计算。最后判断.......
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <fstream>
      5 #include <stack>
      6 using namespace std;
      7 const int maxn = 90;
      8 int priority(char c)
      9 {
     10     if(c=='(')
     11         return 0;
     12     else if(c=='*')
     13         return 2;
     14     else
     15         return 1;
     16 }
     17 void convert(char *str,char *temp)
     18 {
     19     int len = strlen(str),t = 0;
     20     char c;
     21     stack<char> st;
     22     for(int i=0;i<len;i++)
     23     {
     24         if(str[i]!=' ')
     25         {
     26             c = str[i];
     27             if((c<='z'&&c>='a')||(c>='0'&&c<='9'))
     28                 temp[t++]=c;
     29             else
     30             {
     31                 if(st.empty()||c=='(')
     32                     st.push(c);
     33                 else if(c==')')
     34                 {
     35                     while(!st.empty()&&st.top()!='(')
     36                     {
     37                         //push_seq(pn[i],top_seq(p[i]));
     38                         temp[t++]=st.top();
     39                         st.pop();
     40                     }
     41                     st.pop();
     42                 }
     43                 else
     44                 {
     45                     while(!st.empty()&&priority(c)<=priority(st.top()))
     46                     {
     47                         temp[t++]=st.top();
     48                         st.pop();
     49                     }
     50                     st.push(c);
     51                 }
     52             }
     53         }
     54     }
     55     while(!st.empty())
     56     {
     57         temp[t++]=st.top();
     58         st.pop();
     59     }
     60     temp[t]=0;
     61 }
     62 int calculate(char *temp)
     63 {
     64     int len = strlen(temp),x,y,z;
     65     char c;
     66     stack<int> st;
     67     for(int i=0;i<len;i++)
     68     {
     69         c=temp[i];
     70         if(c>='0'&&c<='9')
     71             st.push(c-'0');
     72         else if(c<='z'&&c>='a')
     73             st.push(int(c));
     74         else
     75         {
     76             x=st.top();
     77             st.pop();
     78             y=st.top();
     79             st.pop();
     80             switch(c)
     81             {
     82                 case '*':  z = x*y; break;
     83                 case '+':  z = x+y; break;
     84                 case '-':  z = y-x; break;
     85             }
     86             st.push(z);
     87         }
     88     }
     89     return st.top();
     90 }
     91 int main()
     92 {
     93     freopen("in.txt","r",stdin);
     94     char str[maxn],temp[maxn];
     95     int n;
     96     scanf("%d",&n);
     97     getchar();//此处不能忘记getchar(),否则会出错
     98     while(n--)
     99     {
    100         gets(str);
    101         convert(str,temp);
    102         int ans1=calculate(temp);
    103         gets(str);
    104         convert(str,temp);
    105         int ans2=calculate(temp);
    106         if(ans1==ans2)
    107             printf("YES
    ");
    108         else
    109             printf("NO
    ");
    110     }
    111 }
    View Code


  • 相关阅读:
    谷歌在线测试题
    C++ 重载(overload)、重写(overrride)、重定义(redefine)总结
    C/S架构和B/S架构的概念和区别
    hadoop 及hbase zookeeper 经常出现问题
    Hadoop上的中文分词与词频统计实践 (有待学习 http://www.cnblogs.com/jiejue/archive/2012/12/16/2820788.html)
    HBase数据的导入和导出
    浅谈hbase表中数据导出导入(也就是备份)
    学习大数据看门的几本书
    hadoop pig入门总结
    Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别
  • 原文地址:https://www.cnblogs.com/demodemo/p/4678400.html
Copyright © 2020-2023  润新知