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 ");
        }
    }
  • 相关阅读:
    集合类... 分类: java 20091102 14:22 193人阅读 评论(0) 收藏
    全选复选框 分类: 网页编程【html、js】 20091102 20:56 257人阅读 评论(0) 收藏
    数组自带的push方法 分类: 网页编程【html、js】 20091102 20:51 235人阅读 评论(0) 收藏
    Scanner类 分类: java 20091102 14:18 188人阅读 评论(0) 收藏
    计算阶乘之和 分类: java 20091102 14:12 206人阅读 评论(0) 收藏
    冒泡排序... 分类: java 20091102 14:09 224人阅读 评论(0) 收藏
    获得html 属性值的集中方法 分类: 网页编程【html、js】 20090908 12:32 861人阅读 评论(1) 收藏
    OpenAPI系列:汇总
    OpenAPI系列: 二、资源
    软件生存周期文档系列 之 9.开发进度月报
  • 原文地址:https://www.cnblogs.com/lxm940130740/p/3289003.html
Copyright © 2011-2022 走看看