Expressions until the user says

Assignment Help C/C++ Programming
Reference no: EM13168095

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);

Reference no: EM13168095

Questions Cloud

A firewall works by blocking incoming : A firewall works by blocking incoming requests. Explain how a firewall can distinguish between two incoming requests allowing one while denying the other??
What''s a more efficient way to write : What's a more efficient way to write this function for a matrix? This one has to go through all the numbers, but that would take too long. r_num is row and c_num is column
The stack parameters will not be const reference : In this second case, the stack parameters will not be const reference parameters because they are temporarily changed before being restored. As an alternative, the parameters could be value parameters (so that changing them does not affect the act..
Make a program to play lottery : make a program to play lottery. The program randomly generates a Lottery of a three-digit number( any number from 100 to 999), prompts the user to enter a three-digit number, and determines
Expressions until the user says : Expressions 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.
State and name the four general types of carbonyl reactions : name the four general types of carbonyl reactions. give an example of each including a detailed mechaism. use a propyl group next to a carbonyl in each example.
The program will read integers values : Write a C++ program to do the following. The program will read integers values one at a time from standard input (cin) until you read the value 150. assume that the number of values read in is at least 1 and less than or equal to 100
Create a function in c++ that subtracts two vectors : Create a function in c++ that subtracts two vectors. The function returns a vector than can also be negative.
Electrical energy can be used to separate water into oxygen : Electrical energy can be used to separate water into oxygen gas and hydrogen gas. In one such experiment,

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Before each sort, write psudo-code

Ceate a single cpp ?le (FILE=MAIN2.cpp) containing all three elementry sorts(bubble, insertion, selection). Before each sort, write psudo-code and invarient analysis of the sort in block comment style. Add the code from 3.). Generate a list of 100..

  To reverse the order of elements on a stack

Write a program in C++ to  reverse the order of elements on a stack S using two additional stacks using one additional stack

  Your city''s parking violation

Your city's Parking Violation Bureau wants you to write a program to compute fines for parking violations.

  Write a program that asks the user to enter the names of 3

Write a program that asks the user to enter the names of three salesmen. The program should thenaccept the sales produced for salesman for each quarter of the year. Display the name, and the totalsales amount, of each salesman.

  Write a function that computes length of a character string

Write a function that computes the length of a character string (i.e. write your own implementation of the strlen function.

  Write a program to read only one integer number

Write a program to read ONLY one integer number (your input must be one 3 digit number from 100 to 999), and to think of a number as being ABC (where A, B, and C are the 3 digits of a number)

  The initial data structure with information about a service

Implement a program using function for a defined task. Initial data is read from a file and added from keyboard. Program has to output initial and resulting data.The initial data structure with information about a service:

  Create class having constructor to recieve two ints

Create a class (in C++)named Card. The class should have two int data members named face and suit.The class should have a constructor that recieves the two ints and uses them to initialize the data members.

  Write a pseudocode statement

Assume that a program has two string variables named str1 and str2. Write a pseudocode statement that assigns an all uppercase version of str1 to the str2 variable. is str.add(str1,str2) part of it?

  Coinflip to prompt for and input a number

Write a program and name it CoinFlip to prompt for and input a number of times to flip a coin, then to output the number of "heads" and the number of "tails" that were flipped.

  First display the smallest volume and then the largest volum

first display the smallest volume and then the largest volume.

  Given a character code to indicate the type of customer

Pete's Plumbing of Pompey sells to both retail and wholesale buyers. The retail buyer must pay New York State and Onondaga County sales tax (8%). However, the wholesale buyer does not pay sales tax and receives a volume discount on the amount purchas..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd