Chinaunix首页 | 论坛 | 博客
  • 博客访问: 104578334
  • 博文数量: 19283
  • 博客积分: 9968
  • 博客等级: 上将
  • 技术积分: 196062
  • 用 户 组: 普通用户
  • 注册时间: 2007-02-07 14:28
文章分类

全部博文(19283)

文章存档

2011年(1)

2009年(125)

2008年(19094)

2007年(63)

分类: C/C++

2008-04-20 18:06:10

作者:    

原帖及讨论:

#include
#include
union nuionnum
{
    double num1;
    char num2;
};
struct lianbiao
{
    struct lianbiao *p;
    union nuionnum num;
    int flage;//标志是数字还是符号

};
void main()
{    
    char c;
    struct lianbiao lianhe;
    struct lianbiao *next;
    printf("请输入一个计算机能识别的四则运算表达式,如:a+b*(c+d)/e\n");
    while(1)
    {
first:
        next=&lianhe;
        while((int)(c=getchar())!=10)
        {    
loop:
        //将表达式放入复合链表中
            if (c=='+'||c=='-'||c=='*'||c=='/'||c=='('||c==')')
            {
                next->num.num2=c;
                if (c== '+' || c== '-' )
                    next->flage =1;
                else if(c== '*' ||c== '/' )
                    next->flage =2;
                else if(c== '(' )
                    next->flage =3;
                else if(c== ')' )
                    next->flage =4;
                next->p=calloc(1,sizeof(struct lianbiao));
                next=next->p;
                next->p=NULL;
            }
            else if(c>='0'&&c<='9')
            {  long int t=1;
               int flag=0;
                next->num.num1=c-48;
                while(((c=getchar())>='0'&&c<='9')||c=='.')
                {    
                    if (flag==1 && c!='.')
                            {
                                t=t*10;
                                next->num.num1=next->num.num1+(c-48.0)/t;
                            }
                    else if(c>='0'&&c<='9')
                        next->num.num1=next->num.num1*10+c-48;

                    if (c=='.')
                    {
                        flag=1;
                    }
                }
                next->flage =0;
                next->p=calloc(1,sizeof(struct lianbiao));
                next=next->p;
                next->p=NULL;
                if (c=='\n')break;
                goto loop;
            }
            else
            {
                printf("不是合法的算术表达式!!!\n请重新输入:\n");
                while(getchar()!='\n');//设置重新输入
                goto first;
            }
        }
//将链表中的数字和符号分别入栈
    next=&lianhe;
    {
        int i=0,j=0;//用来标记栈的顶点
        double arrd[100];
        char arrc[2][100];
        while(next->p!=NULL)
        {
            if (next->flage !=0&&next->num.num2==')')//括号的外理
            {
                while(arrc[0][i-1]!='(')//出栈
                {
                    if (arrc[0][i-1]=='+')
                    {
                        arrd[j-2]=arrd[j-2]+arrd[j-1];
                        i=i-1;
                        j=j-1;
                    }
                    else if(arrc[0][i-1]=='-')
                    {
                        arrd[j-2]=arrd[j-2]-arrd[j-1];
                        i=i-1;
                        j=j-1;
                    }
                    else if(arrc[0][i-1]=='*')
                    {
                        arrd[j-2]=arrd[j-2]*arrd[j-1];
                        i=i-1;
                        j=j-1;
                    }
                    else if(arrc[0][i-1]=='/')
                    {
                        arrd[j-2]=arrd[j-2]/arrd[j-1];
                        i=i-1;
                        j=j-1;
                    }
                }
                i=i-1;
            }
            else if (next->num.num2 =='(')//入栈
            {
                arrc[1][i]=next->flage;
                arrc[0][i]=next->num.num2;
                i++;
            }
            else if (next->flage !=0)//对运算复符的理
            {
front:
                if (i>0&&next->flage                {
                    if (arrc[0][i-1]=='*')
                    {
                        arrd[j-2]=arrd[j-2]*arrd[j-1];
                        i=i-1;
                        j=j-1;
                    }
                    else if(arrc[0][i-1]=='/')
                    {
                        arrd[j-2]=arrd[j-2]/arrd[j-1];
                        i=i-1;
                        j=j-1;    
                    }
                    goto front;
                }
                arrc[1][i]=next->flage;
                arrc[0][i]=next->num.num2;
                i++;
            }
            else
            {
                arrd[j]=next->num.num1; //对数字的理
                j++;
            }
            next=next->p;
        }
        while(i!=0)//全部入栈之后对椎的外理
        {
                if (arrc[0][i-1]=='+')
                    {
                        arrd[j-2]=arrd[j-2]+arrd[j-1];
                        i=i-1;
                        j=j-1;
                    }
                else if (arrc[0][i-1]=='-')
                    {
                        arrd[j-2]=arrd[j-2]-arrd[j-1];
                        i=i-1;
                        j=j-1;
                    }
                else if (arrc[0][i-1]=='*')
                    {
                        arrd[j-2]=arrd[j-2]*arrd[j-1];
                        i=i-1;
                        j=j-1;
                    }
                    else if(arrc[0][i-1]=='/')
                    {
                        arrd[j-2]=arrd[j-2]/arrd[j-1];//1+2*3/4+5   1 1.5 5   ++
                        i=i-1;
                        j=j-1;    
                    }
        }
        printf("%lf\n",arrd[0]);

    }
    }
}

 

阅读(424) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~