Lab 10 - Arithmetic
Overview
Given a simple mathematical expression on the command line, you should evaluate the expression and output the result to stdout
Your goal is to error check the input and to convert command line arguments in argv[ ] to floats and char as appropriate. Several techniques exist for converting strings to numerical data types: sscanf will allow format conversions similar to scanf, while atof and atoi convert strings to floating point type double and integer types respectively. The sscanf function works in a manner that is analogous to fscanf and supports the same set of format codes (%d, %f, %c, %s, etc). The only difference is that sscanf reads from a NULL terminated string that is already present in memory instead of a file.
Requirements
Your program should accept a mathematical expression in one of these forms
float operator float
//ex: calc 5.3 + 2.0 or calc 5 + 2.0
int operator int
//ex: calc 5 + 2
intOperator
//ex: calc 5!
on the command line.
-
You must support six operations: +, -, x, /, %, and !. NOTE: use x not * for multiply! % is valid only for integer arithmetic
-
If the expression is ill-formed your program should output an error message and return an error value.
-
If the expression is valid, you should evaluate the expression and output the result to stdout. NOTE: call atof if either operand contains a decimal, atoi otherwise. If either operand has a decimal then perform floating point arithmetic, but it neither has a decimal, perform integer arithmetic.
-
A switch statement must be used.
-
A recursive function call must be used to solve the factorial problem. NOTE: the value of argc is 2 in the factorial case, the last character before the '\0' is !, and the operand is the substring up to the ! and contains no decimal. Do not attempt to access argv[2] or argv[3] when argc is 2.
Compilation Instructions
Name your program calc.c. You can compile your program with: Eclipse.
Example Usage
Your program should work as the sample below:
9
>calc 5 + 4.0
9.0
>calc 5.9 - 0.3
5.6
>calc 4 / 2
2
>calc 5 x 4
20
>calc 5!
120
>calc 5 % 3
2
Extra Credit
You can receive a maximum of 3 points extra credit, just by making all six
operations work, including error reporting for ill formed problems only if your
work in turned in on time.
After you have completed the arithmitic program and
have an output similar to the example usage, please submit your work
via UNCW webmail by following these simple steps.
Confirmations: A student can then be assured
that their assignment has been submitted based on the return email
which is sent only if the above requirements are met. The student
should not delete the confirmation email. Any work submitted after
11:59 PM on Friday November 14th will be late.Turn In Work
calc.c source code attachment