简体   繁体   English

将表达式转换为后缀的程序

[英]program to convert expression to postfix

i am not getting correct output for this program getting as abcde-*++ for give input in main 我没有为该程序获取正确的输出,而在主程序中以abcde-* ++的形式输入

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

 struct Stack{
   int capacity;
   int top;
   int *array;
 };
 struct Stack* createstack(int capacity){
     struct Stack* stack=(struct Stack*)malloc(sizeof(struct Stack));
     stack->top=-1;
     stack->capacity=capacity;
     stack->array=(int*)malloc(sizeof(int)*capacity);
     return stack;
     }
 char pop(struct Stack* stack){
     return(stack->array[stack->top--]);
 }
 void push(struct Stack* stack,char ch ){

     stack->array[++stack->top]=ch;

}
 int isempty(struct Stack* stack){

    return(stack->top==-1);
}
int isfull(struct Stack* stack){
    return(stack->top==stack->capacity-1);
}
int isfront(struct Stack* stack){
    return(stack->array[stack->top]);
}




int precedence(char ch){
    switch(ch){
      case 1: ch=='+';
      case 2: ch=='-';
         return 1;
      case 3: ch=='*';
      case 4: ch=='/';
         return 2;
      case 5: ch=='^';
         return 3;

      return-1;
    }

}
int isoperand(char ch){

    return(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z');
}
void infixtopostfix(struct Stack* stack,char* exp){
       int i,k=-1;
       char res[100];
       for(i=0;exp[i];i++){
          ///if an operand is encountered
         if(isoperand(exp[i]))
            res[++k]=exp[i];
          ///if an operator is encountered
          else{
       // if(isempty(stack)||precedence(isfront(stack))<precedence(exp[i]))

        //else
            while(!isempty&&precedence(isfront(stack))>=precedence(exp[i]))
            res[++k]=pop(stack);
            push(stack,exp[i]);




         }
     }
    while(!isempty(stack))
        res[++k]=pop(stack);

        res[++k]='\0';
      printf("%s",res);

}
int main(){
struct Stack* stack=createstack(100);
char arr[100]="a+b+c*d-e";
infixtopostfix(stack,arr);
}

This program is to convert the expression from infix to postfix Here is the Algorithm 该程序将表达式从infix转换为postfix,这是算法

Algorithm 1. Scan the infix expression from left to right. 算法1.从左到右扫描中缀表达式。

  1. If the scanned character is an operand, output it. 如果扫描的字符是操作数,则将其输出。

  2. Else, 其他,

…..3.1 If the precedence of the scanned operator is greater than the precedence of the operator in the stack(or the stack is empty), push it. …..3.1如果已扫描操作符的优先级大于堆栈中操作符的优先级(或堆栈为空),则将其推入。 ….. ... ..

3.2 Else, Pop the operator from the stack until the precedence of the scanned operator is less-equal to the precedence of the operator residing on the top of the stack. 3.2否则,从堆栈中弹出运算符,直到被扫描的运算符的优先级不等于位于堆栈顶部的运算符的优先级。 Push the scanned operator to the stack. 将扫描的运算符推入堆栈。

  1. Repeat steps until infix expression is scanned. 重复步骤,直到扫描中缀表达式。
  2. Pop and output from the stack until it is not empty. 从堆栈弹出并输出,直到不为空。 i Am not getting correct output getting as abcde-*++ for the given input in my main function 我在我的主要功能中得到的输入不正确,输出为abcde-*++

I don't know if these are your only problems, but two things come to mind: 我不知道这些是否是您唯一的问题,但是我想到了两点:

As @rici pointed out, your precedence function doesn't work the way you think it should. 正如@rici指出的那样,您的precedence功能无法按照您认为的方式工作。 Correct would be: 正确的是:

int precedence(char ch){
    switch(ch){
      case '+':
      case '-':
         return 1;
      case '*':
      case '/':
         return 2;
      case '^':
         return 3;
      default: 
         return-1;
    }
}

When you're checking precedence, you have this conditional: 在检查优先级时,您具有以下条件:

while(!isempty&&precedence(isfront(stack))>=precedence(exp[i]))

That's never going to work because !isempty will always evaluate to false. 这永远都行不通,因为!isempty始终会评估为false。 You're asking here if the address of the isempty function is null. 您在这里询问isempty函数的地址是否为空。 It's not. 不是。 What you really want to do is check whether the stack is empty: 您真正想要做的是检查堆栈是否为空:

while(!isempty(stack) && precedence(isfront(stack))>=precedence(exp[i]))

That will call the isempty function. 那将调用isempty函数。

You really should learn to use your debugger. 您确实应该学习使用调试器。 Single-stepping your code would quickly reveal the errors I noted above. 单步执行代码将迅速揭示出我上面提到的错误。

A couple of notes on your stack implementation. 关于堆栈实现的几点说明。

You have a latent bug in pop . 您在pop有一个潜在的错误。 If somebody calls it when the stack is empty, it will either crash because you're trying to accessing array[-1] , or it will succeed in accessing array[-1] , and return a bogus value. 如果有人在堆栈为空时调用它,则它可能会因为您尝试访问array[-1]崩溃,或者将成功访问array[-1]并返回假值。 You're better off checking the value of top , and throwing an exception (or crashing the program with a message), than returning a bad value. 与返回错误的值相比,最好检查一下top的值并抛出异常(或使程序崩溃并显示一条消息)。 Depending on clients to call isEmpty before calling pop is unreliable. 依赖客户端在调用pop之前调用isEmpty是不可靠的。

You have a similar error in push . 您在push也有类似的错误。 Trying to access beyond the bounds of the array is undefined behavior. 尝试访问超出数组范围的行为是未定义的行为。 The program might continue working, and it might crash. 该程序可能会继续运行,并且可能会崩溃。 In the case of push , you might end up pushing a value that is subsequently changed by something else, and then pop returns a value that you didn't push. push的情况下,您可能最终会推送一个随后被其他更改的值,然后pop返回您未推送的值。

Because of the bug in pop , your isEmpty function also has a bug. 由于pop中的错误,您的isEmpty函数也存在一个错误。 If top ever gets decremented lower than -1 , isEmpty will return false . 如果top的值减小到小于-1 ,则isEmpty将返回false Rather than checking for == -1 , you should check for < 0 . 您应该检查< 0 ,而不是检查== -1 Even if you fix the pop problem, checking for < 0 is better. 即使您解决了pop问题,也最好检查< 0 Defense in depth. 纵深防御。

The name of the function that looks at the top of the stack without popping it is typically called peek , not isfront . 在堆栈顶部而不弹出的函数名称通常称为peek ,而不是isfront

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM