Reading to a Sentinel Value, Decision Logic, Simple Character I/O

Read and follow the following program specification carefully.

The Program Specification: A Simple Four-Function Calculator

You will write a program that performs as a simple four-function calculator; that is, it will perform addition, subtraction, multiplication and division. This calculator is also aware of the C++ distinction between integer and floating-point values, and always performs its calculations according to the rules C++ applies to arithmetic expressions involving

int and double values.

The calculator will read input lines containing valid arithmetic expressions of the form:

operand1 operator operand2

and calculate the result. Each expression will be calculated using either C++ int variables or C++ double variables, depending on a flag given at the beginning of that line.

Input file description and sample:

Your program must read its input from a file named CALC.DAT - use of another input file name will result in massive deductions. Except for the sentinel line, each line of the input file will contain four values, separated by whitespace:

a flag indicating whether the expression is to be evaluated using C++ ints (I) or doubles (D)

the first operand, either an integer or a decimal number

the operator symbol (+, -, /, or *)

the second operand, either an integer or a decimal number

You may assume that each such line will contain a valid arithmetic expression. You may also assume that there will be no decimal operands on a line beginning with the 'I' flag. The flag, operands and operator will be separated by whitespace characters; how many may vary, so don't assume there will be exactly one blank separating each.

Sentinel line: there will be a single line containing the E (exit) flag. The calculator program will exit immediately upon reading the E flag.

For instance:

I 294 + 32

D 23.70 * 17

I 469 / 589

I 173 + 72

D 24.30 - 14.05

I 228 * 792

I 143 * 896

I 165 - 667

D 24 + 2.76

D 6.62 / 32.5

E

Note that you must not make any assumptions about the number of lines of data in the input file. Your program must be written so that it will detect the occurrence of the sentinel value and terminate correctly at that time.

 

What to Calculate:

Your program must read each line of input data, as described above, and calculate the value of the given expression. In order to achieve the best possible accuracy, you are required to use variables of type double to store all real (that is, decimal) numbers needed in this program. Since only the four basic arithmetic operations are used, your program will not need any Standard Library functions, such as pow( ).

Output description and sample:

Your program must write all output data to a file named CALC.OUT - use of any other output file name will result in a massive deduction of points. The sample output file shown below corresponds to the sample input data given above. Note: the following example is not formatted into appropriate columns.

Simple Calculator by

Jonathan Leidig

 

Expression = Value

-------------------------------------------------

294 + 32 = 326

-------------------------------------------------

23.70 * 17.00 = 402.90

-------------------------------------------------

469 / 589 = 0

-------------------------------------------------

173 + 72 = 245

-------------------------------------------------

24.30 - 14.05 = 10.25

-------------------------------------------------

228 * 792 = 180576

-------------------------------------------------

143 * 896 = 128128

-------------------------------------------------

165 - 667= -501

-------------------------------------------------

24.00 + 2.76 = 26.76

-------------------------------------------------

6.62 / 32.50 = 0.20

-------------------------------------------------

 

You are not required to use this exact horizontal spacing, but your output must satisfy the following requirements:

The first line of the output file must contain the given title.

The second line of the output file must contain your name.

The third line of the output file must be blank.

You must use the specified column labels, in the fourth line of the output file.

All doubles must be formatted to show exactly 2 digits following the decimal point.

All ints must be formatted as integers, without a decimal point.

The original equation must be repeated, followed by an equals sign, followed by the computed value of the given expression.

Allow sufficient space for each value your program prints; the operands and results could require up to 8 columns to print. Be careful not to run things together.

You must include divider lines as shown.

You must arrange your output in neatly aligned columns.

You must use the same ordering of the columns as shown here.

Do not insert any additional lines of output; do have a newline at the end of each line.

Programming Standards:

You'll be expected to observe good programming and documentation standards. All the discussions in class about formatting, structure, and commenting your code will be enforced. Some, but not necessarily all, specifics:

You must include header comments specifying your name and the date your source code and documentation were completed.

The header comment must also include a brief description of the purpose of the program (sort of a user guide) - this should be in your own words, not copied from this specification.

You must include a comment explaining the purpose of every variable you use in your program.

You must use meaningful, suggestive (of function or purpose!) variable names.

Precede every major block of your code with a comment explaining its purpose. You don't have to describe how it works unless you do something so sneaky it deserves special recognition.

You must use indentation to make control structures, like loops, if-else statements, or function bodies more readable.

Choose appropriate control structures. You are required to use at least one switch statement in this program.

Hints:

Remember to name your project LastName-First-Project3.cpp. Your program must read lines of input data until the sentinel value is encountered; it's not a bad idea to write your input code so it will also terminate on a read failure. The test files are guaranteed to include a sentinel line, but if your input logic is incorrect your program could miss it.

You should read the operands as numerical values of the appropriate type, not as character data. On the other hand, you'll have to read the operator as character data. The most common source of confusion in this assignment will be the distinction between a character and its meaning - that's deliberately cryptic, but if you're having trouble you might benefit from pondering what it might mean.

Testing:

At minimum, you should be certain that your program produces the output given above when you use the given input file. However, verifying that your program produces correct results on a single test case does not constitute a satisfactory testing regimen. You should make up and try additional input files as well; of course, you'll have to determine what the correct output would be.

What to turn in and how:

This project will be submitted to the Curator system for runtime testing, just as for the earlier Projects.

You must also turn in a printout of your fully documented C++ source code, which will be graded for adherence to the programming and documentation standards given in this specification. Print your source code using a fixed-pitch font, such as Courier rather than a proportional font, such as Times Roman.

Your final project score will be a weighted average of the runtime score (70%) and the score from your hardcopy (30%).

Getting Started:

//Read a line of input

//Determine data type

//Determine operation

//Calculate and output value