gon2

fraction.cpp

Jan 29th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdexcept>
  3.  
  4. using namespace std;
  5.  
  6. class Fraction {
  7.     /*
  8.     Táknar almennt brot með teljara (e. numerator) og nefnara (e. denominator)
  9.     */
  10.    protected:
  11.     int num;
  12.     int den;
  13.  
  14.     int gcd(int n1, int n2) {
  15.         /*
  16.         Finnur stærsta samdeili heiltalnanna n1 og n2
  17.         */
  18.         if (n2 == 0) {
  19.             return n1;
  20.         } else {
  21.             return gcd(n2, n1 % n2);
  22.         }
  23.     }
  24.  
  25.     void reduce() {
  26.         /*
  27.         Styttir brotið svo að teljarinn og nefnarinn séu af lágmarksstærð
  28.         */
  29.         int n = this->num;
  30.         int d = this->den;
  31.         int divisor = this->gcd(n, d);
  32.         this->num = n / divisor;
  33.         this->den = d / divisor;
  34.     }
  35.  
  36.    public:
  37.     Fraction(int n, int d) {
  38.         this->num = n;
  39.         this->den = d;
  40.         if (d == 0) {
  41.             throw logic_error("Error, zero denominator!");
  42.         }
  43.         this->reduce();
  44.     }
  45.  
  46.     Fraction operator+(Fraction f) {
  47.         int n = this->num * f.den + f.num * this->den;
  48.         int d = this->den * f.den;
  49.         return Fraction(n, d);
  50.     }
  51.  
  52.     //búum til tilvik fyrir * virkjann:
  53.     Fraction operator*(Fraction f) {
  54.       int n = this->num * f.num;
  55.       int d = this->den * f.den;
  56.       return Fraction(n, d);
  57.     }
  58.  
  59.     // ostream veittur aðgangur að protected eiginleikum klasans
  60.     friend ostream& operator<<(std::ostream& os, Fraction f);
  61. };
  62.  
  63. ostream& operator<<(ostream& os, Fraction f) {
  64.     /*
  65.     Útskýrt hvernig ostream skal hegða sér þegar hann rekst á tilvik af Fraction
  66.     */
  67.     os << f.num << "/" << f.den;
  68.     return os;
  69. };
  70.  
  71. class MixedFraction : public Fraction {
  72.  
  73.    protected:
  74.      int heiltala;
  75.    public:
  76.     MixedFraction(int num, int den) : Fraction(num, den) {
  77.       /*
  78.         ef teljarinn er stærri en nefnarinn fáum við út heiltölu, teljarinn
  79.         verður svo afgangurinn af deilingunni. Ef teljari og nefnari eru jafnir
  80.         fáum við út 1.
  81.       */
  82.  
  83.       if (this->num > this->den) {
  84.         this->heiltala = this->num / this->den;
  85.         this->num = this->num % this->den;
  86.  
  87.       } else if (this->num == this->den) {
  88.         this->heiltala = 1;
  89.         this->num = 0;
  90.         this->den = 0;
  91.  
  92.       } else {
  93.         this->heiltala = 0;
  94.       }
  95.     };
  96.  
  97.     //búum til annan constructor sem kallar á þann fyrir ofan og bætir fyrsta
  98.     //inntakinu við heiltöluna
  99.     MixedFraction(int heil, int num, int den) : MixedFraction(num, den) {
  100.       this->heiltala += heil;
  101.     };
  102.  
  103.     friend ostream& operator<<(std::ostream& os, MixedFraction f);
  104. };
  105.  
  106. ostream& operator<<(ostream& os, MixedFraction f) {
  107.     //viljum ekki birta brotið ef það skilar núlli:
  108.     if (f.num == 0) {
  109.       os << f.heiltala;
  110.     //viljum ekki birta heiltöluna ef hún er 0 og brotið stærra en 0:
  111.     } else if (f.heiltala==0 && f.num>0) {
  112.       os << f.num << "/" << f.den;
  113.  
  114.     } else {
  115.       os << f.heiltala << " " << f.num << "/" << f.den;
  116.     }
  117.     return os;
  118. };
  119.  
  120. int main() {
  121.     cout << "Búum til og birtum brotið 4/3 sem Fraction: ";
  122.     cout << Fraction(4, 3) << endl;
  123.  
  124.     cout << "Margföldum saman Fraction-in 1/2 og 2/3: ";
  125.     cout << Fraction(1, 2) * Fraction(2, 3) << endl;
  126.  
  127.     cout << "Búum til og birtum brotið 1/4 sem MixedFraction: ";
  128.     cout << MixedFraction(1, 4) << endl;
  129.  
  130.     cout << "Búum til og birtum brotið 4/1 sem MixedFraction: ";
  131.     cout << MixedFraction(4, 1) << endl;
  132.  
  133.     cout << "Búum til og birtum brotið 0/2 sem MixedFraction: ";
  134.     cout << MixedFraction(0, 2) << endl;
  135.  
  136.     cout << "Búum til og birtum brotið 7/6 sem MixedFraction: ";
  137.     cout << MixedFraction(7, 6) << endl;
  138.  
  139.     cout << "Búum til og birtum brotið 1 1/3 sem MixedFraction: ";
  140.     cout << MixedFraction(1, 1, 3) << endl;
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment