栈之前,中,后缀表达式求值

来源:互联网 发布:北京海隆软件 编辑:程序博客网 时间:2024/06/06 07:16
#ifndef Stack_H#define Stack_H#include<iostream>using namespace std;class Stack2;class Stack1 {    int top;    int data[20];public:    Stack1() { top=-1; }    bool IsEmpty() {  return top==-1; }    void Push(int a) { data[++top]=a; }    void Pop() { top--; }    int GetTop() { return data[top]; }    void Show();    friend class Stack2;   //设置为友元以方便字符类能访问数字栈,进行出栈运算。};class Stack2 {    int top;    char data[20];public:    Stack2() { top=-1; }                 bool IsEmpty() { return top==-1; }    void Push(char a) { data[++top]=a; }    void Pop() { top--; }    char GetTop() {  return data[top]; }    void Caculate(Stack1&);      void Fun(Stack1&);    void Fun1(Stack1&);  //为前缀表达式运算,注意运算的时候,第一个数为刚扫描的数字,而已经入栈的数为第二个数。调用的是caculate1,和前中缀运算有所不同    void Priority(Stack1&);     void Caculate1(Stack1&A);};#endif
#include<iostream>using namespace std;#include "Stack.h"void Stack1::Show(){    cout<<"*****************************栈的三种表达式*****************************"<<endl;    cout<<"                                                                        "<<endl;    cout<<"       1 中缀表达式                               2 前缀表达式          "<<endl;    cout<<"                                                                        "<<endl;    cout<<"       3 后缀表达式                               4 离开(Q)             "<<endl;    cout<<"                                                                        "<<endl;    cout<<"************************************************************************"<<endl;}void Stack2::Caculate(Stack1&A){                             //注意先出栈的是哪个数    int a=A.GetTop();    A.Pop();    int b=A.GetTop();    A.Pop();    if(GetTop()=='+')    {         int c=b+a;                 Pop();        A.Push(c);    }    if(GetTop()=='-')    {        int c=b-a;        Pop();        A.Push(c);    }    if(GetTop()=='*')    {        int c=b*a;        Pop();        A.Push(c);    }    if(GetTop()=='/')    {        int c=b/a;        Pop();        A.Push(c);    }}void Stack2::Caculate1(Stack1&A)   //用于前缀表达式所用的运算,从右边开始扫描{    int a=A.GetTop();    A.Pop();    int b=A.GetTop();    A.Pop();    if(GetTop()=='+')    {         int c=a+b;                 Pop();        A.Push(c);    }    if(GetTop()=='-')    {        int c=a-b;        Pop();        A.Push(c);    }    if(GetTop()=='*')    {        int c=a*b;        Pop();        A.Push(c);    }    if(GetTop()=='/')    {        int c=a/b;        Pop();        A.Push(c);    }}void Stack2::Fun(Stack1&A){    char ch;    while(cin>>ch)    {        if(ch>='0'&&ch<='9')        {            A.Push(ch-48);            continue;        }        else {            if(top==-1)            {                Push(ch);                continue;            }            if(ch=='#')            {       cout<<A.GetTop()<<endl; return ; }            else {                Push(ch);                Caculate(A);            }        }    }}void Stack2::Fun1(Stack1&A)  {    char ch[20];    ch[0]='#';    for(int j=1;j<20;j++)    {           cin>>ch[j];        if(ch[j]=='#') break;    }    for(int i=j;i>=0;i--)    {         if(i==j){ Push(ch[j-1]);  continue; }        if(ch[i]>='0'&&ch[i]<='9')        {            A.Push(ch[i]-48);            continue;        }        else {            if(ch[i]=='#') { cout<<A.GetTop()<<endl; return ;}            else {                Push(ch[i]);                Caculate1(A);            }        }    }}void Stack2::Priority(Stack1&A){    char ch;     while(cin>>ch)       {          if(ch>='0'&&ch<='9')        {              A.Push(ch-48);              continue;        }        if(ch=='(' || top==-1)         {             Push (ch);            continue;         }           if(ch=='#')        {             while(top!=0)                 Caculate(A);            cout<<A.GetTop()<<endl;            return;        }        if( ch==')')        {            while(GetTop()!='(')                Caculate(A);            Pop();        }        if(ch=='+'||ch=='-') {                if(top==0) { Push(ch); continue; }                if(GetTop()=='(') { Push(ch); continue; }                 Caculate(A);                  Push(ch);                  continue;        }        if( ch=='/'|| ch=='*')        {                char ch1=GetTop();                if(top==0){ Push(ch);  continue; }                if(GetTop()=='(') { Push(ch); continue; }                if(ch1=='+' ||ch1== '-'){                    Push(ch);                    continue;            }                else   {                    Caculate(A);                    Push(ch);                    continue;                }        }    }}
#include<iostream>#include"Stack.h"using namespace std;int main(){    Stack1 A;    Stack2 B;    char p;    A.Show();    while(cin>>p)    {           if(p=='Q') return 0;        switch(p)        {        case '1':    B.Priority(A);  cin.get(); cin.get();system("cls");   break;        case '2':    B.Fun1(A);      cin.get(); cin.get();system("cls");   break;        case '3':    B.Fun(A);       cin.get(); cin.get();system("cls");  break;        case '4':    return 0;            break;        }        A.Show();    }}

以上代码是栈的前中缀表达式运算,但是没有增加十位数的运算,只能用于个位数的运算,在VC6.0上运行没什么问题,在DEV上可能存在编译器差异的问题。

原创粉丝点击