Question

This programming assignment needs to be written so that it can do infix expression to postfix...

This programming assignment needs to be written so that it can do infix expression to postfix expression (explained in the direction below). When the infix expression is translated into postfix, the parser needs to check if the operators and operands are accepted or rejected. It must look like the what is in the first page, asking the user to continue or end the translation.

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

Infix Expression: Expression of the form a op b. When an operator is in-between every pair of operands.

Postfix Expression: Expression of the form a b op. When an operator is followed for every pair of operands.

Algorithm:

1. Scan the infix expression from left to right.

2.If the scanned character is an operand,output it.

3. Else,

3.1 If the precedence of the scanned operator is greater than the precendence of the operator in the stack contains a'('),push it.

3.2 Else pop all the operators from the stack which are greater than or equal to in precedence than that of the scanned operator. After doing the push the scanned operator of the stack.

4.If the scanned character is an '(' ,push it to the stack.

5. If the scanned character is an ')' , pop the stack and output it until a '(' is encountered,and discard both the parenthesis.

6. Repeat steps 2-6 until infix expression is scanned.

7. Print the output.

8. Pop and output from the stack until it is not empty.

Program in C++:

#include<bits/stdc++.h>

using namespace std;

//Function to return precedence of operators

int prec(char c)

{

    if(c == '^')

    return 3;

    else if(c == '*' || c == '/')

    return 2;

    else if(c == '+' || c == '-')

    return 1;

    else

    return -1;

}

  

// The main function to convert infix expression

//to postfix expression

void infixToPostfix(string s)

{

    std::stack<char> st;

    st.push('N');

    int l = s.length();

    string ns;

    for(int i = 0; i < l; i++)

    {

        // If the scanned character is an operand, add it to output string.

        if((s[i] >= 'a' && s[i] <= 'z')||(s[i] >= 'A' && s[i] <= 'Z'))

        ns+=s[i];

  

        // If the scanned character is an ‘(‘, push it to the stack.

        else if(s[i] == '(')

          

        st.push('(');

          

        // If the scanned character is an ‘)’, pop and to output string from the stack

        // until an ‘(‘ is encountered.

        else if(s[i] == ')')

        {

            while(st.top() != 'N' && st.top() != '(')

            {

                char c = st.top();

                st.pop();

               ns += c;

            }

            if(st.top() == '(')

            {

                char c = st.top();

                st.pop();

            }

        }

          

        //If an operator is scanned

        else{

            while(st.top() != 'N' && prec(s[i]) <= prec(st.top()))

            {

                char c = st.top();

                st.pop();

                ns += c;

            }

            st.push(s[i]);

        }

  

    }

    //Pop all the remaining elements from the stack

    while(st.top() != 'N')

    {

        char c = st.top();

        st.pop();

        ns += c;

    }

      

    cout << ns << endl;

  

}

  

//Driver program to test above functions

int main()

{

    string exp = "a+b*(c^d-e)^(f+g*h)-i";

    infixToPostfix(exp);

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
This programming assignment needs to be written so that it can do infix expression to postfix...
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
  • Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expressi...

    Programming Assignment 2 – RPN Calculator – Infix to Postfix Conversion and The Evaluations of the Postfix Expression. You are to design and implement and algorithm in Java, to input an Infix expression , convert to a postfix expression and finally evaluate the postfix expression… Follow the examples done during class lectures… We are used to infix notation - ”3 + 4” - where the operator is between the operands. There is also prefix notation, where the operand comes before...

  • Develop an Expression Manager that can do the following operations: Balanced Symbols Check • Read a...

    Develop an Expression Manager that can do the following operations: Balanced Symbols Check • Read a mathematical expression from the user. • Check and report whether the expression is balanced. • {, }, (, ), [, ] are the only symbols considered for the check. All other characters can be ignored. Infix to Postfix Conversion • Read an infix expression from the user. • Perform the Balanced Parentheses Check on the expression read. • If the expression fails the Balanced...

  • You are to write a program name expressionTree.java that evaluates an infix expression entered by the...

    You are to write a program name expressionTree.java that evaluates an infix expression entered by the user. The expression may contain the following tokens: (1) Integer constants (a series of decimal digits). (2)   One alphabetic character - "x" (representing a value to be supplied later). (3)   Binary operators (+, -, *, / and % (modulo)). (4)   Parentheses          You will parse the input expression creating an expression tree with the tokens, then use the postOrder tree traversal algorithm to extract...

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

  • This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming...

    This project is designed to practice with OOP, stack data structure, its applications, and C++/Java programming language. You will write a program that reads an infix expression, converts it to a postfix expression, evaluates the postfix expression, and prints out the answer. You must define and implement your own Stack class and a Calculator class. Your Stack class supports standard basic stack operations and you can implement it with an array or a linked list. You should create a class...

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

  • (In Java) you will be creating a GUI for your calculator that evaluates the postfix expression. I...

    (In Java) you will be creating a GUI for your calculator that evaluates the postfix expression. I have already created the postfix class that converts the expression from infix to postfix. Here's the postFix class and its stack class (https://paste.ofcode.org/bkwPyCMEBASXQL4pR2ym43) ---> PostFix class (https://paste.ofcode.org/WsEHHugXB38aziWRrp829n)--------> Stack class Your calculator should have: Text field for the postfix expression Text field for the result Two buttons: Evaluate and Clear You can start with the code written below and improvise to work with this...

  • For this project you will implement a simple calculator. Your calculator is going to parse infix...

    For this project you will implement a simple calculator. Your calculator is going to parse infix algebraic expressions, create the corresponding postfix expressions and then evaluate the postfix expressions. The operators it recognizes are: +, -, * and /. The operands are integers. Your program will either evaluate individual expressions or read from an input file that contains a sequence of infix expressions (one expression per line). When reading from an input file, the output will consist of two files:...

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

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

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