Ask C/C++ Expert


Home >> C/C++

Write a program that takes as input an infix expression and outputs the equivalent post-fix expression. The basic algorithm is con-

tained in "Translating Infix to Postfix Notation" on page 379. Assume that the input may contain numbers; variables; arithmetic operations +, -, *, and /; as well as parentheses. However, the expression need not be fully parenthesized, and when parentheses are missing, the usual C++ precedence rules are used to determine the order of evaluation. Your program should allow the user to enter additional ex-

pressions until the user says he or she wishes to end the program. For a more difficult assignment, enhance your program so that the expression need not be well formed; if it is not well formed, then the user is asked to reenter the expression.

 

Pseudocode (evaluating a post-script expression)

1. Initialize a stack of double numbers.

2. do

if (the next input is a number)

Read the next input and push it onto the stack.

else

{

Read the next character, which is an operation symbol.

Use top and pop to get the two numbers off the stack.

Combine these two numbers with the operation (using the second number

popped as the left operand), and push the result onto the stack.

}

while (there is more of the expression to read);

3. At this point, the stack contains one number, which is the value of the expression.

 

Pseudocode (Converting a Fully Parenthesized Infix Expression to a Postfix Expression)

do

if (the next input is a left parenthesis)

Read the left parenthesis and push it onto the stack.

else if (the next input is a number or other operand)

Read the operand and write it to the output.

else if (the next input is one of the operation symbols)

Read the operation symbol and push it onto the stack.

else

{

Read and discard the next input symbol (which should be a right parenthesis).

There should be an operation symbol on top of the stack, so write this symbol

to the output and pop it from the stack. (If there is no such symbol, then print an

error message indicating that there were too few operations in the infix expression,

and halt.) After popping the operation symbol, there should be a left parenthesis on

the top of the stack, so pop and discard this left parenthesis.

(If there was no left parenthesis, then the input did not have balanced

parentheses, so print an error message and halt.)

}

while (there is more of the expression to read);

 

 

Pseudocode(Converting an Infix Expression to a Postfix Expression (General Case))

1. Initialize a stack of characters to hold the operation symbols and parentheses.

2. do

if (the next input is a left parenthesis)

Read the left parenthesis and push it onto the stack.

else if (the next input is a number or other operand)

Read the operand and write it to the output.

else if (the next input is one of the operation symbols)

{

do

Print the top operation and pop it.

while none of these three conditions are true:

(1)The stack becomes empty, or

(2)The next symbol on the stack is a left parenthesis, or

(3)The next symbol on the stack is an operation with lower

precedence than the next input symbol.

Read the next input symbol, and push this symbol onto the stack

}

else

{

Read and discard the next input symbol (which should be a right parenthesis).

Print the top operation and pop it; keep printing and popping until the next

symbol on the stack is a left parenthesis. (If no left parenthesis is encountered, then

print an error message indicating unbalanced parentheses, and halt.) Finally, pop

the left parenthesis.

}

while (there is more of the expression to read);

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M9458211

Have any Question?


Related Questions in C/C++

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As