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

l2397182642 2023-10-15 17:46:12

#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=getchar();
                  break;
              case '=':
                  Pop(&OPTR,&x); c=getchar();
              case '>':
                  Pop(&OPTR,&theta);Pop(&OPND,&b);Pop(&OPND,&a);
                  Push(&OPND,Operate(a,theta,b));
                  break;
            }
        }
        GetTop(OPTR,&x);
    }
        GetTop(OPND,&y);
        printf("%d",y);


}
 

...全文
93 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
CSDN-Ada助手 2023-10-15
精选
  • 打赏
  • 举报
回复
您的问题已经帮您同步至问答, 链接: https://ask.csdn.net/questions/8012362, 请您保持关注, 如果回答有帮助解决此问题, 麻烦您动动小手给相关的回答点赞, Ada会在评论区为您更新结题状态

70,037

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧