Question

In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an...

In C programming Language

Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers

For Example: Infix expression (6 + 2) * 5 - 8 / 4 to a postfix expression is  62+5*84/-

The program should read the expression into character array infix and use the stack functions implemented in this chapter to help create the postfix expression in character array postfix. The algorithm for creating a postfix expression is as follows:

1 Push a left parenthesis '(' onto the stack.

2 Append a right parenthesis ')' to the end of infix.

3 While the stack is not empty, read infix from left to right and do the following:

  • If the current character in infix is a digit, copy it to the next element of postfix.
  • If the current character in infix is a left parenthesis, push it onto the stack.
  • If the current character in infix is an operator,
    • Pop operators (if there are any) at the top of the stack while they have equal or higher precedence than the current operator, and insert the popped operators in postfix.
    • Push the current character in infix onto the stack.
  • If the current character in infix is a right parenthesis
    • Pop operators from the top of the stack and insert them in postfix until a
      left parenthesis is at the top of the stack.
    • Pop (and discard) the left parenthesis from the stack.

-----------------------------------------------------------------------------------------------

The following arithmetic operations are allowed in an expression:
+ (addition) , - (subtraction), * (multiplication), / (division), ^ (exponentiation) and
   % (remainder).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

- Below is the C Programming code to convert from infix expression to postfix expression

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
int top = -1;
char stck[MAX];
///////////////////////////Implement Stack//////////////////////////////
void push(char ch){
if(top < MAX-1)
stck[++top] = ch;
else
return;
}
char pop(){
if(top > -1)
return stck[top--];
}
char Top(){
if(top > -1)
return stck[top];
}
int isEmpty(){
if(top==-1)
return 1;
else
return 0;
}
///////////////////////////End Stack////////////////////////////////////

///////////////////////////Infix to postfix Implementation//////////////
int prec(char symbol){
switch(symbol){
case '+':
case '-':
return 1;
break;
case '*':
case '/':
case '%':
return 2;
break;
case '^':
return 3;
break;
default:
return 0;
break;
}
}
void infixTopostfix(char ch[],int n){
int i,j=0;
char res[MAX];
for(i=0; i<n; i++){
if(ch[i]=='0'||ch[i]=='1'||ch[i]=='2'||ch[i]=='3'||ch[i]=='4'||ch[i]=='5'
||ch[i]=='6'||ch[i]=='7'||ch[i]=='8'||ch[i]=='9')
res[j++]=ch[i];
else if(ch[i]=='(')
push(ch[i]);
else if(ch[i]==')'){
while(Top()!='(' && !isEmpty()){
res[j++] = pop();
}
if(!isEmpty() && Top() != '(')
return;
else
pop();
}else{
while(!isEmpty() && prec(ch[i]) <= prec(Top()))
res[j++]=pop();
push(ch[i]);
}
}
while(!isEmpty())
res[j++]=pop();
for(i=0; i<j; i++)
printf("%c",res[i]);
}
///////////////////////////End Infix to postfix ////////////////////////
int main()
{
char ch[] = "(6+2)*5-8/4"; //"(1+2)*(3+4)" , "1+2*3+4" insert string without space
int size = sizeof(ch);
infixTopostfix(ch,size);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
In C programming Language Write a version of the infix-to-postfix conversion algorithm. Write a program that converts an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Stacks are used by compilers to help in the process of evaluating expressions and generating machine...

    Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation in which the operator is written to the right of its two operands. The preceding...

  • Code should be written in java Write a program to convert an infix expression to postfix...

    Code should be written in java Write a program to convert an infix expression to postfix expression using stack. Algorithm: a) Create a stack b) For each character t in the input stream If(t is an operand) append t to the output. Else if (t is a right parenthesis) Pop and append to the output until a left parenthesis is popped (but do not append this parenthesis to the output). Else if(t is an operator or left parenthesis) Pop and...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQ...

    Infix Expression Evaluator For this project, write a C program that will evaluate an infix expression. The algorithm REQUIRED for this program will use two stacks, an operator stack and a value stack. Both stacks MUST be implemented using a linked list. For this program, you are to write functions for the linked list stacks with the following names: int isEmpty (stack); void push (stack, data); data top (stack); void pop (stack); // return TRUE if the stack has no...

  • In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack...

    In c++ Section 1. Stack ADT – Overview  Data Items The data items in a stack are of generic DataType. This means use should use templating and your Node class. Structure  The stack data items are linearly ordered from the most recently added (the top) to the least recently added (the bottom). This is a LIFO scheme. Data items are inserted onto (pushed) and removed from (popped) the top of the stack.  Operations  Constructor. Creates an empty stack.  Copy constructor....

  • Write a java code to implement the infix to postfix algorithm as described below: Algorithm convertTo...

    Write a java code to implement the infix to postfix algorithm as described below: Algorithm convertTo Post fix ( infix) // Converts an infix expression to an equivalent postfix expression operatorStack = a new empty stack postfix = anew empty string while (infix has characters left to parse) nextCharacter =next nonblank character of infix switch (nextCharacter) { case variable: Append nextCharacter to postfix break case 'A' operatorStack.push (nextCharacter) break case '+ case '-' : case '*' : case '/' while...

  • Total point: 15 Introduction: For this assignment you have to write a c program that will...

    Total point: 15 Introduction: For this assignment you have to write a c program that will take an infix expression as input and display the postfix expression of the input. After converting the postfix expression, the program should evaluate the expression from the postfix and display the result. What should you submit? Write all the code in a single file and upload the .c file. Problem We as humans write math expression in infix notation, e.g. 5 + 2 (the...

  • This code in C converts infix to postfix and evaluates it. The problem is that it...

    This code in C converts infix to postfix and evaluates it. The problem is that it only evaluates one digit expressions. I need to fix it so that it can evaluate 2 digits expressions as well. #include <stdio.h> #include <ctype.h> #include <string.h> #include <math.h> #define SIZE 100 char s[SIZE]; int top=-1; void infixToPostfix(char *infix, char *postfix); void postfixEvaluation(char *postfix); void push(char elem){ s[++top]=elem; } char pop(){ return(s[top--]); } int pr(char elem){ // Order of precedence switch (elem) { case '(':...

  • Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression...

    Help me to fix this code in C language. This code converts infix expressions to postfix and then evaluate the expression. Right now, it works with single digits. I need to modify it so that it can evaluate expressions with also 2 digits, example: (60+82)%72. Additionally I need to display an error when the parenthesis don't match like (89+8(. I have muted some line that would print the postfix expression with a space like: 22 8 + when the input...

  • We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are...

    We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT