Question
The below program is to be coded in C++ Define a fraction class as depicted in the UML diagram at the top. Note the count
The below program is to be coded in C++
Define a fraction class as depicted in the UML diagram at the top.
Note the count attribute is underlined, that indicates it is a static data member.
Exercise the fraction class using file I/O. The first item in the input data file is a character indicates the operation type:
a add fraction
s subtract fraction
m multiply fraction
d divide fraction
A add a floating value to a fractional number
S subtract a floating value from a fractional number
M multiply a fractional number by a floating value
D divide a fractional number by a floating value
c compare two fractional numbers
To convert a floating value to a fractional number, you need determine the number of decimal places it carries. For example, 2.45 can be converted to 2 45/100 first, then be simplified to 2 9/20. The denominator starts at 100 because 2.45 has two decimal places. In order to determine how many decimal places a floating value carries, you can use the stringstream class or the to_string function introduced in C++ 11, which is implemented in Visual Studio 2012 and after.
The result of the calculation goes to an output file with one calculation per line. Echo back the data participated in calculation so that the output file is readable. The following are some sample output and output.
Output Input
2/5 + 3/5 = 1 a 4 10 3 5
1/2 / 1/3 = 1 1/2 d 3 6 1 3
1/2 + 2.75 = 3 1/4 A 1 2 2.75
2/5
(1/2) / (1/3) = 1 1/2 d 1 2 1 3
1 1 1
or --- / --- = 1 --- d 1 2 1 3
2 3 2
2/5 is less than 4/7 c 2 5 4 7
You may choose either output style. Your output should always be formatted so it is easy to understand. Be sure to display the fraction number properly as shown, i.e., 3/2 should be displayed as 1 1/2, 0/3 should be 0, and 5/5 should be 1, etc.
Data in the data file following each type depend on the type. For example, if type is a, follow it should be four integers, if type is A, follow it should be two integers and one floating value. Make up your own data file to test your program.
When all the data in data file is processed, the program should provide a summary report showing how many fraction class objects were created.
What listed below are basic rules of fractional calculation in mathematics.
N1/D1 and N2/D2 are two fractional numbers, where N1 and N2 are the numerators, D1 and D2 are denominators.
Addition:
N1 N2 N1*D2 + N2*D1 1 3 1 * 5 + 3 * 2 11
----- + ----- = ----------------------- example: --- + --- = ---------------- = ----
D1 D2 D1*D2 2 5 2 * 5 10
Subtraction:
N1 N2 N1*D2 - N2*D1 1 3 1 * 5 - 3 * 2 -1
----- - ----- = ----------------------- example: --- - --- = ---------------- = ----
D1 D2 D1*D2 2 5 2 * 5 10
multiplication:
N1 N2 N1*N2 1 3 1 * 3 3
----- * ----- = -------------- example: --- * --- = ------- = ---
D1 D2 D1*D2 2 5 2 * 5 10
division:
N1 N2 N1*D2 1 3 1 * 5 5
----- / ----- = -------------- example: --- / --- = -------- = ---
D1 D2 D1*N2 2 5 2 * 3 6
The following explains how to simplify a fraction to its least term, where CF stands for common factor:
Simplification:
N *CF N 8 4 * 2 4
------------ = ----- example: ---- = ------- = ----
D * CF D 6 3 * 2 3
CFraction - numerator : int - denominator : int - count : int - simplify () : void + CFraction (int=0, int=1) + operator + (const CFraction&) const : CFraction + operator - (const CFraction&) const : CFraction + operator * (const CFraction&) const : CFraction + operator / (const CFraction&) const : CFraction + operator + (double) const : CFraction + operator - (double) const : CFraction + operator * (double) const : CFraction + operator / (double) const : CFraction + operator == (const CFraction&) const : bool + operator > (const CFraction&) const : bool + operator >(istream&, CFraction&) : istream& + operator (const CFraction&) const : bool + operator >(istream&, CFraction&) : istream& + operatorStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started