For this assignment you will implement a simple calculator or interpreter that reads arithmetic expressions from a file.
Specifically, you will implement the following function:
/*
* Reads one arithmetic "expression" at a time from a file stream, computes, then
* returns the result. If there are additional expressions in the file, they are
* read and computed by successive calls to “calculator”.
*
* “Expressions” are groups of operations (add, subtract, multiply, divide). Your
* calculator will read and execute these operations one by one to compute the
* result of anexpression.
*
* Much like a standard calculator, your function will be able to use the result
* from the immediately previous computation.
*
* Here is a sample “add” (adds 5+4) operation, see the .h file or the later part
* of this specification for more information about the operation format.
*
* 0 5 4
*
* More than one operation can be grouped into a single
* "expression". Here is a sample file:
*
* 3
* 0 5 4
* 1 4
* 5 2
*
* 1
* 2 3 1
*
* 1
* 7 2
*
* In this example there are 3 expressions. Each expression is preceded by a
* single number telling you how many operations there are in an expression. So
* the first expression has 3 operations (the first one is the add operation from
* above).
*
* The next expression has one operation and the last expression also has a
* single operation.
*
*
* Pre: calc_cmd is has been initialized with a file opened in
* read mode.
*
* Post: The result of the next “expression” in the file is returned. So the first
* call to calculator with above file would return ((4 + 5) + 4) * 2 = 26, the
* second (3-1) = 2, and the third (0 / 2) = 0. If there are no more expressions
* or an error is encountered -1 is returned.
*
*/
int calculator(FILE* calc_cmd);
(C code, Linux)
GIVEN:
calculator.c : (This is what needs to be changed
#include "calculator.h" int calculator(FILE* calc_cmd) { return -1; }
calculator.h:
#ifndef CALCULATOR_H_ #define CALCULATOR_H_ #include
int calculator(FILE* calc_cmd); #endif
driver.c :
#include // for I/O functions #include // for C99 exact-width integer types #include // for formatting of stdint types #include // for time(); used for random number generator #include // for rand() and srand() #include "calculator.h" /* Test driver for the simple calculator program */ int main(int argc, char ** argv) { /* check the number of parameters, here in the program we expect 2 * (1) the program name, i.e. "driver" * (2) the filename * So, if we don't have exactly 2 we stop the program */ if(argc != 2) { printf("Error: Incorrect number of parameters.\n"); printf("Usage: driver filename.txt\n"); return 1; } /* open the file provided on the command line */ FILE * calc_cmds = fopen(argv[1], "r"); /* we need to test the file was opened properly, if the FILE* * is NULL then we have a problem */ if(calc_cmds == NULL) { printf("Error: Could not open file: %s.\n", argv[1]); return 2; } printf("Begin testing calculator function: \n"); int output, count = 1; /* here we test your function */ while((output = calculator(calc_cmds)) != -1) { printf("The result of equation %d is %d\n", count, output); count++; } printf("End testing calculator function.\n"); return 0; }
test.txt:
3 0 5 4 1 4 5 2 1 2 3 1 1 7 2
Extra Info:
The focus of the assignment is understanding C I/O functions like fscanf().
Program Logic: In this assignment, you will implement a simple calculator with addition, subtraction, multiplication, and division operations.Further, those operations can be combined together to create larger computations or “expressions”, so your calculator will be able to computethings like: ((5 + 4) + 4) * 2. All of the operations and expressions will be read from a file. Further, all of these operations will use will integers when performing calculations. Much like a standard calculator, your program will be able to use the result from the immediately previous computation. Only the last the result from the previous computation needs to be stored. For the remainder of the specification we refer to this as the "Previous Result". Initially, the Previous Result should be zero, also note that the value of the Previous Result is not retained across function calls. Calculator Operations Operations in the file have a type, which is represented by an integer, followed by either one or two parameters, which are also integers, depending on the type of the operation. Each operation is stored on a single line in the file, and looks like either:
<operation type (int)> <argument 1 (int) > < argument 2 (int)>
or
<operation type (int)> <argument1 (int)>
For example, "0 3 2" in the input file, is the "add" (represented by a 0) operation and it has two parameters. After this line is “executed”, the result 5 (3 + 2) is stored (this is now the Previous Result).
As another example, if the next line in file is “1 2”, it is the add operation that uses the Previous Result (5 in this case) and is the equivalent of 5 + 2. The standalone operations take 2 parameters while the operations that use the Previous Result take only 1 parameter.
Here are all of the operations your calculator must support, their format, parameters, and behavior:
Adding
0 X Y - same as X + Y, stores the newly computed result in Previous Result
1 Y - same as Previous Result + Y, stores the newly computed result in Previous Result
Subtracting
2 X Y - same as X - Y, stores the newly computed result in Previous Result
3 Y - same as Previous Result - Y, stores the newly computed result in Previous Result
Multiplying
4 X Y - same as X * Y, stores the newly computed result in Previous Result
5 Y - same as Previous Result * Y, stores the newly computed result in Previous Result
Dividing
6 X Y - same as X / Y, you don't have to worry about if Y is 0, stores the newly computed result in Previous Result
7 Y - same as Previous Result / Y, like above you don't have worry if Y is 0, stores the newly computed result in Previous Result
Note: that the order of the operations in the file, isthe order they should be computed in, so you can assume there are implied parentheses around each operation
Hi,
As your problem suggests that you need to get the values from a file so you need to make a object of filestream as:
FileStreaam fs=new FileStream();
or you can create the object of inputstreamreader to do so.Also,as you need to check for the expressions in the file data but you have not mentioned the exact order of expression that you need but you can check that with the help of either if condition or switch case and you can store the result into some variable and after your calculation gets over , you can store this in to the file object that you created before.Like this , you can approach your problem.
I hope this solves your problem.Please let me know in case of any further queries.
Get Answers For Free
Most questions answered within 1 hours.