ComputerScienceExpert

(11)

$18/per page/

About ComputerScienceExpert

Levels Tought:
Elementary,Middle School,High School,College,University,PHD

Expertise:
Applied Sciences,Calculus See all
Applied Sciences,Calculus,Chemistry,Computer Science,Environmental science,Information Systems,Science Hide all
Teaching Since: Apr 2017
Last Sign in: 103 Weeks Ago, 3 Days Ago
Questions Answered: 4870
Tutorials Posted: 4863

Education

  • MBA IT, Mater in Science and Technology
    Devry
    Jul-1996 - Jul-2000

Experience

  • Professor
    Devry University
    Mar-2010 - Oct-2016

Category > Programming Posted 29 May 2017 My Price 11.00

Reverse Polish Notation Calculator

Reverse Polish Notation Calculator 

This information is based on the wikipedia description of a RPN calculator, but similar descriptions exist intextbooks and on other web sites. RPN calculator operation is relatively straightforward:• While there is information to read from the user– Read in the information in the form of a string– If the string contains a number∗ If the stack is full· print “(Error) Stack overflow.”· continue the loop∗ Add the number onto the top of the stack. This operation is referred to as pushing a numberonto the stack.– If the string is not a number, check to see if it is a known operator. (+,-,*./,p,l,q)∗ If the operation is ’q’ break the loop.∗ If it is a known operator check to make sure there are enough numbers, called operands, onthe stack to perform the operation. ( [+, -, *, /] takes 2 operands, [p] takes 1 operand, [l, q]require no operands)· If there are fewer operands in the stack than required for the operation the programshould print “(Error) there are not sufficient values in the expression”.· continue the loop∗ Remove the operands from the stack∗ Perform the operation, with the values as arguments.1∗ Push the result, if any, back onto the stack.– If the string contains an unknown operator or symbol∗ Print “(Error) Unknown operator”∗ continue the loopExampleThe expression ”5 + ((1 + 2) * 4) - 3” can be written down like this in RPN:512+4*+3-The table below describes what should be happening in your program:Input Operation Stack num operands Comment5 Push operand 5 11 Push operand 5, 1 22 Push operand 5, 1, 2 3+ Add 5, 3 2 Pop two values (1, 2) and push result (3)4 Push operand 5, 3, 4 3* Multiply 5, 12 2 Pop two values (3, 4) and push result (12)+ Add 17 1 Pop two values (5, 12) and push result (17)3 Push operand 17, 3 2- Subtract 14 1 Pop two values (17, 3) and push result (14)The stack is an array. The num operands is used to keep track of how many numbers (operands) are in thestack at any time. One important thing to realize is that to remove a number from the stack all one has todo is subtract one from the num operands. The value does not have to be “cleared” or removed in any otherway. The num operands represents how many valid operands there are on the stack. The contents of the restof the stack do not matter.2A RPN calculator can be found at http://www.arachnoid.com/lutusp/calculator.htmlI have provided an executable file. It should work on most windows computers. If it does not work, you arewelcome to look at any number of online RPN calculators.Assignment1. Get the partial source file included with this document. The provided code will read data from the userand determine if it is a number or an operator. Add the appropriate code where indicated. The codeadded to the program must process numbers and operators from the list above appropriately. It shouldnot be possible to crash the calculator. That requires that you make sure there are enough operandson the stack and the operation is defined. Also, the user must not be able to preform operations thatgenerate errors such as dividing by zero or overflowing the stack. If the user attempts a illegal action,the command should be ignored and an error message describing the error should be printed to thescreen.2. Each item in the table below, with the exception of quit, should be a function. The only two functionsthat should modify the stack are pop and push. The list function will access the stack to print thevalues, but not change the number of items in the stack or the stack itself.entry command operands descriptionreqired+ add 2 pops two numbers, adds them, and push result- subtract 2 pops two numbers, subtracts the last from the first, and push result* multiply 2 pops two numbers, multiplies them, and push result/ divide 2 pops two numbers, divides second number popped by first,and push resultp pop 1 removes a number from the stackl list 0 prints all the numbers in the stackq quit 0 stops the program0–9 push 0 adds the number to the stackStart by implementing three functions: push, pop, and list. Push and pop are the building blocks forthe remaining functions and list will tell you what is in the stack. The pop function should removea value from the stack and return the value. It is important to note that removing a number from thestack only requires a change in the stack size. The push function should add a value to the stack anddoes not need to return anything.An example function:void divide(){double result;3/*Check to ensure that there are two numbers on the stack andwe are not dividing by zero*/if(idx

 

ENGR 200FALL 2015Homework 7: Reverse Polish Notation CalculatorEC Nov. 5that Class time, Due Nov. 10that 11:59pmPOINTS: 50ObjectivePractice using functions, switch statements, arrays, and loops.How a reverse polish notation (RPN) calculator worksThis information is based on the wikipedia description of a RPN calculator, but similar descriptions exist intextbooks and on other web sites. RPN calculator operation is relatively straightforward:•While there is information to read from the user–Read in the information in the form of a string–If the string contains a number*If the stack is full·print “(Error) Stack overflow.”·continue the loop*Add the number onto the top of the stack. This operation is referred to as pushing a numberonto the stack.–If the string is not a number, check to see if it is a known operator. (+,-,*./,p,l,q)*If the operation is ’q’ break the loop.*If it is a known operator check to make sure there are enough numbers, called operands, onthe stack to perform the operation. ( [+, -, *, /] takes 2 operands, [p] takes 1 operand, [l, q]require no operands)·If there are fewer operands in the stack than required for the operation the programshould print “(Error) there are not sufficient values in the expression”.·continue the loop*Remove the operands from the stack*Perform the operation, with the values as arguments.1

/**********************************************************************************************Program Name:REVERSE POLISH CALCULATORAuthor(s): I. R.Hacker*ENGR 200.01Assignment 6*Date 10-14-07**Function:A reverse polish notation calculator************************************************************************************************Variables|function|units*--------------------------------------------------------------------------**********************************************************************************************//* Header Files */#include <stdio.h>#include <math.h>#define MAX_BUFFER_SIZE 100/*prototypes*//*global variables*//*Start of main function*/int main(void){/* declarations: */char buff[MAX_BUFFER_SIZE];double x;int do_not_quit = 1;printf("REVERSE POLISH CALCULATOR\n");while(do_not_quit == 1){/*This reads a string from the stack*/if( scanf("%s", buff) < 1 ){printf("I did not understand\n");return 0;}/* checks to see if the user entered a number */if(isdigit(buff[0]) || isdigit(buff[1])){/*turns string into number and puts value in x*/sscanf( buff, "%lf", &x);/* place statements here to handle getting a number */} else {/* place statements to do operations */

Attachments:

Answers

(11)
Status NEW Posted 29 May 2017 04:05 AM My Price 11.00

-----------

Not Rated(0)