Question

In C language please: Having read a str infix such as: (7 - 3) / (2...

In C language please:

Having read a str infix such as: (7 - 3) / (2 + 2)

Convert from infix to postfix: 7 3 - 2 2 + /

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


ANSWER:-

#include <stdio.h>
#include <ctype.h>
#define max 10
char s[100];
int top=-1;
void push(char);
char pop();
int precede(char);
main(){
   char infix[100],postfix[100],ch;
   int i=0,j=0;
   // Read infix expression from user
   printf("Enter infix expression:");
   scanf("%s",infix);
   // evaluate each char in infix expression
   while((ch=infix[i++])!='\0'){
       // if it is number, add it to postfix expression
       if(isalnum(ch)){
           postfix[j++]=ch;
       }else if(ch=='('){
           // if it open paranthesis, push it to stack
           push(ch);
       }else if(ch==')'){
           // if it is closing paranthesis, pop all the
           // numbers from stack and add it postfix
           while(s[top]!='('){
               postfix[j++]=pop();
           }
           top--;
       }else{
           /*
               if if is operator, if stack is empty or precedence of current operator is
               greater than precedence of top of the stack, then push it into stack.
               otherwise pop all the operators from stack until precedence of top of the stack is
               less than precedence of the current operator.
           */
           if(top==-1||precede(ch)>precede(s[top])){
               push(ch);
           }else{
               while(precede(ch)<=precede(s[top])&&top!=-1){
                   postfix[j++]=pop();
               }
               push(ch);
           }
       }
   }
   while(top!=-1){
       postfix[j++]=pop();
   }
   postfix[j]='\0';
   printf("Infix expression = %s\nPostfix expression = %s",infix,postfix);
}
void push(char a)
{
   s[++top]=a;
}
char pop(){
   return s[top--];
}
int precede(char a){
   if(a=='('||a==')'){
       return 1;
   }else if(a=='-'||a=='+'){
       return 2;
   }else if(a=='%'||a=='/'||a=='*'){
       return 3;
   }
}

OUTPUT:-

NOTE:- If you have any doubts, please comment below. Please give positive rating. THANK YOU!!!

Add a comment
Know the answer?
Add Answer to:
In C language please: Having read a str infix such as: (7 - 3) / (2...
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
  • By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert...

    By using PYTHON language Postfix to Infix using Stack Develop a stack application that can convert Postfix notation to Infix notation using the following algorithm. In your stack application, you can use only two stacks, one for a stack that can store Postfix notation, and the other is a stack to store infix notation. Also, it would help if you had a function to distinguish between an operation or an operand. Input A B C * + D E /...

  • 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...

  • 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...

  • Write a program in c++ to convert an expression written in infix notation to be converted...

    Write a program in c++ to convert an expression written in infix notation to be converted to postfix notation. The program must do the following: a. Read a string of characters representing an expression in infix notation. The '$' is to be added at the end of the string to mark its ending. Each character is a letter, digit, +,-,*, or /. If a character is any other character an error must be signaled and the program is terminated b....

  • 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...

  • write a program in c++ Read an infix expression from an input file and convert to...

    write a program in c++ Read an infix expression from an input file and convert to postfix. Instead of displaying directly on the screen, first place in a queue, and then display the contents of the queue on the screen. Precondition: The expression will be read from a file (input.txt) that contains a single line. There will be no spaces between the operands and the operators. The following operators are allowed: ( ) + - * / The normal rules...

  • Write a program to convert an expression written in infix notation to be converted to postfix...

    Write a program to convert an expression written in infix notation to be converted to postfix notation. The program must do the following: a. Read a string of characters representing an expression in infix notation. The '$' is to be added at the end of the string to mark its ending. Each character is a letter, digit, +,-,*, or /. If a character is any other character an error must be signaled and the program is terminated b. Use stacks...

  • Write a java program to convert and print an infix expression to postfix expression. You can...

    Write a java program to convert and print an infix expression to postfix expression. You can use Java stack methods. (Must read input from System.in) Your main method should be as follow: public static void main(String args[]) { intopost p = new intopost (); String iexp, pexp; //infix postfix expression             try{ Scanner inf = new Scanner (System.in);                     // Read input from KB/ File while(inf.hasNext()){ // read next infix expression                                 iexp = inf.next(); // Assume method name to convert infix...

  • C++ Write a program that takes an infix expression as an input and produces a postfix...

    C++ Write a program that takes an infix expression as an input and produces a postfix expression. Use stack to convert an infix expression into postfix expression. Include a function that evaluates a postfix expression.

  • implement a class of infix calculators (Using C++). Consider a simple infix expression that consist of...

    implement a class of infix calculators (Using C++). Consider a simple infix expression that consist of single digit operands; the operators +, -, *, and / ; and parentheses. Assume that unary operators are illegal and that the expression contains no embedded spaces. Design and implement a class of infix calculators. Use the algorithms given in the chapter 6 to evaluate infix expressions as entered into the calculator. You must first convert the infix expression to postfix form and then...

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