Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Program 3: Command Line Calculator Topics: Functions or methods, global variables OVERVIEW You will build a simple command line calculator. It supports addition (add), subtraction

Program 3: Command Line Calculator

Topics: Functions or methods, global variables

OVERVIEW

You will build a simple command line calculator. It supports addition (add), subtraction (sub), multiplication (mul), division (div), and calculation of the absolute value (abs). You will use functions to support each type of calculation. You can assume that the numbers are float data types.

SOFTWARE SPECIFICATIONS

The user interacts with program by entering a command, followed by some parameters at the command prompt >. For example, if the user wants to add 4 and 5, he/she enters add 4 5 and hits the return key. The system then outputs the result of the addition command (in this case, 9). See sample run output below for more examples.

Once the commands operation is completed, the system goes back to the command prompt waiting for another command from the user. The commands are add, sub, mul, div, and abs. All the commands take two arguments except for abs which takes one argument. Each command will call the corresponding function to perform the calculation. Name each function the same name as the command. The quit command calls the printStats function and ends the program. The printStats function prints the number of times each function were called (executed). To keep track of function call usage, you will need global variables which can be shared across functions.

Command

# of arguments

Behavior

add

2

Adds the two arguments.

sub

2

Subtracts the second argument from the first argument.

mul

2

Multiplies the two arguments.

div

2

Divides the first argument by the second argument.

If the second argument is 0, print Error: Division by zero and return 0.

abs

1

Computes the absolute value of the argument.

quit

0

Prints the function usage statistics, and ends the program.

PARSING THE COMMAND LINE

Your program will read the command as a single line. It then splits it into several tokens. The first is the command. Any other tokens after the first are the parameters. To parse a command, simply use the split function on the string. Dont forget to convert the parameters to floats so that you can perform arithmetic operations.

The code below shows you how to use the split function to extract the command and the parameters.

>>> userinput="add 1 2" >>> tokens = userinput.split(" ") >>> print(tokens[0]) add >>> print(tokens[1]) 1 >>> print(tokens[2]) 2

SAMPLE OUTPUTS

Welcome to iCalculator. >add 3 4.0 7.0 >mul 3 5 15.0 >abs 5 5.0 >abs -7 7.0 >abs 8 8.0 >quit Function usage count add function : 1 sub function : 0 mul function : 1 div function : 0 abs function : 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases 28th British National Conference On Databases Bncod 28 Manchester Uk July 2011 Revised Selected Papers Lncs 7051

Authors: Alvaro A.A. Fernandes ,Alasdair J.G. Gray ,Khalid Belhajjame

2011th Edition

3642245765, 978-3642245763

More Books

Students also viewed these Databases questions