In: Computer Science
Project 1 Fractional Number Class
Part-1 for Five groups of member functions:
Part-2 for the explanation for the behavior observed by running the provided test pattern:
six (6) operations in one statement vs. six (6) operations in six (6) statements.
PROJECT 1 Frac Class
The following information, located on Github m03, are provided as a starter.
Please follow the TODO List below to finish all Class definitions, and to expand the starter to exercise and verify all the Frac member methods.
What is the number Type?
In our daily life we use the Quantitative Numbers to represent the physical world, such as:
For example, a floating number
123.45 = 1.2345 × 10+2 (for 10-based)
The floating point number construction and operations are pretty complicated but using the floating point number type in C++ is pretty straight forward. Since all the commonly used floating number operators are defined (overloaded) to have the same look and feel like an integer data type.
What is a Fractional number?
The fractional numbers are figured out by two whole numbers (the fraction terms) that are separated by a horizontal line (the fraction line). The number above the line (the numerator) can be every whole number and the number below the line (the denominator) should be different from zero.
Here are some examples of some fractions: 3/4, 5/4,...
Project Requirement
The form and format the Fractional number to be used
In this project, We are only going to
(1) use two numbers: numerator over denominator to represent a fraction, no mixed numerals.
(2) allow the usage of the proper and improper fraction:
We are not going to use Mixed Fraction and Equivalent Fractions:
TODO List
To complete the definition of all Five (5) Required Frac class method groups:
1. Constructors: * you may combine the first 3 contructors below into one.
2. Necessary getter and setters
3. Support the following operators:
4. Type conversion operators, for Frac numbers to work with integers and fractional numbers:
int a = 1; float b = 2.2; Frac f, f2; f = b; f2 = f-a;
5. overload friend iostream insertion <<, and extraction >> operator
After successfully completed the definitions of the above 5 groups of methods:
After successfully implemented and executed your program:
The GitHub m03 is below:
Frac.h (header)
// Function Prototypes for Overloaded Stream Operators | |
// Forward declaration needs to be filled | |
// if multifiles are used, make sure to place the inclusion guard. | |
#include <iostream> | |
class Frac; | |
ostream &operator << (ostream &, const Frac &); | |
istream &operator >> (istream &, Frac &); | |
class Frac { | |
private: | |
int num, den; | |
long gcd(long a, long b) | |
Frac lowterms(Frac &f); | |
public: | |
Frac(); | |
Frac(string s); | |
Frac(int num_, int den_); | |
Frac(const Frac& rhs); | |
Frac operator=(const Frac& rhs); | |
// math + - * must be minimum term, i.e. no 2/8 | |
Frac operator + (Frac &rhs); | |
Frac operator - (Frac &rhs); | |
Frac operator * (Frac &rhs); | |
Frac operator / (Frac &rhs); | |
// increment ++ decrement -- | |
Frac operator++(); | |
Frac operator++(int); | |
Frac operator--(); | |
Frac operator--(int); | |
// comparators | |
bool operator == (Frac &f2); | |
bool operator != (Frac &f2); | |
bool operator < (Frac &f2); | |
bool operator > (Frac &rhs); | |
bool operator <= (Frac &f2); | |
bool operator >= (Frac &f2); | |
// overloading >> << stream operators | |
friend istream& operator>>(istream& strm, Frac& f); | |
friend ostream& operator<<(ostream& strm,const Frac& f) { | |
}; |
testFrac_starter.cpp (file)
///////////////////////////////////////////////////////////// | |
// This starter is a one-file all inclusive test appliation. | |
// This starter combines with the necessary Frac definition. | |
// Do not re-include the Frac.h | |
// If you prefer a multiple file approach, separte them cleanly. | |
///////////////////////////////////////////////////////////// | |
#include <iostream> | |
#include <vector> | |
#include <sstream> // stringstream | |
using namespace std; | |
class Frac; | |
ostream &operator<< (ostream &stm, Frac const &rhs); | |
class Frac { | |
long num; | |
long den; | |
public: | |
// Frac() { num=0; den=1; } | |
Frac() : num(0), den(1) {} | |
Frac(long n, long d) {num=n; den=d;} | |
Frac(const Frac &obj) {*this = obj;} | |
void operator=(const Frac &rhs) { | |
num = rhs.num; den = rhs.den; | |
} // Frac a = b; | |
// string constructor is challenging, worth 2 pts, try your best! | |
Frac(string s); | |
// math operators | |
Frac operator+(const Frac &rhs) { | |
Frac temp; | |
temp.num = num*rhs.den + rhs.num*den; | |
temp.den = den*rhs.den; | |
// need to apply lower term by / gcd here | |
return temp; | |
} | |
// postfix increment operator ++ -- | |
Frac operator++() { | |
num += den; | |
// lowterms(*this); | |
return *this; } | |
Frac operator--() { | |
num -= den; | |
// lowterms(*this); | |
return *this; } | |
// overload ostream insertion operator<< | |
friend ostream &operator<< (ostream &stm, Frac const &rhs) { | |
stm << rhs.num << "/" << rhs.den; } | |
}; | |
int main() { | |
Frac x(3,4); | |
Frac y(1,2); | |
cout << "\nCreated Frac x(3,4) as " << x; | |
cout << "\nCreated Frac y(1,2) as " << y; | |
// Turn on this one when you completed the definition of string constructor | |
// Frac s("6/7"); // passing a Frac number as a string. | |
// cout << "\nString constructed: s: " << s; | |
Frac z(x); | |
cout << "\nCopy constructed z as x: " << z; | |
Frac v = x + y; | |
cout << "\nx + y is: " << v; | |
cout << "\nPlease observe the outputs of identical commands\n" | |
<< " executed in one statement v. separated statements. \n"; | |
Frac f(5,6); | |
cout << f << " " // start | |
<< --f << " " | |
<< f << " " | |
<< ++f << " " | |
<< --f << " " | |
<< ++f << endl; // end of one statement | |
cout << f << " " ; | |
cout << --f << " " ; | |
cout << f << " " ; | |
cout << ++f << " " ; | |
cout << --f << " " ; | |
cout << ++f; | |
cout << "\nWhy the above output values are not the same?\n\n"; | |
} |
thank you
Note : I have done implementations for most of the functions.I have doubt regarding post and preincrementations of the fraction .I will browse and complete the remaining functions.Need some time.Thank u
______________________
// This starter is a one-file all inclusive test
appliation.
// This starter combines with the necessary Frac definition.
// Do not re-include the Frac.h
// If you prefer a multiple file approach, separte them
cleanly.
/////////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <sstream> // stringstream
using namespace std;
#include "Frac.h"
long Frac::gcd(long a, long b)
{
}
Frac Frac::lowterms(Frac &f)
{
}
Frac::Frac()
{
this->num=0;
this->den=1;
}
Frac::Frac(string s)
{
}
Frac::Frac(int num_, int den_)
{
this->num=num_;
this->den=den_;
}
Frac::Frac(const Frac& rhs)
{
}
Frac::Frac operator=(const Frac& rhs)
{
}
// math + - * must be minimum term, i.e. no 2/8
Frac::Frac operator+(Frac &rhs)
{
int a = num;
int b = den;
int c = f.num;
int d = f.den;
int sumnumer = (a * d + b * c);
int denom = (b * d);
Frac frac(sumnumer,denom);
return frac;
}
Frac::Frac operator-(Frac &rhs)
{
int a = num;
int b = den;
int c = f.num;
int d = f.den;
int sumnumer = (a * d - b * c);
int denom = (b * d);
Frac frac(sumnumer,denom);
return frac;
}
Frac::Frac operator*(Frac &rhs)
{
int a = num;
int b = den;
int c = f.num;
int d = f.den;
int mulnumer = (a * c);
int muldenom = (b * d);
Frac frac(mulnumer,muldenom);
return frac;
}
Frac::Frac operator/(Frac &rhs)
{
int a = num;
int b = den;
int c = f.num;
int d = f.den;
int divnumer = (a * d);
int divdenom = (c * b);
Frac frac(divnumer,divdenom);
return frac;
}
// increment ++ decrement --
Frac::Frac operator++()
{
}
Frac::Frac operator++(int)
{
}
Frac::Frac operator--()
{
}
Frac::Frac operator--(int)
{
}
// comparators
bool Frac::operator==(Frac &f2)
{
int a = this->num;
int b = this->den;
int c = f.num;
int d = f.den;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 == n2) && (d1 == d2))
return true;
else
return false;
}
bool Frac::operator!=(Frac &f2)
{
int a = this->num;
int b = this->den;
int c = f.num;
int d = f.den;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 != n2) || (d1 != d2))
return true;
else
return false;
}
bool Frac::operator<(Frac &f2)
{
int a = this->num;
int b = this->den;
int c = f.num;
int d = f.den;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) < (n2 / d2))
return true;
else
return false;
}
bool Frac::operator>(Frac &rhs)
{
int a = this->num;
int b = this->den;
int c = f.num;
int d = f.den;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
if ((n1 / d1) > (n2 / d2))
return true;
else
return false;
}
bool Frac::operator<=(Frac &f2)
{
int a = this->num;
int b = this->den;
int c = f.num;
int d = f.den;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) <= (n2 / d2))
return true;
else
return false;
}
bool Frac::operator>=(Frac &f2)
{
int a = this->num;
int b = this->den;
int c = f.num;
int d = f.den;
double n1 = a * d;
double d1 = b * d;
double n2 = c * b;
double d2 = d * b;
// cout<<f1<<" "<<f2<<endl;
if ((n1 / d1) >= (n2 / d2))
return true;
else
return false;
}
// overloading >> << stream operators
istream& operator>>(istream& strm, Frac& f)
{
}
ostream& operator<<(ostream& strm,const Frac&
f)
{
strm<< f.num << "/" << f.den;
return strm;
}
________________________