zoukankan      html  css  js  c++  java
  • poj1686

     

    Lazy Math Instructor

     
    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
     
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <stack>
    using namespace std;
    const int maxn = 90;
    int priority(char c)
    {
        if(c=='(')
            return 0;
        else if(c=='*')
            return 2;
        else
            return 1;
    }
    void convert(char *str,char *temp)
    {
        int len = strlen(str),t = 0,i;
        char c;
        stack<char>st;
        for(i=0; i<len; i++)
        {
            if(str[i]!=' ')
            {
                c = str[i];
                if((c<='z'&&c>='a')||(c>='0'&&c<='9'))
                    temp[t++]=c;
                else
                {
                    if(st.empty()||c=='(')
                        st.push(c);
                    else if(c==')')
                    {
                        while(!st.empty()&&st.top()!='(')
                        {
                            temp[t++]=st.top();
                            st.pop();
                        }
                        st.pop();
                    }
                    else
                    {
                        while(!st.empty()&&priority(c)<=priority(st.top()))
                        {
                            temp[t++]=st.top();
                            st.pop();
                        }
                        st.push(c);
                    }
                }
            }
        }
        while(!st.empty())
        {
            temp[t++]=st.top();
            st.pop();
        }
        temp[t]=0;
    }
    int calculate(char *temp)
    {
        int len = strlen(temp),x,y,z,i;
        char c;
        stack<int> st;
        for(i=0; i<len; i++)
        {
            c=temp[i];
            if(c>='0'&&c<='9')
                st.push(c-'0');
            else if(c<='z'&&c>='a')
                st.push(int(c));
            else
            {
                x=st.top();
                st.pop();
                y=st.top();
                st.pop();
                switch(c)
                {
                case '*':
                    z = x*y;
                    break;
                case '+':
                    z = x+y;
                    break;
                case '-':
                    z = y-x;
                    break;
                }
                st.push(z);
            }
        }
        return st.top();
    }
    int main()
    {
        char str[maxn],temp[maxn];
        int n;
        scanf("%d",&n);
        getchar();
        while(n--)
        {
            gets(str);
            convert(str,temp);
            int ans1=calculate(temp);
            gets(str);
            convert(str,temp);
            int ans2=calculate(temp);
            if(ans1==ans2)
                printf("YES ");
            else
                printf("NO ");
        }
    }
  • 相关阅读:
    币值转换
    第八周作业
    第七周作业
    第五周编程总结
    第四周编程总结
    第三周编程总结
    7-1 查找整数
    7-2 求最大值及其下标
    秋季学习总结
    对我影响最大的三个老师
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3289003.html
Copyright © 2011-2022 走看看