//Rational.h
#pragma once
#include<iostream>
class Rational
{
public:
friend std::istream &operator>>(std::istream &input, Rational &rational);
void friend addition(const Rational &rational1, const Rational &rational2);
Rational(int numerator = 1, int denominator = 2);
void setNumerator(int numerator);
void setDenominator(int denominator);
int getNumerator()const;
int getDenominator()const;
void printFloat();
void printFraction();
private:
struct
{
int numerator;
int denominator;
}number;
inline void simplify();
};
//Rational.cpp
#include "Rational.h"
#include<stdexcept>
#include<cstdlib>
#include<cassert>
using namespace std;
std::istream& operator>>(std::istream& input, Rational& rational)
{
char* strPtr = NULL;
input >> strPtr;
char** strPtr1_2 = NULL;
assert(strPtr != NULL);
rational.number.numerator = strtol(strPtr, strPtr1_2, 10);
char** strPtr2_2 = strPtr1_2 + 1;
rational.number.denominator = strtol(*strPtr2_2, NULL, 10);
return input;
}
void addition(const Rational& rational1, const Rational& rational2)
{
int numerator1 = rational1.number.numerator * rational2.number.denominator;
int numerator2 = rational2.number.numerator * rational1.number.denominator;
int numerator = numerator1 + numerator2;
int denominator = rational1.number.denominator * rational2.number.denominator;
Rational rational(numerator, denominator);
rational.printFraction();
}
Rational::Rational(int numerator, int denominator)
{
number.numerator = numerator;
setDenominator(denominator);
}
void Rational::setNumerator(int numerator)
{
number.numerator = numerator;
simplify();
}
void Rational::setDenominator(int denominator)
{
if (denominator == 0)
{
throw invalid_argument("The denominator can't be '0'");
}
else
{
number.denominator = denominator;
simplify();
}
}
int Rational::getNumerator() const
{
return number.numerator;
}
int Rational::getDenominator() const
{
return number.denominator;
}
void Rational::printFloat()
{
float result = (float)number.numerator / number.denominator;
cout << result << endl;
}
void Rational::printFraction()
{
cout << number.numerator << '/' << number.denominator << endl;
}
inline void Rational::simplify()
{
int a = number.denominator;
int b = number.numerator;
int c = a % b;
while (c != 0)
{
a = b;
b = c;
c = a % b;
}
number.denominator /= b;
number.numerator /= b;
}
//main.cpp
#include"Rational.h"
using namespace std;
int main()
{
Rational rational1(3, 9);
Rational rational2(1, 2);
cout << "The numerator of rational1 is " << rational1.getNumerator() << endl;
cout << "The denominator of rational1 is " << rational1.getDenominator() << endl;
cout << "Print the rational1 in float form:";
rational1.printFloat();
cout << "Print the rational1 in fraction form:";
rational1.printFraction();
cout << "\n\n" << endl;
cout << "The numerator of rational2 is " << rational2.getNumerator() << endl;
cout << "The denominator of rational2 is " << rational2.getDenominator() << endl;
cout << "Print the rational2 in float form:";
rational2.printFloat();
cout << "Print the rational2 in fraction form:";
rational2.printFraction();
cout << "\n\n" << endl;
cout << "The result of that rational1 add rational2 is:";
addition(rational1, rational2);
cout << "\n\n" << endl;
cin >> rational1;
cout << "The numerator of rational1 is " << rational1.getNumerator() << endl;
cout << "The denominator of rational1 is " << rational1.getDenominator() << endl;
cout << "Print the rational1 in float form:";
rational1.printFloat();
cout << "Print the rational1 in fraction form:";
rational1.printFraction();
cin >> rational2;
cout << "The numerator of rational2 is " << rational2.getNumerator() << endl;
cout << "The denominator of rational2 is " << rational2.getDenominator() << endl;
cout << "Print the rational2 in float form:";
rational2.printFloat();
cout << "Print the rational2 in fraction form:";
rational2.printFraction();
cout << "The result of that rational1 add rational2 is:";
addition(rational1, rational2);
}