首页 文章

使用堆栈的中缀到Postfix

提问于
浏览
2

我的讲师给了我一个创建程序的任务,使用Stack将中缀表达式转换为postfix . 我已经制作了堆栈类和一些函数来读取中缀表达式 .

但是这个名为inToPos(char string [])的函数正在创建一个断点,该函数负责将字符串inFix中的inFix表达式转换为使用堆栈的字符串postFix中的post fix表达式 . 你能帮助我,告诉我我做错了什么吗?

这些是我的代码,非常需要你的帮助.. :)

#include<stdio.h>
#include<stdlib.h>
#define MAX 15
#define true 1 
#define false 0 

typedef struct node* nodeptr;

typedef struct node{
    int data;
    nodeptr next;
}Node;

typedef struct{
    int count;
    nodeptr top;
}Stack;
typedef Stack* StackList;

StackList create();
void display(StackList list);
int isEmpty(StackList list);
void push(StackList list, int item);
void pop(StackList list);

int inToPos(char string[]);
int isOperator(char string[], int i);
int precedence(char x);

StackList create(){
StackList list;
list=(StackList)malloc(sizeof(Stack));
list->count=0;
list->top=NULL;
return list;
}

void display(StackList list){
    nodeptr ptr;
    ptr=list->top;
    while(ptr!=NULL){
        printf("%d ",ptr->data);
        ptr=ptr->next;
    }
    printf("\n");
}

int isEmpty(StackList list){
    return list->count==0;
    //return list->top==NULL;
}

void push(StackList list, int item){
    nodeptr temp;
    temp=(nodeptr)malloc(sizeof(Node));
    temp->data=item;
    temp->next=list->top;
    list->top=temp;
    (list->count)++;
}

void pop(StackList list){
    nodeptr temp;
    temp=list->top;
    list->top=temp->next;
    temp->next=NULL;
    free(temp);
    (list->count)--;
}

int inToPos(char string[]){
    int i,a=0;
    char postfix[MAX];
    StackList list=create();
    for(i=0;string[i]!='\0';i++){
        if(!isOperator(string,i)){
            postfix[a]=string[i];
            a++;
        }
        else if(isEmpty(list))
            push(list,string[i]);
        else{
            if(precedence(string[i])>precedence(list->top->data))
                push(list,string[i]);
            else{
                postfix[a]=list->top->data;
                a++;
                pop(list);
                if(!isEmpty(list)){
                    while(precedence(list->top->data)<=precedence(string[i])){
                        postfix[a]=list->top->data;
                        a++;
                        pop(list);
                    }
                }
                else
                    push(list,string[i]);
            }
        }
    }
    puts(postfix);
}


int isOperator(char string[], int i){
    switch(string[i])
    {
    case '+':
    case '-':
    case '*':
    case '%':
    case '/': return true;

    default: return false;
    }
}

int precedence(char x){
    switch(x)
    {
    case '%':
    case '*':
    case '/': return 2;
    case '+':
    case '-': return 1;

    default: return 0;
    }
}
int main(void){

    char string[MAX]="a+b*c-d";
    inToPos(string);
}

注意inToPos函数是使用此算法制作的:

  • 从左到右扫描中缀字符串 .

  • 初始化空堆栈 .

  • 如果扫描的字符是操作数,请将其添加到Postfix字符串 . 如果扫描的字符是操作符且堆栈为空则将字符推送到堆栈 .

  • 如果扫描的字符是操作符且堆栈不为空,则将字符的优先级与堆栈顶部的元素(topStack)进行比较 . 如果topStack优先于扫描的字符,则弹出堆栈,否则将扫描的字符推送到堆栈 . 只要stack不为空且topStack优先于字符,就重复此步骤 . 重复此步骤,直到扫描完字符 .

  • (扫描完所有字符后,我们必须添加堆栈可能具有的任何字符到Postfix字符串 . )如果stack不为空,请将topStack添加到Postfix字符串并弹出堆栈 . 只要堆栈不为空,重复此步骤 .

  • 返回后缀字符串 .

2 回答

  • 0

    你真的应该学习如何使用调试器,它是一个很好的工具,可以解决这些问题 . 但是,我跑了它并找出了你的问题:

    在这一行:

    while(precedence(list->top->data)<=precedence(string[i])){
    

    当您遍历列表时,您需要每次检查堆栈是否为空,而不仅仅是在进入循环之前 . 所以,做这样的事情:

    while(!isEmpty(list) && precedence(list->top->data)<=precedence(string[i])){
    

    代替 .

  • 2
    [Program to convert infix to postfix using stack][1]
    
    C program to convert infix to postfix using stackC
    
    #define SIZE 50            
    #include <ctype.h>
    char s[SIZE];
    int top=-1;       /* Global declarations */
    
    push(char elem)
    {                       /* Function for PUSH operation */
        s[++top]=elem;
    }
    
    char pop()
    {                      /* Function for POP operation */
        return(s[top--]);
    }
    
    int pr(char elem)
    {                 /* Function for precedence */
        switch(elem)
        {
        case '#': return 0;
        case '(': return 1;
        case '+':
        case '-': return 2;
        case '*':
        case '/': return 3;
        }
    }
    
    main()
    {                         /* Main Program */
        char infix[50],postfix[50],ch,elem;
        int i=0,k=0;
        printf("\n\nEnter Infix Expression : ");
        scanf("%s",infix);
        push('#');
        while( (ch=infix[i++]) != '\0')
        {
            if( ch == '(') push(ch);
            else
                if(isalnum(ch)) postfix[k++]=ch;
                else
                    if( ch == ')')
                    {
                        while( s[top] != '(')
                            postfix[k++]=pop();
                        elem=pop(); /* Remove ( */
                    }
                    else
                    {       /* Operator */
                        while( pr(s[top]) >= pr(ch) )
                            postfix[k++]=pop();
                        push(ch);
                    }
        }
        while( s[top] != '#')     /* Pop from stack till empty */
            postfix[k++]=pop();
        postfix[k]='\0';          /* Make postfix as valid string */
        printf("\nPostfix Expression =  %s\n",postfix);
    }
    

相关问题