Intro to Computer Programming 2 Class : C++ Please dumb the code down using inpu
ID: 3811795 • Letter: I
Question
Intro to Computer Programming 2 Class : C++
Please dumb the code down using input and output functions etc. Try not to use coding outside of this class scope and the requirements below. Also overload and please make the code easy and simple to access use comments if necessary. Thank You and I'll leave a like and comment so please be on the lookout afterwards
Question #1: Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. Represent rational numbers as two pointers to integers, one for the numerator and one for the denominator. Call the class Rational. Include a constructor with two arguments that can be used to set the member variables of an object to any legitimate values. Also, include a constructor that has only a single parameter of type int; call this single parameter whole number and define the constructor so that the object will be initialized to the rational number whole number/1. Also, include a default constructor that initializes an object to 0 (that is, to 0/1). Write input and output functions. Numbers are to be output in the form 1/2, 15/32, 300/401, and so forth. Note that the numerator, the denominator, or both may contain a minus sign, so-1/2, 15/- 32, and 300 401 are also possible inputs. Overload all of the following operators so that they correctly apply to the type Rational K, and Hints: Two rational numbers a/b and c/d are equal if a d equals c*b. If b and d are positive rational numbers, a/b is less than c d provided a d is less than c You should include a function to normalize the values stored so that, after normalization, the denominator is positive and the numerator and denominator are as small as possible. For example, after normalization 4/- 8 would be represented the same as 1/2Explanation / Answer
Here is the code along with the demo for you:
#include <iostream>
using namespace std;
class Fraction
{
private:
int numerator;
int denominator;
public:
Fraction()
{
numerator = 0;
denominator = 1;
}
Fraction(int num, int denom)
{
numerator = num;
denominator = denom;
}
void print()
{
cout<<numerator / denominator<< " " <<numerator % denominator <<" / "<<denominator;
}
//Arithmetic operators: + - * /
Fraction operator+(Fraction second)
{
Fraction temp;
temp.setNumerator(numerator*second.getDenominator() + denominator*second.getNumerator());
temp.setDenominator(denominator * second.getDenominator());
return temp.reduce();
}
Fraction operator-(Fraction second)
{
Fraction temp;
temp.setNumerator(numerator*second.getDenominator() - denominator*second.getNumerator());
temp.setDenominator(denominator * second.getDenominator());
return temp.reduce();
}
Fraction operator*(Fraction second)
{
Fraction temp;
temp.setNumerator(numerator * second.getNumerator());
temp.setDenominator(denominator * second.getDenominator());
return temp.reduce();
}
Fraction operator/(Fraction second)
{
Fraction temp;
temp.setNumerator(numerator * second.getDenominator());
temp.setDenominator(denominator * second.getNumerator());
return temp.reduce();
}
//Relational operators: < <= > >=
bool operator<(Fraction second)
{
return ((double)numerator / denominator) < ((double)second.getNumerator() / second.getDenominator());
}
bool operator<=(Fraction second)
{
return ((double)numerator / denominator) <= ((double)second.getNumerator() / second.getDenominator());
}
bool operator>(Fraction second)
{
return ((double)numerator / denominator) > ((double)second.getNumerator() / second.getDenominator());
}
bool operator>=(Fraction second)
{
return ((double)numerator / denominator) >= ((double)second.getNumerator() / second.getDenominator());
}
//Equality operators: == !=
bool operator==(Fraction second)
{
return ((double)numerator / denominator) == ((double)second.getNumerator() / second.getDenominator());
}
bool operator!=(Fraction second)
{
return ((double)numerator / denominator) != ((double)second.getNumerator() / second.getDenominator());
}
void setNumerator(int num)
{
numerator = num;
}
void setDenominator(int den)
{
denominator = den;
}
int getNumerator()
{
return numerator;
}
int getDenominator()
{
return denominator;
}
//Method to reduce fraction
int gcd() // returns the greatest common divisor of num and denom
{
int mn = min(); // min of num and denom
int mx = max(); // mX of num and denom
for (int x = mn; x > 0; x--) // find the greatest common divisor
if (mx % x == 0 && mn % x == 0) return x;
return 1;
}
Fraction reduce() // simplify the Fraction number
{
int tmp = gcd();
return Fraction(numerator / tmp, denominator / tmp);
}
int max() { return (numerator >= denominator) ? numerator : denominator; }
int min() { return (numerator >= denominator) ? denominator : numerator; }
//Stream Insertion: << (i.e. 3/7 form)
friend ostream &operator<<(ostream &, const Fraction &);
//Stream Extraction: >> (i.e. 3/7 form)
friend istream &operator>>(istream &, Fraction &);
};
ostream &operator<<( ostream &output, Fraction &c )
{
output<<c.getNumerator() <<" / "<<c.getDenominator();
return output;
}
istream &operator>>( istream &input, Fraction &c )
{
char ch;
input >> c.numerator >> ch >> c.denominator;
return input;
}
/*int main()
{
Fraction f3;
int choice, numerator, denominator, whole;
while(1)
{
cout<<"Enter whole part of first fraction: ";
cin>>whole;
cout<<"Enter the numerator of first fraction: ";
cin>>numerator;
cout<<"Enter the denominator of first fraction: ";
cin>>denominator;
//f1.setNumerator(numerator);
//f1.setDenominator(denominator);
Fraction f1 (whole, numerator, denominator);
cout<<"Enter whole part of second fraction: ";
cin>>whole;
cout<<"Enter the numerator of second fraction: ";
cin>>numerator;
cout<<"Enter the denominator of second fraction: ";
cin>>denominator;
//f2.setNumerator(numerator);
//f2.setDenominator(denominator);
Fraction f2 (whole, numerator, denominator);
cout<<"Enter the operation to perform: 1.Addition. 2. Subtraction. 3. Multiplication. 4. Division. 5. Exit."<<endl;
cout<<"Enter your choice: ";
cin>>choice;
switch(choice)
{
case 1: f3 = f1 + f2; break;
case 2: f3 = f1 - f2; break;
case 3: f3 = f1 * f2; break;
case 4: f3 = f1 / f2; break;
case 5: return 0;
}
f3.print();
}
}*/
int main() // make sure to simplify/reduce in operator >>
{
Fraction f1, f2, sum, diff, prod, quo;
cout<<"Enter the first fraction in the form numerator/denominator: ";
cin >> f1;
cout<<"Enter the second fraction in the form numerator/denominator: ";
cin >> f2;
cout << "first fraction is " << f1 << endl << endl;
cout << "second fraction is " << f2 << endl << endl;
sum = f1 + f2;
diff = f1 - f2;
prod = f1 * f2;
quo = f1 / f2;
cout <<"The sum is " << sum << endl << endl;
cout <<"The difference is " << diff << endl << endl;
cout <<"the product is " << prod << endl << endl;
cout <<"The quotient is " << quo << endl << endl;
if(f1 < f2)
cout<<f1 <<" < "<<f2<<endl;
else
cout<<f1<<" !< "<<f2<<endl;
if(f1 <= f2)
cout<<f1 <<" <= "<<f2<<endl;
else
cout<<f1<<" !<= "<<f2<<endl;
if(f1 > f2)
cout<<f1 <<" > "<<f2<<endl;
else
cout<<f1<<" !> "<<f2<<endl;
if(f1 >= f2)
cout<<f1 <<" >= "<<f2<<endl;
else
cout<<f1<<" !>= "<<f2<<endl;
if (f1 == f2)
cout << "fraction 1 equals fraction 2"<<endl;
else
cout << "fraction 1 does not equal fraction 2"<<endl;
if (f1 != f2)
cout << "fraction 1 does not equal faction 2"<<endl;
else
cout << "fraction 1 equals fraction 2"<<endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.