计算数学表达式

来源:互联网 发布:淘宝的所在地怎么修改 编辑:程序博客网 时间:2024/05/16 18:10
/*计算表达式的值*///例如输入:1-2.2*5+4/(5+6)-7*8#include<stdio.h>#include<sys/malloc.h>#include<stdlib.h>////////////////////////////////////////////////////////////////////////////////////////////////////定义数据结构#define MaxSize 50typedef union {    char ch;    double db;} two_type_data;typedef struct{    int b;//指示是数字还是运算符,如果是数字为0,如果是运算符,也用来区分是同类中是第几个运算    int pri;    two_type_data data;}Array;//////////////////////////////////////////////////////////////////////////////////////////////////void Print(Array A[],int length){    int i=0;    while(i<length){        if(A[i].b)            printf("%c",A[i].data.ch);        else            printf("%f",A[i].data.db);        i++;    }    printf("\n");}int ToPostOrder(Array A[],Array C[],int length){    int i=0,k=0;    Array D[MaxSize];//栈    int top=-1,temp;    for(i=0;i<length;i++){        if(A[i].b!=0){//判断是否是运算符            temp=A[i].pri;            if(top!=-1){                while(top!=-1 && D[top].pri>temp)                    C[k++]=D[top--];//出栈                if(top!=-1 && D[top].pri==temp){                    top--;//出栈‘(’并且舍弃‘)’                    continue;                }            }            D[++top]=A[i];//入栈            switch(A[i].data.ch){//修改栈内优先级                case '+':D[top].pri=3;break;                case '-':D[top].pri=3;break;                case '*':D[top].pri=5;break;                case '/':D[top].pri=5;break;                case '(':D[top].pri=1;break;                case ')':D[top].pri=6;break;            }        }        else            C[k++]=A[i];//如果是数字,直接输出    }    while(top!=-1)//出栈        C[k++]=D[top--];    return k;}double Calculate(Array A[],int length){//利用后序序列求解    Array D[MaxSize];//栈    int top=-1,i=0;    for(i=0;i<length;i++)        if(A[i].b==0)            D[++top]=A[i];        else            switch(A[i].data.ch){                case '+':D[top-1].data.db+=D[top].data.db;top--;break;                case '-':D[top-1].data.db-=D[top].data.db;top--;break;                case '*':D[top-1].data.db*=D[top].data.db;top--;break;                case '/':D[top-1].data.db/=D[top].data.db;top--;break;                default : break;            }    return D[top].data.db;}//主函数int main(){    //输入并处理;    char ch=0;    double temp,result=0;    int index=0,q1=1,q2=1,q3=1,q4=1,q5=1,q6=1;    int lenC=0;    Array A[MaxSize],C[MaxSize];    while(1){        scanf("%c",&ch);        if(ch>=48 && ch<=57){//连续输入数字成多位数            temp=ch-48;            while(1){                scanf("%c",&ch);                if(ch>=48 && ch<=57)                    temp=temp*10+ch-48;                else if(ch=='.'){//输入小数点                    double power=0.1;                    double dotnum=0.0;                    while(1){                        scanf("%c",&ch);                        if(ch>=48 && ch<57){                            dotnum+=(ch-'0')*power;                            power*=0.1;                        }                        else break;                    }                    temp+=dotnum;                }                if(!(ch>='0' && ch<='9'))                    break;            }            A[index].data.db=temp;            printf("%f\n",temp);            A[index].b=0;//指示数字            index++;        }        switch(ch){            case '+':A[index].data.ch=ch;A[index].b=q1++;A[index].pri=2;index++;break;            case '-':A[index].data.ch=ch;A[index].b=q2++;A[index].pri=2;index++;break;            case '*':A[index].data.ch=ch;A[index].b=q3++;A[index].pri=4;index++;break;            case '/':A[index].data.ch=ch;A[index].b=q4++;A[index].pri=4;index++;break;            case '(':A[index].data.ch=ch;A[index].b=q5++;A[index].pri=6;index++;break;            case ')':A[index].data.ch=ch;A[index].b=q6++;A[index].pri=1;index++;break;            default :break;        }        if(ch=='\n') break;    }    lenC=ToPostOrder(A,C,index);//转为树的后序    Print(C,lenC);    result=Calculate(C,lenC);    printf("%f\n",result);    return 0;}

0 0
原创粉丝点击