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

  Write a method, to be included in a template unsorted list

Write a metho, to be included in a template unsorted list class, called replace_item, that will receive two xType parameters, one called olditem, the other called newitem.

  Write a function that finds area and volume of a cube

write a function that finds area and volume of a cube when length is input and then write a program to test it. here is what i have so far.

  Implemented a regular binary search tree

In Computer Science I, you implemented a regular binary search tree. Due to your busy schedule, you've decided that it would be best if you could simply reuse this code without any changes. You realize that your code would ONLY work for Arup's ass..

  Design a class named employeerecord

Design a class named EmployeeRecord that holds an employee's ID number, name, and payrate. Include mutator methods to set the values for each data field and output the values for each data field. Create the class diagram and write the code that

  Create program to input miles driven and gallons used

Create a program in C++ which will input miles driven and gallons used (both as integers) for each tankful. Program must compute and display the miles per gallon

  Two-dimensional array of double to hold the sales

Suppose you have a two-dimensional array of double to hold the sales amounts for all your salespeople three years. Assume you employ 50 salespeople and this array already holds valid data.

  Multidimensional arrays

multidimensional arrays to life, let's consider a specific example: How can you visualize a 4-dimensional array? How can you give meaning to each dimension this array has? What kind of application would such an array be useful for?

  A company hired 10 temporary workers

A company hired 10 temporary workers who are paid hourly and you are given a data file that contains the last name of the employees, the number of hours each employee worked in a week, and the hourly pay rate of each employee.

  Allows you to enter 10 phonecalls into an array

Create a main() function that allows you to enter 10 PhoneCalls into an array. If a PhoneCall is already been placed to a number, do not allow a second PhoneCall to the same number

  Design and implement a library system

Design and implement a library system that does the following: ? Takes details of a student/library users: first name, last name, other names, user ID number, and nationality.

  Individual genes are substrings of a genome

Individual genes are substrings of a genome delineated by 3-element start and stop codons. Genes begin with the start codon ATG and end with one of the following 3 stop codons: TAG, TAA or TGA. Note that start codons can appear anywhere in the string..

  Up through 2 to the 10th power

program should compute and output all of the "powers of two" from 2 to the 0th power, up through 2 to the 10th power.

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