凯H 2023-04-11 21:30 采纳率: 81%
浏览 72
已结题

这串代码求后缀表达式时存在什么错误


void Suffix()
{
    char input[STACKMAX];        
    char str[STACKMAX];        
    char result[STACKMAX];        
    int i = 0, j, sum;
    int top_str = 0, top_res;
    char ch;
    getchar();
    printf("请输入中缀表达式,以'#'结束:");
    do
    {
        i++;
        scanf("%c", &input[i]);
    } while ('#' != input[i] && i != STACKMAX);
    sum = i;
    top_res = 1;
    i = 1;
    ch = input[i];
    while ('#' != ch)
    {
        switch (ch)
        {
            case '(':
                top_str++;
                str[top_str] = ch;        
                break;
            case ')':
                while ('(' != str[top_str])        
                {
                    result[top_res++] = str[top_str--];
                    result[top_res++] = ' ';
                }
                top_str--;
                break;
            case'+':
            case'-':
                while (0 != top_str && '(' != str[top_str])        
                {
                    result[top_res++] = str[top_str--];        
                    result[top_res++] = ' ';
                }
                str[++top_str] = ch;        
                break;
            case'*':
            case'/':
                while ('*' == str[top_str] || '/' == str[top_str])
                    while (0 != top_str && '(' != str[top_str])
                    {
                        result[top_res++] = str[top_str--];
                        result[top_res++] = ' ';
                    }
                str[++top_str] = ch;
                break;
            case' ':
                break;
            default:
                while (ch >= '0' && ch <= 'z')        
                {
                    result[top_res++] = ch;        
                    i++;
                    ch = input[i];
                }
                i--;
                result[top_res++] = ' ';    
        }
        i++;
        ch = input[i];
    }
    while (0 != top_str)
    {
        result[top_res++] = str[top_str--];
        if (top_str != 0)
            result[top_res++] = ' ';
    }
    printf("输入的中缀表达式为:");
    for (j = 1; j < sum; j++)
        printf("%c", input[j]);
    printf("转化为后缀表达式为:");
    for (j = 1; j < top_res; j++)
        printf("%c", result[j]);
    printf("\n");
}

这串代码里面哪里有问题

img


使这个后缀表达式错误了
正确的形式是3 4 25 6 15 + - / 8 * +

  • 写回答

4条回答 默认 最新

  • 「已注销」 2023-04-11 21:37
    关注

    引用new bing部分回答作答:
    这个代码存在一个错误,即在处理运算符优先级的时候,乘法和除法的优先级没有被正确处理。具体地说,在遇到乘法或除法运算符时,应该判断栈顶的运算符是不是乘法或除法运算符,如果是,则将栈顶运算符弹出,加入到后缀表达式中,否则将当前运算符入栈。但是这个代码没有正确地处理这个逻辑,导致输出的后缀表达式不正确。
    以下是修正后的代码:

    #include <iostream>
    #include <string>
    #include <stdio.h>
    #include <stdlib.h>
    #define STACKMAX 1000
    void Suffix()
    {
        char input[STACKMAX];        
        char str[STACKMAX];        
        char result[STACKMAX];        
        int i = 0, j, sum;
        int top_str = 0, top_res;
        char ch;
        getchar();
        printf("请输入中缀表达式,以'#'结束:");
        do
        {
            i++;
            scanf_s("%c", &input[i]);
        } while ('#' != input[i] && i != STACKMAX);
        sum = i;
        top_res = 1;
        i = 1;
        ch = input[i];
        while ('#' != ch)
        {
            switch (ch)
            {
                case '(':
                    top_str++;
                    str[top_str] = ch;        
                    break;
                case ')':
                    while ('(' != str[top_str])        
                    {
                        result[top_res++] = str[top_str--];
                        result[top_res++] = ' ';
                    }
                    top_str--;
                    break;
                case'+':
                case'-':
                    while (0 != top_str && '(' != str[top_str])        
                    {
                        result[top_res++] = str[top_str--];        
                        result[top_res++] = ' ';
                    }
                    str[++top_str] = ch;        
                    break;
                case'*':
                case'/':
                    while (0 != top_str && '(' != str[top_str] && ('*' == str[top_str] || '/' == str[top_str]))        
                    {
                        result[top_res++] = str[top_str--];
                        result[top_res++] = ' ';
                    }
                    str[++top_str] = ch;        
                    break;
                case' ':
                    break;
                default:
                    while (ch >= '0' && ch <= '9')        
                    {
                        result[top_res++] = ch;        
                        i++;
                        ch = input[i];
                    }
                    i--;
                    result[top_res++] = ' ';    
            }
            i++;
            ch = input[i];
        }
        while (0 != top_str)
        {
            result[top_res++] = str[top_str--];
            if (top_str != 0)
                result[top_res++] = ' ';
        }
        printf("输入的中缀表达式为:");
        for (j = 1; j < sum; j++)
            printf("%c", input[j]);
        printf("转化为后缀表达式为:");
        for (j = 1; j < top_res; j++)
            printf("%c", result[j]);
        printf("\n");
    }
    int main()
    {
        Suffix();
        system("pause");
        return 0;
    }
    
    

    修正后的代码中,在遇到乘法或除法运算符时,增加了一个while循环来处理栈顶的运算符,确保只有栈顶为乘法或除法运算符时才会将其弹出。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 4月19日
  • 已采纳回答 4月11日
  • 创建了问题 4月11日

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助