中缀转前缀

来源:互联网 发布:淮南南乡子大数据 编辑:程序博客网 时间:2024/06/05 19:41
#include <iostream>#include <string.h>#include <math.h>using namespace std;struct num_strT{    //0代表数字 1 代表字符    bool flag;    int num;    char str;};class stack1{    public:    char wait[1005];    int count;    stack1(){count=0;}    void push(char s){wait[count++]=s;}    void pop(){count--;}    char top(){return wait[count-1];}    bool empty(){return count==0;}    void show()    {        for(int i=0;i<count;i++)        {            cout<<wait[i]<<" ";        }    }};class stack_num{    public:    int wait[1005];    int count;    stack_num(){count=0;}    void push(int s){wait[count++]=s;}    void pop(){count--;}    int top(){return wait[count-1];}    bool empty(){return count==0;}    void show()    {        for(int i=0;i<count;i++)        {            cout<<wait[i]<<" ";        }    }};class screenC{    public:    num_strT screen[1005];    int count;    screenC(){count=0;}    void push(num_strT s){screen[count++]=s;}    void pop(){count--;}    num_strT top(){return screen[count-1];}    bool empty(){return count==0;}    void show()    {        for(int i=count-1;i>=0;i--)        {            if(screen[i].flag==0)cout<<screen[i].num<<" ";            else{cout<<screen[i].str<<" ";}        }    }};int main(){    //两个栈    //数字直接入screen栈,字符入 waiting栈    //screen栈,放的东西是一个结构体(数字或字符)待会考虑如何用    stack1 wait;    screenC screening;    num_strT temp2;    char s[1005],*p;    int temp,braces=0,count=0;    //读入字符串,如果是数字,读入screenINg,    //如果是字符+- ,把所有都放入screen,如果是*/直接进waiting,无视空格,如果是(入waiting,如果是),把直到(的放进screen    cin.getline(s,1005);    p=&s[strlen(s)-1];    while(*p)    {        if(*p>='0'&&*p<='9')        {            temp=0;            count=0;            while(*p>='0'&&*p<='9')            {                temp=temp+(*p-'0')*pow(10,count);                p--;                count++;            }            temp2.flag=0;            temp2.num=temp;            screening.push(temp2);        }        else if(*p=='+'||*p=='-')        {            //for(int i=0;i<wait.count;i++)cout<<wait.wait[i]<<" ";            //cout<<"empty is"<<wait.empty()<<"  braces are"<<braces;            while(!wait.empty())            {                if(wait.count<=0||wait.top()==')'||wait.top()=='-'||wait.top()=='+'){break;}                else{temp2.flag=1;temp2.str=wait.top();wait.pop();screening.push(temp2);}            }            wait.push(*p);            p--;        }        else if(*p=='*'||*p=='/'||*p==')')        {            wait.push(*p);            if(*p==')')braces+=1;            p--;        }        else if(*p=='(')        {            braces-=1;            while(wait.top()!=')')                  {                      temp2.flag=1;                      temp2.str=wait.top();                      wait.pop();                      screening.push(temp2);                  }            wait.pop();            p--;        }        else{p--;}    }    while(!wait.empty())    {        temp2.flag=1;        temp2.str=wait.top();        wait.pop();        screening.push(temp2);    }    screening.show();    stack_num calcu;    int qian,hou,ans;    for(int i=0;i<screening.count;i++)    {        if(screening.screen[i].flag==0){calcu.push(screening.screen[i].num);}        else        {            qian=calcu.top();            calcu.pop();            hou=calcu.top();            calcu.pop();            switch(screening.screen[i].str)            {                case '+':ans=qian+hou;break;                case '-':ans=qian-hou;break;                case '*':ans=qian*hou;break;                case '/':ans=qian/hou;break;            }            calcu.push(ans);        }    }    cout<<endl;     calcu.show();}

0 0
原创粉丝点击