Dhairya Gupta
Dhairya Gupta

Reputation: 57

Infix to postfix in c

I am trying to convert a given infix expression to prefix expression in c. But i am encountering a problem in it. The value in postfix is not getting updated. Please help me understand what the issue is with my code.

I understood what the algorithm of infix to prefix is and tried to code it accordingly. But when i run the code i do not get errors but the code does not print the postfix expression in main. If i put something to print at the end of infixToPostfix, it doesn't print anything/.

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_LEN 100

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

int isOperator(char c){
    return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}

void infixToPostfix(char* infix,char* postfix){
    int i = 0, j = 0;
    int len = strlen(infix), top = -1;
    int isNextDig = 0;
    char stack[MAX_LEN];

    while(i < len){
        if(infix[i] = '\t' || infix[i] == ' '){
            i++;
            continue;
        }

        if (isalnum(infix[i])) {
            if(i+1 < len && isalnum(infix[i+1])){
                postfix[j++] = infix[i++];
                isNextDig = 1;
            }
            else{
                postfix[j++] = infix[i++];
                postfix[j++] = ' ';
            }
        }
        else if(infix[i] == '('){
            stack[++top] = infix[i++];
        }
        else if (infix[i] == ')') {
            while (top > -1 && stack[top] != '('){
                postfix[j++] = stack[top--];
                postfix[j++] = ' ';
            }if (top > -1 && stack[top] != '(')
                return;
            else
                top--;
        }

        else if (infix[i] == ')') {
            while (top > -1 && stack[top] != '(')
                postfix[j++] = stack[top--];
            if (top > -1 && stack[top] != '(')
                return;
            else
                top--;
        }

        else if(isOperator(infix[i])){

            if(top > -1 && precedence(infix[i]) > precedence(stack[top])){
                stack[++top] = infix[i++];
            }
            else{
                postfix[j++] = stack[top--];
                postfix[j++] = ' ';
            }
        }

    while (top > -1) {
        if (stack[top] == '(') {
            return;
        }
        postfix[j++] = stack[top--];
        postfix[j++] = ' ';
    }

    postfix[j] = '\0';
    printf("postfix %s\n", postfix);
}
    

}

int main(){
    char infix[MAX_LEN] = "a+b*(c^d-e)^(f+g*h)-i";
        char* postfix;
        infixToPostfix(infix, postfix);
        printf("The postfix is : %s\n", postfix);
        return 0;
}

Upvotes: 1

Views: 483

Answers (1)

Ashok Sharma
Ashok Sharma

Reputation: 33

There are many issues: pointer char* postfix is not malloced and isNextDig is not used

if(infix[i] = '\t' || infix[i] == ' '){

should be if(infix[i] == '\t' || infix[i] == ' '){

This is a bad code. Anyway this is what you might want:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LEN 100

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

int isOperator(char c){
    return (c == '+' || c == '-' || c == '*' || c == '/' || c == '^');
}

void infixToPostfix(char* infix,char* postfix){
    int i = 0, j = 0;
    int len = strlen(infix), top = -1;
    int isNextDig = 0;
    char stack[MAX_LEN];

    while(i < len){
        if(infix[i] == '\t' || infix[i] == ' '){
            i++;
            continue;
        }

        if (isalnum(infix[i])) {
            // if(i+1 < len && isalnum(infix[i+1])){
                postfix[j++] = infix[i++];
                // isNextDig = 1;
            // }
            // else{
            //     postfix[j++] = infix[i++];
            //     postfix[j++] = ' ';
            // }
        }
        else if(infix[i] == '('){
            stack[++top] = infix[i++];
        }
        else if (infix[i] == ')') {
            while (top > -1 && stack[top] != '('){
                postfix[j++] = stack[top--];
                postfix[j++] = ' ';
            }
            if (top > -1 && stack[top] != '(')
                return;
            else
                top--;
                i++;
        }

        else if(isOperator(infix[i])){

            while(top > -1 && precedence(infix[i]) <= precedence(stack[top])){
                postfix[j++] = stack[top--];
            }
            // else{
                stack[++top] = infix[i++];
                // postfix[j++] = ' ';
            // }
        }

    }
        while (top > -1) {
            if (stack[top] == '(') {
                return;
            }
            postfix[j++] = stack[top--];
            // postfix[j++] = ' ';
        }
    
    postfix[j] = '\0';
    printf("postfix %s\n", postfix);
//}
    

}

int main(){
    char infix[MAX_LEN] = "a+b*(c^d-e)^(f+g*h)-i";
        char* postfix = (char*)malloc(sizeof(char) * (strlen(infix)));
        infixToPostfix(infix, postfix);
        printf("The postfix is : %s\n", postfix);
        return 0;
}

Upvotes: 1

Related Questions