The below program is to be coded in C++ UML Diagram: CFraction - numerator : int
ID: 3701033 • Letter: T
Question
The below program is to be coded in C++
UML Diagram:
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 < (const CFraction&) const : bool
+ operator >> (istream&, CFraction&) : istream&
+ operator << (ostream&, const CFraction&) : ostream&
+ getcount () : int
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 < 4/7 c 2 5 4 7
(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& + operatorExplanation / Answer
#pragma once
#include <fstream>
using namespace std;
class CFraction
{
int numerator;
int denominator;
static int count;
public:
void simplify();
CFraction( int num = 0, int den = 1 );
CFraction( double );
~CFraction();
CFraction operator +( const CFraction & obj ) const;
CFraction operator -( const CFraction & obj ) const;
CFraction operator *( const CFraction & obj ) const;
CFraction operator /( const CFraction & obj ) const;
CFraction operator +( double val ) const;
CFraction operator -( double val ) const;
CFraction operator *( double val ) const;
CFraction operator /( double val ) const;
bool operator == ( const CFraction& obj ) const;
bool operator > ( const CFraction& obj ) const;
bool operator < ( const CFraction& obj ) const;
int getcount();
friend istream& operator >> ( istream& in, const CFraction &obj );
friend ostream& operator << ( ostream& out, const CFraction &obj );
};
#include "stdafx.h"
#include "Fraction.h"
#include <string>
int CFraction::count = 0;
void CFraction::simplify()
{
for ( int i = denominator * numerator; i > 1; i-- ) {
if ( ( denominator % i == 0 ) && ( numerator % i == 0 ) ) {
denominator /= i;
numerator /= i;
}
}
}
CFraction::CFraction( int num, int den )
{
count++;
numerator = num;
denominator = den;
simplify();
}
CFraction::CFraction( double num)
{
count++;
string s = to_string( num );
int pos = s.find( '.' );
int decimals = s.length() - pos -1;
numerator = num* pow( 10, decimals );
denominator = pow( 10, decimals );
simplify();
}
CFraction::~CFraction()
{
count--;
}
CFraction CFraction::operator+( const CFraction & obj ) const
{
CFraction result;
result.numerator = numerator*obj.denominator + obj.numerator*denominator;
result.denominator = denominator*obj.denominator;
result.simplify();
return result;
}
CFraction CFraction::operator-( const CFraction & obj ) const
{
CFraction result;
result.numerator = numerator*obj.denominator - obj.numerator*denominator;
result.denominator = denominator*obj.denominator;
result.simplify();
return result;
}
CFraction CFraction::operator*( const CFraction & obj ) const
{
CFraction result;
result.numerator = numerator*obj.numerator;
result.denominator = denominator*obj.denominator;
result.simplify();
return result;
}
CFraction CFraction::operator/( const CFraction & obj ) const
{
CFraction result;
result.numerator = numerator*obj.denominator;
result.denominator = denominator*obj.numerator;
result.simplify();
return result;
}
CFraction CFraction::operator+( double val ) const
{
return double();
}
CFraction CFraction::operator-( double val ) const
{
return double();
}
CFraction CFraction::operator*( double val ) const
{
return double();
}
CFraction CFraction::operator/( double val ) const
{
return double();
}
bool CFraction::operator==( const CFraction & ) const
{
return false;
}
bool CFraction::operator>( const CFraction & obj ) const
{
double a = numerator / denominator;
double b = obj.numerator / obj.denominator;
return a > b;
}
bool CFraction::operator<( const CFraction & obj ) const
{
double a = numerator / denominator;
double b = obj.numerator / obj.denominator;
return a < b;
}
int CFraction::getcount()
{
return count;
}
istream & operator >> ( istream& in, const CFraction &obj )
{
return in;
// TODO: insert return statement here
}
ostream & operator<<( ostream& out, const CFraction &obj )
{
if (obj.numerator == obj.denominator)
return out << obj.numerator ;
return out << obj.numerator << "\" << obj.denominator;
// TODO: insert return statement here
}
// FractionCalc.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Fraction.h"
#include <iostream>
#include <fstream>
#include <sstream>
int main()
{
std::ifstream infile( "thefile.txt" );
std::string line;
int objectsCreated = 0;
while ( std::getline( infile, line ) ) {
std::istringstream iss( line );
char op;
string outputStr;
if ( !( iss >> op ) ) { break; } // error
int a, b, c, d;
float e;
CFraction f1, f2;
if ( op == 'a' || op == 's' || op == 'm' || op == 'd' || op =='c') {
iss >> a >> b >> c >> d;
f1 = CFraction( a, b );
f2 = CFraction( c, d );
}
else if ( op == 'A' || op == 'S' || op == 'M' || op == 'D' ) {
iss >> a >> b >> e;
f1 = CFraction( a, b );
f2 = CFraction( e );
}
CFraction f3;
bool cmp = false;
switch ( op ) {
case 'a':
case 'A':
cout << f1;
cout << "+";
cout << f2;
f3 = f1 + f2;
cout << "="<<f3;
break;
case 's':
case 'S':
cout << f1;
cout << "-";
cout << f2;
f3 = f1 - f2;
cout << "=" << f3;
break;
case 'm':
case 'M':
cout << f1;
cout << "*";
cout << f2;
f3 = f1 * f2;
cout << "=" << f3;
break;
case 'd':
case 'D':
cout << "("<<f1<<")";
cout << "\";
cout << "(" << f2 << ")";
f3 = f1 / f2;
cout << "=" << f3;
break;
case 'c':
cmp = f1 < f2;
cout << f1;
if ( cmp )
cout << " is less than ";
else
cout << " is greater than ";
cout << f2;
}
if ( f1.getcount() > objectsCreated )
objectsCreated = f1.getcount();
cout << endl;
}
cout << endl << "Total objects created are: " << objectsCreated<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.