PA 4.2: Calculator
- Due Mar 23, 2021 by 11:59pm
- Points 10
- Submitting a file upload
- File Types cpp
- Available after Mar 9, 2021 at 2pm
When completing this programming assignment, you may work with up to one other person. No groups of more than two are permitted.
In this program, you will create a calculator that allows the user to enter simple expressions in the format of: x <op> y, where <op> can be any of:
- *
- +
- -
- /
- % (mod; x and y must be cast to integers)
- ^ (exponentiate, e.g., x to the power of y)
Your program should allow the user to enter an expression, process it, display the result, then wait for the next expression. If the operator is not recognized, an error message should be displayed and the program should wait for the next expression.
The program should stop when the first operand isn't numerical, that is when there is an error reading a double value with cin. In this case, cin.fail() will be true. Effectively, this means you can tell the user to enter "quit" to exit the program (but really, anything should work).
Here's an example where the user computes five different expressions, one of them with an unrecognized operator:
$ ./calculator.exe
==== Calculator ====
Please enter simple expressions in the form: x <op> y, where <op> can be any of *, +, -, /, or %.
Type "quit" to exit.
> 5 * 3
15
> 5 & 3
Unsupported operator.
> 22 - .5
21.5
> 2 ^ 3
8
> 5 / 2
2.5
> quit
Copy the following header and specifications checklist and paste it at the top of your source code. As you complete the specifications, fill in the [ ] next to that specification in the check list (e.g., like this: [x]). I will not grade submissions with missing specifications; please do not submit if you cannot check all of them off. This specs checklist is essentially a way for you to self grade before you submit your program.
// Name:
// Date:
// Partner:
//
// Specifications checklist for PA 4.2
//
// General specs:
// [ ] the header includes your name and anyone you worked with
// [ ] the header includes this specifications checklist with all completed
// specifications checked off: [x]
// [ ] the code is indented properly (inside of every block, code is indented
// one more tab)
// [ ] each chunk of code that "hangs together" (works toward a higher level goal):
// [ ] includes a brief, useful comment above it
// [ ] is separated from the next chunk by a blank line
// [ ] there are no really long lines of code or comments (if you have long
// lines, split them across multiple shorter lines)
// [ ] identifiers are well named
// [ ] the program compiles
// [ ] the program runs without crashing or hanging
// [ ] all output and prompts look clean (correct spelling, capitalization,
// nothing squished)
// [ ] all prompts make it clear what data the user is expected to enter and in
// what format and work as expected
//
// Specific specs:
// [ ] the program reads in simple binary expressions of the form: x <op> y from the user
// [ ] *, /, -, +, %, and ^ are all supported
// [ ] unsupported operators cause an error message to be displayed
// [ ] the result of the expression is displayed
// [ ] for mod (%), both x and y are cast to integers prior to performing the mod
// [ ] the result of the expression is accurately computed
// [ ] the program loops until the user enters something other than a numerical x in the
// input expression (e.g., "quit")
// [ ] this is tested by checking if cin.fail() is true
// [ ] appropriate loops are used
If you are working with a partner, both of you must submit individually.