为什么我输出超限了关于中缀表达式转换后缀表达式的

#include<iostream>
#include<stdio.h>
#include<fstream>
#include<cstdlib>
using namespace std;
#define MAXSIZE 1000
typedef struct
{
 char *top;
 char *base;
 int stacksize;
} Sq;
Sq S1;
void InitStack(Sq &S1)
{
 S1.base = new char[MAXSIZE];
 if(!S1.base)
        exit(0);
 S1.top = S1.base;
 S1.stacksize = MAXSIZE;
}
void push(Sq &S1, char g)
{
 *S1.top++ = g;
}
char pop(Sq &S1)
{
 char g;
 g = *--S1.top;
 return g;
}
int main()
{
    int i;
    char ch[301];
    scanf("%s",ch);
    InitStack(S1);
    for(i=0;ch[i]!='\0';i++)
    {
        if(ch[i]!='+'&&ch[i]!='-'&&ch[i]!='*'&&ch[i]!='/'&&ch[i]!='('&&ch[i]!=')')
        {
            printf("%c",ch[i]);
        }
        else if(ch[i]=='('){push(S1,ch[i]);}
        else if(ch[i]==')')
        {
            while(*(S1.top-1)!='(') printf("%c",pop(S1));pop(S1);
        }
        else if(ch[i]=='+'||ch[i]=='-'||ch[i]=='*'||ch[i]=='/')
        {
            if(S1.top==S1.base) push(S1,ch[i]);
            else
            {
               if(ch[i]=='*'||ch[i]=='/')
               {
                   while(*(S1.top-1)!='+'&&*(S1.top-1)!='-'&&*(S1.top-1)=='('&&S1.top!=S1.base)
                   {
                       printf("%c",pop(S1));
                   }
                   push(S1,ch[i]);
               }
               else
               {
                   while(*(S1.top-1)!='('&&S1.top!=S1.base) printf("%c",pop(S1));
                   push(S1,ch[i]);
               }
            }
        }
    }
    while(S1.top!=S1.base) printf("%c",pop(S1));
    printf("\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值