用栈实现中缀表达式c++

来源:互联网 发布:lol韩服网络错误 编辑:程序博客网 时间:2024/06/08 19:55
```#include "isotream"using namespace std;class  stack{public:int maxsize;    int top;    char *ListArray;    stack(int size = 0);    void push(char s);    bool pop(char &x);    void clear();    void display();    bool lsEmpty();    bool getTop(char &x);};class expression{private:    string infix;    string postfix;public:    int isp(char ch);    int icp(char ch);    expression();    void get_infix();    void show_infix();    void converttopostfix();    void show_postfix();};#include "iostream"#include "string"#include "stack.h"using namespace std;stack::stack(int size){    maxsize = size;    top = 0;    ListArray = new char[maxsize];}void stack::push(char s){    ListArray[top] = s;    top++;}bool stack::pop(char &x){    if (top == 0)        return false;    else        x = ListArray[top - 1];         top--;    return true;}void stack::display(){    int i = top;    while (i > 0)    {        i = i - 1;        cout << ListArray[i] << " ";    }    cout << endl;}void stack::clear(){    top = 0;}bool stack::lsEmpty(){    if (top == 0)        return true;}int expression::isp(char ch)//栈内优先级{    switch (ch)    {    case '#':        return 0;    case '(':        return 1;    case '+':    case '-':        return 3;    case '*':    case '/':    case '%':        return 5;    case ')':        return 6;    default:        return -1;    }}int expression::icp(char ch)//栈外优先级{    switch (ch)    {    case '#':        return 0;    case '(':        return 6;    case '+':    case '-':        return 2;    case '*':    case '/':    case '%':        return 4;    case ')':        return 1;    default:        return -1;    }}bool stack::getTop(char &x){    if (top == 0)        return false;    x = ListArray[top - 1];    return true;}void expression::get_infix(){    cout << "input infix:";    cin >> infix;}expression::expression(){    infix = "";    postfix = "";}void expression::show_infix(){    cout << infix << endl;}/*主要的思想:如果栈外的优先级高于栈内的优先级,那么就要将栈外的符号压入栈内,反之就将栈内的元素弹出,赋值给后缀表达式,栈外的符号继续与栈内的符号比较直到栈内的符号优先级小于栈外的,此时将栈外的符号压入栈内,如果优先级相同就判断是否是括号,如果是就将括号弹出*/void expression::converttopostfix(){    stack s;    char ch = '#', ch1, op;    int i = 0;    int length = infix.length();//字符串的长度,用来判断字符串什么时候读完    s.push(ch);    ch = infix.at(0);    while ((ch != '#') || !s.lsEmpty())    {        if (isdigit(ch))//如果ch是数字        {            postfix += ch;   //成为后缀表达式的一部分            //cout << postfix << endl;            i++;             if (i < length)            ch = infix.at(i);//继续读取下一个字符            else   //此时是表示字符串中的元素已经读取完了,然而栈内此时栈内的符号还没有完全弹出赋值给后缀表达式            {                       while (s.top>1)                {                    s.pop(op);                    postfix += op;//将符号弹出赋值给后缀表达式,直至到'#'                }               ch='#';//结束的标志            }        }        else          //不是数字        {            s.getTop(ch1);  //得到栈内的第一个元素ch1            if (isp(ch1) < icp(ch))//栈外优先级大于栈内            {                s.push(ch);   //进栈                i++;                if (i<length)                ch = infix.at(i);//读取下一个字符                else                {                    while (s.top>1)                    {                        s.pop(op);                        postfix += op;                    }                    ch = '#';                }            }            else if (isp(ch1)>icp(ch))//栈内优先级大与栈外            {                s.pop(op);//出栈                postfix += op;//成为后缀表达式的一部分                //cout << postfix << endl;            }            else  //优先级相同            {                s.pop(op);                if (op == '(')                    i++;                if (i<length)                    ch = infix.at(i);                else                {                    while (s.top>1)                    {                        s.pop(op);                        postfix += op;                    }                    ch = '#';                }            }        }    }}void expression::show_postfix(){    cout << postfix << endl;}
0 0
原创粉丝点击