In: Computer Science
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 command’s 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. Don’t 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
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You !! =========================================================================== # check if the token are valid numbers def valid_number(value): try: float(value) return True except: return False def main(): print('Welcome to iCalculator') add = sub = mul = div = absolute = 0 while True: userinput = input('>').lower() if userinput == 'quit': break tokens = userinput.split(" ") if len(tokens) == 2: operation = tokens[0] if operation == 'abs' and valid_number(tokens[1]): print(abs(float(tokens[1].strip()))) absolute += 1 else: print('Invalid number.') elif len(tokens) == 3: operation = tokens[0] if operation == 'add' and valid_number(tokens[1]) and valid_number(tokens[2]): print(float(tokens[1].strip()) + float(tokens[2].strip())) add += 1 elif operation == 'sub' and valid_number(tokens[1]) and valid_number(tokens[2]): print(float(tokens[1].strip()) - float(tokens[2].strip())) sub += 1 elif operation == 'mul' and valid_number(tokens[1]) and valid_number(tokens[2]): print(float(tokens[1].strip()) * float(tokens[2].strip())) mul += 1 elif operation == 'div' and valid_number(tokens[1]) and valid_number(tokens[2]): if float(tokens[2]) != 0: print(float(tokens[1].strip()) / float(tokens[2].strip())) div += 1 else: print('/0 undefined.') else: print('Invalid syntax') else: print('Command not found.') print('Function usage count') print('add function: {}'.format(add)) print('sub function: {}'.format(sub)) print('mul function: {}'.format(mul)) print('div function: {}'.format(div)) print('abs function: {}'.format(absolute)) main()
================================================================