正文

表达式求值(测试版)2005-08-12 17:24:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/elva6401/3712.html

分享到:

#include <iostream.h>

struct list
{
    int index;
    int a;
    char b;
};
struct list s[100];
int comp1(char a,char b)
{
    char op[4][2]={'*','/','+','-','(','(',')',')'};
    int i;
    int a1,b1;
    for(i=0;i<4;i++)
    if (a==op[i][0] || a==op[i][1])
    {
        a1=i;
        break;
    }
    for(i=0;i<4;i++)
    if (b==op[i][0] || b==op[i][1])
    {
        b1=i;
        break;
    }
    if (a1<b1) return 1;
    else return 0;
}
int comp2(char a,char b)
{
    char op[4][2]={'(','(','*','/','+','-',')',')'};
    int i;
    int a1,b1;
    for(i=0;i<4;i++)
    if (a==op[i][0] || a==op[i][1])
    {
        a1=i;
        break;
    }
    for(i=0;i<4;i++)
    if (b==op[i][0] || b==op[i][1])
    {
        b1=i;
        break;
    }
    if (a1<b1) return 1;
    else return 0;
}
int oper(int a,int b,char op)
{
    int result;
    if(op=='*') result=a*b;
    else if(op=='/') result=a/b;
    else if(op=='+') result=a+b;
    else if(op=='-') result=a-b;
    return result;
}

int main()
{
    int len=0,i=0;
    int number=0;
    char enter[100];
    cin>>enter;
    while(enter[i]!='\0')
    {
        if(enter[i]>='0' && enter[i]<='9')
        {
            if (enter[i+1]>='0' && enter[i+1]<='9')
                number=10*number+enter[i]-'0';
            else
            {
            s[len].index=1;
            s[len].a=10*number+enter[i]-'0';
            s[len].b='0';
            len++;
            number=0;
            }
        }
        else
        {

            s[len].index=0;
            s[len].a=0;
            s[len].b=enter[i];
            len++;
        }    
        i++;
    }
    /*for (i=0;i<len;i++)
    {
        if (s[i].index)
        cout<<s[i].index<<" "<<s[i].a<<endl;
        else
        cout<<s[i].index<<" "<<s[i].b<<endl;
    }*/
    int s1[100],top1=0,top2=0;
    char s2[100];
    s2[top2]='(';top2++;
    for(i=0;i<len;i++)
    {
        if (s[i].b=='=')
        {
            while(top1!=0)
            {
            int m=s1[top1-1];
            top1--;
            int n=s1[top1-1];
            top1--;
            char op=s2[top2-1];
            top2--;
            int result=oper(n,m,op);
            s1[top1]=result;
            top1++;
            }
        }

        if(s[i].index==1)
        {
            s1[top1]=s[i].a;
            top1++;
        }
        else
        {
            if (comp1(s[i].b,s2[top2-1])==1)
            {
                s2[top2]=s[i].b;
                top2++;
            }
            else
            {
                while(comp2(s[i].b,s2[top2-1])==0)
                {
                    int m=s1[top1-1];
                    top1--;
                    int n=s1[top1-1];
                    top1--;
                    char op=s2[top2-1];
                    top2--;
                    int result=oper(n,m,op);
                    s1[top1]=result;
                    top1++;
                }
            }
        }
    }
    //cout<<s1[0]<<endl;
    /*char a,b;
    while(cin>>a>>b)
    {
        cout<<comp1(a,b)<<" "<<comp2(a,b)<<endl;
    }
    */
}

阅读(2563) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册