• 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 ");
        }
    }
  • 相关阅读:
    向量、矩阵常用范数
    关于HP M451网络连接的资料
    pyqt程序最小化到系统托盘(未测试)
    博客园美化大集合2020最新!不用担心失效问题!
    添加QQ聊天
    关于加密
    python抓取谷歌学术关键词下文章题目
    如何确定网站可否可爬取
    灵狐浏览器
    利用beautifulsoup进行对标签的二次查找-以打印网易云歌单内容为例
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3289003.html
Copyright © 2020-2023  润新知