Zijeak
2018-11-28 13:43
采纳率: 0%
浏览 565
已采纳

【数据结构】求大佬帮忙看一下这个错误是怎么回事?(用栈实现带括号的多位数四则运算器)

题目原型: 四则运算器:实现多位整数的带括号的四则运算

现存问题:

程序中出现如图所示错误,麻烦大佬指点一下原因

图片说明

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

#define TRUE 1
#define FALSE 0
#define Stack_Size 50

typedef char StackElementType; //运算符 
typedef double StackElementType_num; //运算数 
typedef struct
{
    StackElementType elem[Stack_Size];
    int top;
}SeqStack; //运算符栈 
typedef struct
{
    StackElementType_num elem[Stack_Size];
    int top;
}SeqStack_num; //运算数栈 

StackElementType_num ExpEvaluation();

void InitStack(SeqStack *S);
int Push(SeqStack *S,StackElementType x);
int Push_num(SeqStack *S,StackElementType_num x);

int Pop(SeqStack *S,StackElementType *x);
int Pop_num(SeqStack *S,StackElementType_num *x);
int GetTop(SeqStack *S,StackElementType *x);
int GetTop_num(SeqStack *S,StackElementType_num *x);
StackElementType Compare(StackElementType ch1,StackElementType ch2);
StackElementType Execute(StackElementType_num a,StackElementType op,StackElementType_num b);

int main()
{
    printf("%lf",ExpEvaluation()); 
    return(0);
}
StackElementType_num ExpEvaluation()
{
    char exp[100];//用字符数组将运算表达式存起来 
    int i=0,count=0;
    printf("Please input an expression (Ending with #:)");
    exp[i]=getchar();
    while(exp[i]!='#')
    {
        i++;count++; 
        exp[i]=getchar();
    }
    SeqStack OPTR;
    SeqStack_num OVS;
    StackElementType ch,op,x;
    StackElementType_num a,b,v;
    InitStack(&OPTR);
    InitStack_num(&OVS);
    Push(&OPTR,'#');
    for(i=0;i<count;i++)
    {
        ch=exp[i];
        while(ch!='#'||GetTop(&OPTR,&x)!='#')
        {
          if(ch>='0'&&ch<='9')//不是操作符,是操作数,进OVS栈 
          {
            int temp;
            temp=ch-'0';//先把当前操作数从字符变为数字 
            i++;
            ch=exp[i];
            while(ch>='0'&&ch<='9')//继续判断下一位是否为操作数
            {
                temp=temp*10+ch-'0';
                i++;
                ch=exp[i];
            } 
            Push_num(&OVS,temp);//压栈 

          }
          else
          switch(Compare(ch,GetTop(&OPTR,&x)))
          {
            case '>':Push(&OPTR,ch);
              i++;ch=exp[i];
            case '=':Pop(&OPTR,&op);i++;ch=exp[i];break;//脱括号 
            case '<':Pop(&OPTR,&op);//形成运算 
            Pop_num(&OVS,&b);
            Pop_num(&OVS,&a);
            v=Execute(a,op,b);
            Push_num(&OVS,v);
            break; 
          }
        }
    }

    v=GetTop_num(&OVS,&x);
    return(v);
} 

//初始化
void InitStack(SeqStack *S)
{
    /*构造一个空栈S*/
    S->top=-1; 
} 
void InitStack_num(SeqStack_num *S)
{
    /*构造一个空栈S*/
    S->top=-1; 
} 
//进栈
int Push(SeqStack *S,StackElementType x)
{
    if(S->top==Stack_Size-1)
     return(FALSE);
    S->top++;
    S->elem[S->top]=x;
    return(TRUE);
} 
int Push_num(SeqStack *S,StackElementType_num x)
{
    if(S->top==Stack_Size-1)
     return(FALSE);
    S->top++;
    S->elem[S->top]=x;
    return(TRUE);
} 

//出栈
int Pop(SeqStack *S,StackElementType *x)
{
    if(S->top==-1)
     return(FALSE);
    *x=S->elem[S->top];
    S->top--;
    return(TRUE);
} 
int Pop_num(SeqStack *S,StackElementType_num *x)
{
    if(S->top==-1)
     return(FALSE);
    *x=S->elem[S->top];
    S->top--;
    return(TRUE);
} 

//读栈顶
int GetTop(SeqStack *S,StackElementType *x)
{
    if(S->top==-1)
     return(FALSE);
    else
    {
        *x=S->elem[S->top];
        return(TRUE);
    }
} 

int GetTop_num(SeqStack *S,StackElementType_num *x)
{
    if(S->top==-1)
     return(FALSE);
    else
    {
        *x=S->elem[S->top];
        return(TRUE);
    }
} 


//比较优先级,返回'>','<','=' 
StackElementType Compare(StackElementType ch1,StackElementType ch2)
//ch1为还未进栈的运算符,ch2为当前运算符栈顶元素 
{
    //'('未入栈时,优先级最高,入栈后,优先级最低
    int m,n; 
    switch(ch1)
    {
        case '(':m=6;break;
        case '*':
        case '/':m=5;break;
        case '+':
        case '-':m=4;break;
        case ')':m=3;break;

    }
    switch(ch2)
    {
        case '*':
        case '/':n=6;break;
        case '+':
        case '-':n=5;break;
        case ')':n=4;break;
        case '(':n=3;break;

    }
    if(ch1>ch2)
     return('>');
     else if(ch1=ch2)
      return('=');
      else
       return('<');
}
StackElementType_num Execute(StackElementType_num a,StackElementType op,StackElementType_num b)
{
    StackElementType_num v;
    switch(op)
    {
        case '+':v=a+b;break;
        case '-':v=a-b;break;
        case '*':v=a*b;break;
        case '/':v=a/b;break;
    }
    return(v);
}

1条回答 默认 最新

相关推荐 更多相似问题