analywize 2023-10-16 21:34 采纳率: 0%
浏览 10

中缀表达式转后缀表达式并求值栈方法

下面是代码,还请指正。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>

typedef char element;
typedef int num;
#define error -1

struct snode{
    element *data;
    int top;
    int maxsize; //堆栈最大容量
};
struct tnode{
    num *data;
    int top;
    int maxsize;
};

typedef struct snode *sqstack;
typedef sqstack stackchar;

typedef struct tnode *sqstack1;
typedef sqstack1 stacknum;


char *s_gets(char *st,int n);

//首先初始化,然后满,空检查,然后装,出,读栈顶

stackchar initialize_char(int maxsize)
{
    stackchar s = (stackchar)malloc(sizeof(struct snode));
    s->data = (element *)malloc(maxsize * sizeof(element));
    s->top = -1;
    s->maxsize = maxsize;
    return s;
}

stacknum initialize_num(int maxsize)
{
    stacknum s = (stacknum)malloc(sizeof(struct tnode));
    s->data = (num *)malloc(maxsize * sizeof(num));
    s->top = -1;
    s->maxsize = maxsize;
    return s;
}

bool stackempty(sqstack s)
{
    if(s->top == -1)
        return true;
    else
        return false;
}

bool pushchar(stackchar s,element x)
{
    if(s->top == s->maxsize - 1)
    {
        printf("堆栈满pushchar");
        return false;
    }
    s->data[++s->top] = x;
    return true;
}

element popchar(stackchar s)
{
    if(stackempty(s))
    {
        printf("堆栈空popchar");
        return error;
    }
    return (s->data[(s->top)--]);
}

element gettopchar(stackchar s)
{
    if(stackempty(s))
    {
        printf("栈空gettopchar");
        return error;
    }
    return (s->data[s->top]);
}

bool pushnum(stacknum p,num e)
{
    if(p->top == p->maxsize - 1);
    {
        printf("栈满!pushnum");
        return false;
    }
    p->data[++p->top] = e;
    return true;
}

num popnum(stacknum p)
{
    if(p->top == -1)
    {
        printf("栈空popnum");
        return error;
    }
    else
        return p->data[(p->top)--];
}

num gettopnum(stacknum p)
{
    if(p->top == -1)
    {
        printf("栈空gettopnum");
        return error;
    }
    return p->data[p->top];
}

void fun(stacknum p,char e)
{
    num temp1,temp2;
    temp1 = popnum(p);
    temp2 = popnum(p);
    switch(e)
    {
        case '+':
            pushnum(p,temp1 + temp2);
            break;
        case '-':
            pushnum(p,temp1 - temp2);
            break;
        case '*':
            pushnum(p,temp1 * temp2);
            break;
        case '/':
            if(temp2 != 0.0)
                pushnum(p,(double)temp1 / temp2);
            else
            {
                printf("错误:除法分母位0;\n");
            }
            break;
        default:
            printf("符号输入错误!");
    }
}

char readstack1(stackchar s)
{
    while(s->top != -1)
        printf("%c",s->data[s->top--]);
}

void readstack2(stacknum s)
{
    while(s->top != -1)
        printf("%d",s->data[s->top--]);
}

int main()
{
    int t;
    int i;//循环变量
    printf("请输入maxsize:");
    scanf("%d",&t);
    stackchar s = initialize_char(t);
    stacknum q = initialize_num(t);

    char infix[t];
    int temp;
    char oper;//临时算子
    fflush(stdin);
    //gets(infix);
    /*for(int i = 0;i < t;i++)
        printf("%c",infix[i]);*/ //检查infix问题,发现infix无法读入字符

    //开始遍历
for(;;)
{
printf("请输入中缀表达式:");
//s_gets(infix,t);
gets(infix);
    for(i = 0;infix[i] != '\0';i++)
    {
        if(infix[i] >= '0' && infix[i] <= '9')
        {
            temp = infix[i] - '0';//char转int
            while(infix[i+1] != '\0')
            {
                if(infix[i+1] >= '0' && infix[i+1] <= '9')
                {
                    temp = temp * 10 + infix[i+1] - '0';//多位数字转换
                    i++;
                }
                else
                    break;
            }
            pushnum(q,temp);//数字压栈?这里一直报错
        }
        else if(infix[i] == '+' || infix[i] == '-' || infix[i] == '*' || infix[i] == '/')
        {
            switch(infix[i])
            {
                case '+':
                    if(s->data[s->top - 1] != '+' && s->data[s->top - 1] != '-' && s->data[s->top - 1] != '*' && s->data[s->top - 1] != '/')//检查前面的算子与这个算子优先度
                        pushchar(s,'+');
                    else
                    {
                        while(s->top > -1 && s->data[s->top - 1] != '(')//检查括号
                        {
                            oper = popchar(s);
                            fun(q,oper);
                        }
                        pushchar(s,'+');
                    };
                    break;
                case '-':
                    if(s->data[s->top - 1] != '+' && s->data[s->top - 1] != '-' && s->data[s->top - 1] != '*' && s->data[s->top - 1] != '/')
                        pushchar(s,'-');
                    else
                    {
                        while(s->top > -1 && s->data[s->top - 1] != '(')
                        {
                            oper = popchar(s);
                            fun(q,oper);
                        }
                        pushchar(s,'-');
                    };
                    break;
                case '*':
                    if(s->data[s->top - 1] != '*' && s->data[s->top - 1] != '/')
                        pushchar(s,'*');
                    else
                    {
                        while(s->top > -1 && s->data[s->top - 1] != '(')
                        {
                            oper = popchar(s);
                            fun(q,oper);
                        }
                        pushchar(s,'*');
                    };
                    break;
                case '/':
                    if(s->data[s->top - 1] != '*' && s->data[s->top - 1] != '/')
                        pushchar(s,'/');
                    else
                    {
                        while(s->top > -1 && s->data[s->top - 1] != '(')
                        {
                            oper = popchar(s);
                            fun(q,oper);
                        }
                        pushchar(s,'/');
                    };
                    break;
                case '(':
                    pushchar(s,'(');
                    break;
                case ')'://当出栈信号
                    while(s->data[s->top - 1] != '(')
                    {
                        oper = popchar(s);
                        fun(q,oper);
                    }
                    oper = popchar(s);//如果不行,直接出),不管
                default:
                    printf("error!");
            }
        }
    }
    while(s->top > -1)
    {
        oper = popchar(s);
        fun(q,oper);
    }
    printf("\t\t%s = %.2f",infix,gettopnum(q));
    printf("\n");
    system("pause");
}
}

//处理输入
char *s_gets(char *st,int n)
{
    char *ret_val;
    char *find;

    ret_val = fgets(st,n,stdin);
    if(ret_val)
    {
        find = strchr(st,'\n');
        if(find)
            *find = '\0';
        else
            while(getchar() != '\n')
                continue;
    }

    return ret_val;
}

Q:
1.哪怕输入字符,比如2+5,但infix没有字符,只有2与5,改成gets(),问题没有解决
2.栈的top,maxsize没有问题,但是pushnum一直报错,式子也没有结果,初步怀疑是data里面的问题,但是不知道怎么解决(这里在初始函数加入pushnum,popnum之类的是为了辨别)

img

简介:
在了解并尝试写出中缀表达式转后缀表达式并求值的程序之时,我看到了下面这位 超级小白龙 的代码,试图将栈转化成给定容量分配的栈,并且允许用户输入一个生成栈的最大值,然而却遇到了诸多问题,弄了一晚不知道如何改善。

img

后记:
解决了
1.top指针的问题,不能减少
2.分号问题
3.一些表述问题,比如分母为0的情况再处理

  • 写回答

2条回答 默认 最新

  • 无医. 2023-10-16 22:30
    关注

    过几天看我博客,我写个这个代码发出去你看下

    评论

报告相同问题?

问题事件

  • 修改了问题 10月19日
  • 创建了问题 10月16日

悬赏问题

  • ¥100 求懂行的大ge给小di解答下!
  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多
  • ¥15 设计一个温度闭环控制系统