【表达式求值---链栈处理】

来源:互联网 发布:淘宝网店排名规则 编辑:程序博客网 时间:2024/05/09 09:10

数据结构作业记录。
如有BUG ,欢迎指出 !!

#include<iostream>#include<cstring>#include<cstdio>#include<cmath>using namespace std;#define status int #define OK  1 #define ERROR 0 struct Node1{  // 存储操作数的栈     double op;    Node1* next;}node1;struct LinkStack1{      Node1 *base=new Node1;    bool init(){        base->next=NULL;        return OK;    }     bool Push(double val){        Node1 *p=new Node1 ;        p->op=val;        p->next=base->next;        base->next=p;        return OK;    }    double Pop(){        Node1 *p;        p=base->next;        base->next=p->next;        double val=p->op;        delete p;        return val;    }     double Top(){         return base->next->op;    }}OPND;struct Node2{  //  存储 操作符的栈     char op;    Node2* next;}node2;struct LinkStack2{    Node2 *base=new Node2;    bool init(){        base->next=NULL;        return OK;    }     bool Push(char val){        Node2 *p=new Node2 ;        p->op=val;        p->next=base->next;        base->next=p;        return OK;    }    char Pop(){        Node2 *p;            p=base->next;            base->next=p->next;            char val=p->op;            delete p;            return val;    }     char Top(){         return base->next->op;    }}OPTR;bool In(char c){  //  判断输入     if(c>='0'&&c<='9') return ERROR;    else return OK;}char pcde[7][7] = { '>','>','<','<','<','>','>',                    '>','>','<','<','<','>','>',                    '>','>','>','>','<','>','>',                    '>','>','>','>','<','>','>',                    '<','<','<','<','<','=','0',                    '>','>','>','>','0','>','>',                    '<','<','<','<','<','0','='};char Precede(char a,char b){  //  优先级     int x,y;    if (a == '+') x=0;if (a == '-') x=1;if (a == '*') x=2;    if (a == '/') x=3;if (a == '(') x=4;if (a == ')') x=5;if (a == '#') x=6;    if (b == '+') y=0;if (b == '-') y=1;if (b == '*') y=2;    if (b == '/') y=3;if (b == '(') y=4;if (b == ')') y=5; if (b == '#') y=6;    //printf("fuhao  %c\n",pcde[x][y]);    return pcde[x][y];}double Operate(double a,char ch,double b){  //  计算     double ans;    switch(ch){        case '+':{            ans=a+b;            break;        }         case '-':{            ans=a-b;            break;        }        case '*':{            ans=a*b;            break;        }        case '/':{            ans=a/b;            break;        }    }    return ans;}double  Calculate(){    OPND.init();    OPTR.init();    printf("请输入表达式(表达式以 # 结束输入):");    OPTR.Push('#');    char ch; cin>>ch;    while(ch!='#'||OPTR.Top()!='#') {        if(!In(ch)){              double x=0;   //  获取超过1位数的数字值             while(!In(ch)){                x=x*10+ch-'0';                cin>>ch;            }             OPND.Push(x);         }        else switch(Precede(OPTR.Top(),ch)){             case '<':{                OPTR.Push(ch); cin>>ch;                break;            }            case '>':{                char theta; double a,b,c;                 theta=OPTR.Pop();                a=OPND.Pop(); b=OPND.Pop();                //printf("%lf %lf %c\n",a,b,theta);                OPND.Push(Operate(b,theta,a));                break;            }            case '=':{                OPTR.Pop(); cin>>ch;                break;            }        }    }    return OPND.Top();}int main(){    puts("-------欢迎使用-------");    while(1){        printf("\nA :计算表达式值(大于9的数字也可以计算)\n");        printf("Q :退出\n");        char op[5];puts("");        printf("请输入操作指令: ");        scanf("%s",op);            if(op[0]=='Q') break;        else             if(op[0]=='A')  printf("表达式的值为: %.3lf\n", Calculate());         else              puts("您输入的指令有误,请重新选择!!");    }    puts("欢迎下次使用!");    return 0;}

二 C++ 自带的栈写。

#include<bits/stdc++.h>using namespace std;const int MAXN = 1e6;char s[MAXN];char pcde[7][7] = { '>','>','<','<','<','>','>',                    '>','>','<','<','<','>','>',                    '>','>','>','>','<','>','>',                    '>','>','>','>','<','>','>',                    '<','<','<','<','<','=','0',                    '>','>','>','>','0','>','>',                    '<','<','<','<','<','0','='};char Precede(char a,char b){  //  优先级     int x,y;    if (a == '+') x=0;if (a == '-') x=1;if (a == '*') x=2;    if (a == '/') x=3;if (a == '(') x=4;if (a == ')') x=5;if (a == '#') x=6;    if (b == '+') y=0;if (b == '-') y=1;if (b == '*') y=2;    if (b == '/') y=3;if (b == '(') y=4;if (b == ')') y=5; if (b == '#') y=6;    //printf("fuhao  %c\n",pcde[x][y]);    return pcde[x][y];}double Calculate(){    stack<double>S;    stack<char>F;    F.push('#'); int i=1;    while(s[i]!='#'||F.top()!='#'){        if(s[i]>='0'&&s[i]<='9') {                double x=0;                while(s[i]>='0'&&s[i]<='9'){                    x=x*10+s[i]-'0';                    i++;                }                   S.push(x);            }else switch(Precede(F.top(),s[i])){                case '<':{                    F.push(s[i]); i++;                    break;                }                case '>':{                    double a,b,ans;char ch;                    ch=F.top(); F.pop();                    a=S.top();S.pop();b=S.top();S.pop();                            if(ch=='+') ans=a+b;                        else                             if(ch=='-') ans=a-b;                        else                             if(ch=='*') ans=a*b;                        else                             if(ch=='/') ans=a/b;                //  printf("%lf %lf %c\n",a,b,ch);                     S.push(ans);                    break;                }                case '=':{                       F.pop();i++;                    break;                  }        }       }    return S.top();}int main(){    int T ;scanf("%d",&T);    while(T--){        scanf("%s",s+1);        int len=strlen(s+1);s[0]=s[len+1]='#';        printf("%.3lf\n",Calculate());    }       return 0;}
原创粉丝点击