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 ");
        }
    }
  • 相关阅读:
    HP Nonstop SQLMX (SPJ) Stored Procedure in Javas Getting Started
    Term of Wall Street
    iOS自带地图纠偏问题
    Android 界面排版的5种方式
    Android底部导航栏
    Android中Intent传值与Bundle传值的区别详解
    Android开发EditText属性
    从点击一个链接到浏览器显示页面,这个过程中发生了什么?
    HTML的常用标签属性及使用时需注意的一些细节
    写一个简单的脚本,并在脚本生成的的文件中添加内容
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3289003.html
Copyright © 2011-2022 走看看