第三章:顺序栈及其应用之一---表达式求值

/*

能力有限,代码还不完善,每次进行计算的需要压栈出栈的结果,不能超过10...

*/

  1 #include 
  2 #include 
  3 using namespace std;
  4 
  5 #include 
  6 #include <string.h>
  7 char fu[8][8]={">><<<>>",">><<<>>",">>>><>>",">>>><>>",
  8                 "<<<<<=",">>>>&>>","<<<<<&="};
  9 #define MAXSIZE 100
 10 #define ElemType char
 11 typedef struct LNode
 12 {
 13     ElemType *base;
 14     ElemType *top;
 15     int lenth;
 16 }LNode,*LinkList;
 17 
 18 LinkList Open,Oper;
 19 //全局变量,
 20 void Creat(LinkList &L)
 21 {
 22     L=new LNode;
 23     //初始化顺序栈,分配空间;
 24     L->base = new ElemType[MAXSIZE];
 25     L->top= L->base ;
 26     L->lenth = MAXSIZE;
 27     printf("顺序栈初始化完毕!\n");
 28 }
 29 
 30 int Pan(LinkList L)
 31 {
 32     //栈空返回1;否则返回0;
 33     if((L->top)-(L->base)) return 1;
 34     else return 0;
 35 }
 36 void Push(LinkList &L,ElemType e)
 37 {
 38     //将元素e压入栈中;
 39     if(L->top-L->base == MAXSIZE) {printf("栈已满!\n");}
 40     *(L->top)=e;
 41     L->top++;
 42 }
 43 
 44 void Pop(LinkList &L, ElemType &e)
 45 {
 46     //退出栈顶元素,并将其存入元素e中;
 47     if(L->top == L->base) {printf("栈已空!\n");}
 48     L->top--;
 49     e=(*L->top);
 50 }
 51 char Get(LinkList &L)
 52 {
 53     //取出栈顶元素;
 54     if(L->top == L->base ) printf("栈顶没有元素!\n");
 55     return *(L->top-1);
 56 }
 57 
 58 
 59 char Pre(char x,char y)
 60 {
 61     //运算符优先级比较,返回 > < = ;
 62     char str[10]={"+-*/()#"};
 63     int a,b,i;
 64     for(i=0;i<10;i++)
 65     {
 66         if(x==str[i]) a=i;
 67         if(y==str[i]) b=i;
 68     }
 69     return fu[a][b];
 70 }
 71 int Operate(char a,char x,char b)
 72 {
 73     //运算,这里只包含整除的情况;
 74     if(x=='+') return a+b-'0'-'0';
 75     else if(x=='-') return a-b-'0'-'0';
 76     else if(x=='*') return (a-'0')*(b-'0');
 77     else return (a-'0')/(b-'0');
 78 }
 79 char PP()
 80 {
 81     //这是一个计算运算表达式的函数;
 82     printf("这是一个计算表达式的函数!\n请输入一个表达式:\n");
 83     char f[30];
 84     Push(Oper,'#');
 85     char ch;cin>>ch;
 86     while(ch!='#' || Get(Oper)!='#')
 87     {
 88         if(ch>='0'  && ch<='9'){ Push(Open,ch);cin>>ch;}
 89         else
 90         {
 91             if(Pre(Get(Oper),ch)=='<') //这里需要一个比较两字符大小的函数,需要用到优先级表;
 92             {
 93                 Push(Oper,ch);cin>>ch;
 94             }
 95             else if(Pre(Get(Oper),ch)=='>')
 96             {
 97                 char a,b,oper;
 98                 Pop(Open,b);Pop(Open,a);
 99                 Pop(Oper,oper);
100                 int num=Operate(a,oper,b);//这里需要一个计算表达式值得函数;
101                 Push(Open,num+'0');//将计算的字符重新入栈;
102             }
103             else if(Pre(Get(Oper),ch)=='=')
104             {
105                 char t;
106                 Pop(Oper,t);
107                 cin>>ch;
108             }
109         }
110     }
111     return Get(Open);
112 }
113 int main()
114 {
115     Creat(Open);Creat(Oper);
116     char m=PP();
117     printf("%c\n",m);
118 
119     return 0;
120 }

 

转载于:https://www.cnblogs.com/songacm/p/3341463.html

你可能感兴趣的:(第三章:顺序栈及其应用之一---表达式求值)