Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

can anyone help me please with this one i posted 2 times thst both of them was wrong, need step by step i couldnt do

can anyone help me please with this one i posted 2 times thst both of them was wrong, need step by step

i couldnt do the part 3

For this assignment, write a program that will perform various arithmetic operations.

The program should be able to:

add two integer values

subtract two integer values

multiply two integer values

divide two integer values

raise an integer value to an integer power

calculate the factorial of an integer

Rather than starting from scratch for this assignment, a driver program is available and will need to be modified to complete the assignment. The driver program can be found at:

http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm6driver.cpp

or on Blackboard. The program is menu-driven, allowing the user to select an option and then based on the option, asking for more information. A value of '+' indicates that an addition operation should be performed, '-' indicates subtraction, '*' indicates multiplication, '/' indicates division that results in the quotient and remainder, '^' indicates raising a number to a power, '!' indicates that a factorial operation should be performed, 'q' or 'Q' indicates the user wants to quit the program, and any other character is invalid.

For all of the arithmetic operations, with the exception of factorial, the user will be prompted to enter two integer values to use in the calculation. The factorial option will only prompt the user to enter one integer value to use in the calculation.

To receive full credit for the assignment, the following 3 parts must be completed

Part 3:

DO NOT move on to this part of the assignment until Parts 1 and 2 run correctly.

In the previous assignment, the random number generator was used to remove the need for user input in the program. For the final part of this assignment, all of the user input statements will be replaced by statements that read the information from a file.

Input File

The input file for this assignment is named math.txt. It can be downloaded from http://faculty.cs.niu.edu/~byrnes/csci240/pgms/math.txt or Blackboard.

Each line in the file represents a potential arithmetic operation with the first character on the line indicating the type of operation that should be performed.

If the first character on the line is a '+', then an addition operation should be performed with the two integer values that follow the '+'.

A '-' indicates that a subtraction operation should be performed with the two integer values that follow the '-'. The second value should be subtracted from the first value.

A '*' indicates that a multiplication operation should be performed with the two integer values that follow the '*'.

A '/' indicates that a division operation that results in a quotient and quotient should be performed with the two integer values that follow the '/'. The first number is the dividend. The second number is the divisor.

A '^' indicates that a value should be raised to a power. It will be followed by two integer values. The first number is the base. The second number is the power/exponent.

A '!' indicates that a factorial operation should be performed with the single integer value that follows the '!'.

Any other character is invalid and should cause an error message to be displayed and any other information on the line to be ignored.

So, a line in the file that resembles:

+ 23 34 

should display the result of adding 23 and 34 together.

Working with an Input File

Since the input for this program will be coming from a text file rather than standard input (the keyboard), the program will not use cin when it needs to read data. Instead, it will have to read the data from the file. This also means that there will not be any cout statements to prompt the user to enter information.

In order to read from a file, add two #include statements to the program. The new libraries that are needed for this part of the assignment are fstream and cstdlib.

Now, create an input file stream variable and "connect" it with the text file. The "connection" is created by opening the file:

ifstream infile; //input file stream variable //this will be used instead of cin infile.open( "math.txt" ); //open the file for reading 

The open statement will open a file named math.txt and connect it to the variable infile. The file must be located in the same directory as the CPP file. (For Mac users, you will probably have to put in the set of double quotes(""), find where the file has been saved on your computer, and then drag and drop the file in between the quotes. It should place the name of the file, including the complete path of where it is located on your computer, between the quotes.)

Once the file has been opened, make sure that it opened correctly. This is an important step because a file cannot be processed if it doesn't exist or open correctly. To test if the file opened correctly:

if( infile.fail() ) //if the input file failed to open { cout << "input file did not open" << endl; exit(-1); //stop execution of the program immediately } 

In order to read a character from a file, the input operator can be used in the same manner that it's used with a cin statement. So:

cout << "What operation would you like to perform:" << endl << " + addition" << endl << " - subtraction" << endl << " * multiplication" << endl << " / division" << endl << " ^ number to power" << endl << " ! factorial" << endl << " q quit" << endl << endl << "Operation? "; cin >> operation; 

from the original driver program, will become:

infile >> operation; 

In the driver program, the user had the option to 'q' or 'Q' to indicate they are ready to quit. Since the data is coming from a file and the prompts to the user are being eliminated, there has to be some other way to determine when the program should stop running. The "other way" is to determine when there is no more data in the file. One way to do this is to use the input file stream variable as a boolean variable:

while( infile ) //while there are characters in the file 

As long as there is information in the file, the input file stream variable will remain valid (or true). Once the end of the data has been reached, the stream will become invalid (or false). Note: this test is only successful AFTER an attempt has been made to read data from the file. This means that a standard read loop pattern should be followed.

In the case of an invalid operation type, it's possible that there is other information on the line in the file. For example:

a 12 -9 

If the program is written correctly, the 'a' will be read into the operation variable from the earlier example. Before moving on to the next line in the file, the 12 and -9 need to be removed from the file. To do this, use the ignore function:

infile.ignore( 100, ' ' ); 

This will either ignore the next 100 characters in the input file stream or all of the characters in the input file stream until a newline character is reached, which ever occurs first.

Finally, when a file is done being processed, it should be closed.

infile.close(); 

Part 3 Requirements

In the file that is submitted for grading, make sure that the open statement in main() contains only the file name math.txt. Any path that is placed before the file name (usually on a Mac) should be removed.

Hand in a copy of the source code (CPP file) from Part 3 using Blackboard. This file should contain all of the changes that were made to the original driver program through all 3 parts of the assignment.

Part 3 Output

88 + 19 = 107 29 - 28 = 1 1000 + 14 = 1014 6 * 3 = 18 2^5 = 32 45 / 8 = 5 45 % 8 = 5 6! = 720 That is an invalid operation! 0! = 1 8^0 = 1 9 * -2 = -18 -50 - -324 = 274 -4 * -9 = 36 10 / 2 = 5 10 % 2 = 0 5 / 9 = 0 5 % 9 = 5 240 + 322 = 562 5! = 120 That is an invalid operation! That is an invalid operation! 16^4 = 65536 1 / 2 = 0 1 % 2 = 1 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions