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

  Reportingmark a string of 2 to 4 upper case

Declare a structure named: Car containing: reportingMark a string of 2 to 4 upper case characters carNumber an int kind could be box tank flat or other loaded a bool destination a string with a destination or the word NONE Note: A destination is r..

  Lu decomposition with gauss elimination

The LU decomposition with Gauss elimination and what is the physical interpretation of the elements of ? Write C++ programs for steps 2, 3, and 4.

  Ruby implement primitive types

How does Ruby implement primitive types, such as those for integer and floating-point data?  3-What is the single most important practical difference between Smalltalk and C++?

  C assignment of curl library

C Assignment Curl library must be used To be done on linux, it should be compiled using the following command: gcc -Wall -ansi -pedantic NameOfFile.c -lncurses Please make sure it is commented with clarification Variable names in camel case

  Putting objects within objects is the essence of composition

Putting objects within objects is the essence of composition. It is called composition for obvious reasons. As we always say that if something is made from other things that it is composed from those things.

  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

  Allows the user to enter the coefficients for a system

Create a graphical (not text-based) Visual C++ program that allows the user to enter the coefficients for a system of four equations with four unknowns. Include a button that when clicked, will solve the system of equations and display the results in..

  Write an lc-3 machine language program

Write an LC-3 machine language program starting at location x3000 which divides the number in memory location x4000 by the number in memory location x4001 and stores the quotient at x5000 and the remainder at x5001.

  C++ pointer function synopsis

C++ pointer Function Synopsis: char *amonthLess(char *dateString) Description: Variable dataString is a valid date in a form of mm/dd/yyyy Given the above information, write a main() program that asks a user to enter a date in a form of mm/dd/yyyy..

  Using an abstract class with only pure virtual functions

Using an abstract class with only pure virtual functions, create three small classes unrelated by inheritance---clases Building, Car and Bicycle. Give each class some uique appropriate attributes and behaviors that it does not have in common with oth..

  Recursive function

What is the result of the following recursive function when called with mm(10)? Write out the execution trace of this call to mm(10).

  Optimization is to add a sorting algorithm

The second optimization is to add a sorting algorithm. Each time you add an item to the array (from disk or from keyboard), you call a function called SortMyArray(), which use the bubble sorting algorithm to sort the array in the ascending order, ..

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