Fractions & Calc

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<string>
class Fraction {
private:
int integer;
int nominator;
int denominator;
void simplify(){
/*if (nominator < 0){
integer *= -1;
nominator *= -1;
}*/
if (denominator < 0){
integer *= -1;
denominator *= -1;
}
if (nominator > denominator){
integer = nominator / denominator;
nominator %= denominator;
}
if (std::abs(nominator) < 2)return;
//std::cout << integer << " " << nominator << "/" << denominator << std::endl;
//std::cout << std::endl;
int gcd = getGCD(std::abs(nominator), denominator);
nominator /= gcd;
denominator /= gcd;
}
static int getGCD(int a, int b){
while (a != b)
if (a > b) a -= b; else b -= a;
return a;
}
public:
Fraction(){
}
Fraction(int integer, int nominator, int denominator) : integer(integer), nominator(nominator), denominator(denominator){
if (this->denominator == 0) std::cout << "Division by zero" << std::endl;
simplify();
}
Fraction(Fraction &other) :integer(other.getInteger()), nominator(other.getNominator()), denominator(other.getDenominator()){}
int getInteger() {
return integer;
}
int getNominator() {
return nominator;
}
int getDenominator() {
return denominator;
}
friend std::ostream &operator<<(std::ostream &stream, Fraction &fraction){
return stream << fraction.getInteger() << " " << fraction.getNominator() << "/" << fraction.getDenominator();
}
friend std::istream &operator>>(std::istream &input, Fraction &fraction){
input >> fraction.integer;
input >> fraction.nominator;
input >> fraction.denominator;
return input;
}
};
Fraction operator+(Fraction &a, Fraction &b)
{
int comInteger = a.getInteger() + b.getInteger();
int comNominator = a.getNominator()*b.getDenominator() + b.getNominator()*a.getDenominator();
int comDenominator = a.getDenominator() * b.getDenominator();
return Fraction(comInteger, comNominator, comDenominator);
}
Fraction operator-(Fraction &a){
return Fraction(-a.getInteger(), -a.getNominator(), a.getDenominator());
}
Fraction operator-(Fraction &a, Fraction &b){
return a + (-b);
}
Fraction operator*(Fraction &a, Fraction &b){
int aNominator = a.getInteger()*a.getDenominator() + a.getNominator();
int bNominator = b.getInteger()*b.getDenominator() + b.getNominator();
int comInteger = 0;
return Fraction(comInteger, aNominator*bNominator, a.getDenominator()*b.getDenominator());
}
Fraction operator/(Fraction &a, Fraction &b){
int aNominator = a.getInteger()*a.getDenominator() + a.getNominator();
int bNominator = b.getInteger()*b.getDenominator() + b.getNominator();
int comInteger = 0;
return Fraction(comInteger, aNominator*b.getDenominator(), a.getDenominator()*bNominator);
}
int main()
{
Fraction a, b;
std::cin >> a >> b;
std::cout << a * b << std::endl;
//std::cout << a / b << std::endl;
getchar();
getchar();
return 0;
}