CSDN-Ada助手 2023-10-15 18:42 采纳率: 1.6%
浏览 11
已结题

这个程序为什么不能出结果_(语言-c语言)

该问题来自社区帖: https://bbs.csdn.net/topics/617411915.为符合问答规范, 该问题经过ChatGPT优化
以下是优化后的问题: ```html

这个程序为什么不能出结果_

#include <stdio.h>
#include <stdlib.h>
#define Stack_Size 50
#define true 1
#define false 0
typedef char Elemtype;
typedef struct
{
     Elemtype elem[Stack_Size];
     int top;
} SeqStack;
void InistStack(SeqStack *S)
{
     S->top=0;
}
void Push(SeqStack *S, Elemtype x)
{
     if(S->top==Stack_Size)
          return -1;
     S->elem[S->top]=x;
     S->top++;
}
void Pop(SeqStack *S, Elemtype *x)
{
     if(S->top==0) return -1;
     else
     {
          S->top--;
          *x=S->elem[S->top];
     }
}
char GetTop(SeqStack S,char *x)
{
     if(S.top==0) return false;
     else
     {
          *x=S.elem[S.top-1];
     }
}

char Precede(char a,char b)
{
     if(a=='#')
          if(b=='#')
               return '=';
          else
          {
               return '<';
          }
     if(a=='+'||a=='-')
          if(b=='+'||b=='-'||b==')'||b=='#')
               return '>';
          else
          {
               return '<';
          }
     if(a=='*'||a=='/')
          if(b=='(')
               return '<';
          else
          {
               return '>';
          }
 if(a=='(')
          if(b==')')
               return '=';
          else
          {
               return '<';
          }
if(a==')')
          return '>';

}

int Operate(int a,char theta,int b)
{
     int result;
     switch(theta)
     {
          case'+':return a+b;
          case'-':return a-b;
          case'*':return a*b;
          case'/':
               if(b!=0)
               return a/b;
               else
               {
                    printf("Divisor can not Be zero!\n");
                    exit(0);
               }
     }
}


main()
{
     SeqStack OPTR,OPND;
     InistStack(&OPTR);
     Push(&OPTR,'#');
     InistStack(&OPND);
     int a,b;
     char c=getchar();
     char x,y,theta;
     GetTop(OPTR,&x);
     while(c!='#'||x!='#')
     {
          if(c>='0'&&c<='9')
          {
               Push(&OPND,c);
               c=getchar();
          }
          else
          {
               switch(Precede(x,c))

            { case '<':
                    Push(&OPTR,c); c=get

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-10-15 20:58
    关注

    【以下回答由 GPT 生成】

    这个程序无法输出结果的原因是因为主函数main()缺少输出语句来显示计算结果。在运算结束后,应该添加一个输出语句来显示最终的计算结果。

    此外,程序还存在其他一些问题:

    1. Push()Pop()函数中,应该将返回类型改为void,因为这两个函数没有返回值。修改后的函数声明如下:
    void Push(SeqStack *S, Elemtype x);
    void Pop(SeqStack *S, Elemtype *x);
    
    1. GetTop()函数中,应该将返回类型改为int,表示是否成功获取栈顶元素。修改后的函数声明如下:
    int GetTop(SeqStack S, char *x);
    
    1. Operate()函数中,需要添加一个默认的返回语句,以处理未识别的操作符。

    修改后的代码如下所示:

    #include <stdio.h>
    #include <stdlib.h>
    #define Stack_Size 50
    
    typedef char Elemtype;
    
    typedef struct {
        Elemtype elem[Stack_Size];
        int top;
    } SeqStack;
    
    void InitStack(SeqStack *S) {
        S->top = 0;
    }
    
    void Push(SeqStack *S, Elemtype x) {
        if(S->top == Stack_Size) {
            return;
        }
        S->elem[S->top] = x;
        S->top++;
    }
    
    void Pop(SeqStack *S, Elemtype *x) {
        if(S->top == 0) {
            return;
        } else {
            S->top--;
            *x = S->elem[S->top];
        }
    }
    
    int GetTop(SeqStack S, char *x) {
        if(S.top == 0) {
            return 0;
        } else {
            *x = S.elem[S.top-1];
            return 1;
        }
    }
    
    char Precede(char a, char b) {
        if(a == '#') {
            if(b == '#') {
                return '=';
            } else {
                return '<';
            }
        }
    
        if(a == '+' || a == '-') {
            if(b == '+' || b == '-' || b == ')' || b == '#') {
                return '>';
            } else {
                return '<';
            }
        }
    
        if(a == '*' || a == '/') {
            if(b == '(') {
                return '<';
            } else {
                return '>';
            }
        }
    
        if(a == '(') {
            if(b == ')') {
                return '=';
            } else {
                return '<';
            }
        }
    
        if(a == ')') {
            return '>';
        }
    
        return ' '; // 添加该行来处理未识别的操作符
    }
    
    int Operate(int a, char theta, int b) {
        int result;
        switch(theta) {
            case '+': return a + b;
            case '-': return a - b;
            case '*': return a * b;
            case '/':
                if(b != 0) {
                    return a / b;
                } else {
                    printf("Divisor can not be zero!\n");
                    exit(0);
                }
            default: return 0; // 添加该行来处理未识别的操作符
        }
    }
    
    int main() {
        SeqStack OPTR, OPND;
        InitStack(&OPTR);
        Push(&OPTR, '#');
        InitStack(&OPND);
        int a, b;
        char c = getchar();
        char x, y, theta;
        GetTop(OPTR, &x);
    
        while(c != '#' || x != '#') {
            if(c >= '0' && c <= '9') {
                Push(&OPND, c);
                c = getchar();
            } else {
                switch(Precede(x, c)) {
                    case '<':
                        Push(&OPTR, c);
                        c = getchar();
                        break;
                    case '>':
                        Pop(&OPTR, &theta);
                        Pop(&OPND, &b);
                        Pop(&OPND, &a);
                        Push(&OPND, Operate(a, theta, b));
                        break;
                    case '=':
                        Pop(&OPTR, &theta);
                        c = getchar();
                        break;
                }
            }
            GetTop(OPTR, &x);
        }
    
        int result;
        Pop(&OPND, &result);
        printf("计算结果:%d\n", result); // 添加输出语句来显示计算结果
        return 0;
    }
    

    希望以上解决方案能够解决您的问题。如果您有任何疑问,请随时提出。


    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 系统已结题 10月23日
  • 创建了问题 10月15日