中缀转前缀

19 篇文章 0 订阅
5 篇文章 0 订阅
#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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值