Write a program in C that carries out simple mathematical operations entered as a command-line strings named "calculator". Implementation: You will receive a single prefix arithmetic expression in text via argv[1]. You can assume the operands and operators will be space-separated. Test for possibility (i.e. no dividing by zero) and is correctly formatted (i.e. has exactly two operands). Your operands are: Your operators are: Your errors are: zero plus Error: not an operator: one minus Error: not an operand: two multiply Error: not enough operands three divide Error: too many elements four Error: divide by zero five six seven eight nine Common arithmetic expressions with the operator in betwene the operands are called "infix". Prefix notation puts the operand first, so "one plus two" is the infix version and "plus one two" is the prefix version. Be careful to apply context-sensitive operators (like minus and divide) in the correct direction, for example "2 - 1" in infix is equivalent to "minus two one" in prefix and not "minus one two" You can presume you'll have a single simple expression. There will be no subexpressions, so every expression should have exactly three words: an operator followed by two operands. Once you decode the expression, you should compute it and output the result in double precision (%2.2ld) and return 1. If you find an error, you should print an error message and return -1. e.g. ./calculator "plus four five" 9.00 ./calculator "divide one four" 0.25 ./calculator "five plus four" Error: not an operator: two ./calculator "divide three zero" Error: divide by zero ./calculator Error: not an operator: ./calculator "plus two one six" Error: too many elements
Write a program in C that carries out simple mathematical operations entered as a command-line strings named "calculator".
Implementation:
You will receive a single prefix arithmetic expression in text via argv[1]. You can assume the operands and operators will be space-separated. Test for possibility (i.e. no dividing by zero) and is correctly formatted (i.e. has exactly two operands).
Your operands are: Your operators are: Your errors are:
zero plus Error: not an operator: <text>
one minus Error: not an operand: <text>
two multiply Error: not enough operands
three divide Error: too many elements
four Error: divide by zero
five
six
seven
eight
nine
Common arithmetic expressions with the operator in betwene the operands are called "infix". Prefix notation puts the operand first, so "one plus two" is the infix version and "plus one two" is the prefix version. Be careful to apply context-sensitive operators (like minus and divide) in the correct direction, for example "2 - 1" in infix is equivalent to "minus two one" in prefix and not "minus one two"
You can presume you'll have a single simple expression. There will be no subexpressions, so every expression should have exactly three words: an operator followed by two operands.
Once you decode the expression, you should compute it and output the result in double precision (%2.2ld) and return 1.
If you find an error, you should print an error message and return -1.
e.g.
./calculator "plus four five"
9.00
./calculator "divide one four"
0.25
./calculator "five plus four"
Error: not an operator: two
./calculator "divide three zero"
Error: divide by zero
./calculator
Error: not an operator:
./calculator "plus two one six"
Error: too many elements
Step by step
Solved in 2 steps with 3 images