简单表达式求值

作者在 2011-10-25 21:29:32 发布以下内容
标题: 2、操作符重载
时 限: 1000 ms
内存限制: 10000 K
总时限: 3000 ms
描述:

定义有理数Rational类。成员变量:分子int Up; 分母 int Down。

完成成员函数:Rational operator + (int num);

              Rational operator + (Rational r);

              Rational operator * (int num);

              Rational operator * (Rational r);

完成友元函数:friend Rational operator + (int num, Rational r);

              friend Rational operator * (int num, Rational r);

 

用户输入操作,可以为双操作数的加、乘运算,或多操作数的连加、连乘运算,由屏幕返回运算结果。exit退出

输入:

运算操作;

exit。

输出:

运算结果。

输入样例:

1/3+1/3+1/3

2+1/3

1/3+2

2*1/3

1/3*2*1/3

exit

输出样例:

1

7/3

7/3

2/3

2/9

提示:

设计Rational类的成员函数Reduce(),使其能够对输入的有理数约分。

计算结果的输出要求:若为整数,不以分数形式输出。例如,结果为1,输出为1,而非1/1。

来源:
#include <iostream>
#include <stack>
using namespace std;

class Rational
{
private:
    int up;
    int down;
public:
    void reduce()
    {
        if(up==0) return;
        int a,b,c;
        a=up,b=down;
        if(a<b)
        {
            a=a+b;
            b=a-b;
            a=a-b;
        }
        do
        {
            c=a%b;
            a=b;
            b=c;
        }
        while(c!=0);
        up=up/a;
        down=down/a;
    }
    Rational():up(0),down(1) {}
    Rational(int _up,int _down):up(_up),down(_down)
    {
        reduce();
    }
    ~Rational() {}
    void display()
    {
        if(up==0){cout<<up<<endl; return;}
        if(1==down||down==-1) {cout<<up/down<<endl; return;}
        if(up<0&&down>0){ cout<<up<<'/'<<down<<endl;return;}
        if(up>0&&down<0){ cout<<'-';cout<<up<<'/'<<-down<<endl;return;}
        if(up<0&&down<0){ cout<<-up<<'/'<<-down<<endl;return;}
        else cout<<up<<'/'<<down<<endl;
    }
    Rational operator +(int num)
    {
        return Rational (up+num*down,down);
    }
    Rational operator +(Rational r)
    {
        return Rational (up*r.down+r.up*down,r.down*down);
    }
    Rational operator *(int num)
    {
        return Rational (up*num,down);
    }
    Rational operator *(Rational r)
    {
        return Rational (up*r.up,down*r.down);
    }
    //friend Rational operator + (int num, Rational r);
    
//friend Rational operator * (int num, Rational r);
};
/*Rational::Rational operator +(int num, Rational r)
{
    return Rational(num*r.down+r.up,r.down);
}
Rational::Rational operator * (int num, Rational r)
{
    return Rational(num*r.up,r.down);
}
*/
char opera[] = {'+', '*', '#'};
char o_prior[3][3]=
{
    {'<','>','<'},
    {'<','<','<'},
    {'>','>','='}
};
bool exit_in_opera(char c,char *str)
{
    for (int i=0; str[i]!='\0'; i++)
    {
        if(c==str[i])
            return true;
    }
    return false;
}
int change_in_subpt(char c)
{
    switch(c)
    {
    case '+':
        return 0;
    case '*':
        return 1;
    case '#':
        return 2;
    }
}
char judge_prior(char oprt,char c)
{
    return o_prior[change_in_subpt(oprt)][change_in_subpt(c)];
}
Rational calculate(Rational a,char c,Rational b)
{
    if(c=='+')
        return a+b;
    if(c=='*')
        return a*b;
}
Rational evaluate_expression(string expression)
{
    stack<Rational>OPND;
    stack<char>OPTR;
    int up,down;
    Rational a,b;

    OPTR.push('#');
    expression.push_back('#');
    string::iterator iter = expression.begin();
    char c=*iter;
    while(c!='#'|| OPTR.top() != '#')
    {
        if(!exit_in_opera(c,opera))
        {
            up=0;
            down=1;
            int flag1=0;
            if(c=='-'){flag1=1;c=*(++iter);}
            while (c>='0'&&c<='9')
            {
                up=up*10+c-'0';
                c=*(++iter);
            }
            if(flag1==1){up=-up;}
            if(c=='/')
            {
                c=*(++iter);
                int flag2=0;
                if(c=='-'){flag2=1;c=*(++iter);}
                down=0;
                while (c>='0'&&c<='9')
                {
                    down=down*10+c-'0';
                    c=*(++iter);
                }
                if(flag2==1){down=-down;}
            }
            OPND.push(Rational(up,down));
        }
        else
        {
            switch(judge_prior(OPTR.top(),c))
            {
            case '<':
            {
                char before_op=OPTR.top();
                OPTR.pop();
                b = OPND.top();
                OPND.pop();
                a = OPND.top();
                OPND.pop();
                OPND.push(calculate( a,before_op,b));
                break;
            }
            case '>':
            {
                OPTR.push(c);
                c=*(++iter);
                break;
            }
            case '=':
            {
                OPTR.pop();
                c=*(++iter);
            }
            }
        }
    }
    return OPND.top();
}
int main()
{
    /*string str;
    cin>>str;
    while(str!="exit")
    {
        str.length()
    }
*/
    /*Rational a(2,3);
    Rational b(0,1);
    b=a*a*a;
    a.display();
    b.display();
    b=2+a;
    b.display();
*/
    string expression;
    cin>>expression;
    while (expression!="exit")
    {
        Rational result=evaluate_expression(expression);
        result.display();
        cin>>expression;
    }
    return 0;
}
c++ | 阅读 964 次
文章评论,共0条
游客请输入验证码
浏览70040次