凝檬v 2022-07-03 20:47 采纳率: 66.7%
浏览 42
已结题

使用堆栈计算中辍表达式

请问我这个在运行到主函数第一个if里的push函数时为什么会出错
补充:之后做了其他题,就在这代码上修改,但有关堆栈使用的函数没改,也同样运行不了


#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

struct num
{
    int size;
    int *top,*base;
};
char ch;

int f()
{
    int x=0;
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
        printf("%c\n",ch);
    }
    printf("%d\n",x);
    return x;
}

void build(struct num *s)
{
    s=(struct num*)malloc(sizeof(struct num));
    s->base=(int*)malloc(100*sizeof(int));
    if(!s->base)exit(-2);
    s->top=s->base;
    s->size=100;
    printf("1\n");
}

int pop(struct num *s)
{
    int data;
    s->top--;
    data=*(s->top);
    return data;
}

void push(int data,struct num *s)
{
    if ((s->top) - (s->base) >= (s->size))
    {
        s->base = (int *)realloc(s->base,(s->size + 100)*sizeof(int));
        if (!s->base) exit(-2);
        s->top = s->base + s->size;
        s->size += 100;
    }
    *(s->top)=data;
    (s->top)++;
    return;
}

int check(struct num *s)
{
    if(s->top==s->base)return 0;
    return 1;
}

int main()
{
    struct num *sn,*sp;
    build(sp);
    build(sn);
    while((ch=getchar())!='\n')
    {
        printf("%c\n",ch);
        if((ch>='0'&&ch<='9')||ch=='+'||ch=='-'||ch=='/'||ch=='*'||ch=='('||ch==')')
        {
            printf("%c\n",ch);
            if(ch>='0'&&ch<='9')
            {
                push(f(),sn);
                printf("%c\n",ch);
            }
            else if(!check(sp))
            {
                push((int)ch,sp);
            }
            else if(ch=='+'||ch=='-')
            {
                if(check(sp)&&(*(sp->top-1)=='+'||*(sp->top-1)=='-'||*(sp->top-1)=='*'||*(sp->top-1)=='/'))
                {
                    while(check(sp)&&*(sp->top-1)!=')')
                    {
                        char c;
                        c=pop(sp);
                        int x=pop(sn),y=pop(sn);
                        switch(c)
                        {
                        case '+':
                            y+=x;
                            break;
                        case '-':
                            y-=x;
                            break;
                        case '*':
                            y*=x;
                            break;
                        case '/':
                            y/=x;
                            break;
                        }
                        push(y,sn);
                    }
                }
                push((int)ch,sp);
            }
            else if(ch=='/'||ch=='*')
            {
                if(check(sp)&&(*(sp->top-1)=='*'||*(sp->top-1)=='/'))
                {
                    while(*(sp->top-1)=='*'||*(sp->top-1)=='/')
                    {
                        char c0;
                        c0=(char)pop(sp);
                        int x=pop(sn),y=pop(sn);
                        switch(c0)
                        {
                        case '+':
                            y+=x;
                            break;
                        case '-':
                            y-=x;
                            break;
                        case '*':
                            y*=x;
                            break;
                        case '/':
                            y/=x;
                            break;
                        }
                        push(y,sn);
                    }
                }
                push((int)ch,sp);
            }
            else if(ch==')')
            {
                while(*(sp->top-1)!='(')
                {
                    char c0;
                    c0=(int)pop(sp);
                    int x=pop(sn),y=pop(sn);
                    switch(c0)
                    {
                    case '+':
                        y+=x;
                        break;
                    case '-':
                        y-=x;
                        break;
                    case '*':
                        y*=x;
                        break;
                    case '/':
                        y/=x;
                        break;
                    }
                    push(y,sn);
                }
                pop(sp);
            }
            else
            {
                push((int)ch,sp);
            }
        }
    }
    while(check(sp))
    {
        char c0;
        c0=(int)pop(sp);
        int x=pop(sn),y=pop(sn);
        switch(c0)
        {
        case '+':
            y+=x;
            break;
        case '-':
            y-=x;
            break;
        case '*':
            y*=x;
            break;
        case '/':
            y/=x;
            break;
        }
        push(y,sn);
    }
    printf("%d",*(sn->base));
    return 0;
}


  • 写回答

2条回答 默认 最新

  • Hann Yang 全栈领域优质创作者 2022-07-03 21:27
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月18日
  • 已采纳回答 12月10日
  • 修改了问题 7月3日
  • 创建了问题 7月3日

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测