Advertisement
GrandtherAzaMarks

ДействияСДробями

Jan 24th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. class Fraction {
  7. public:
  8.     int num, denom; //numerator, denominator
  9.  
  10.     Fraction(int n, int d) {
  11.         num = n;
  12.         denom = d;
  13.     }
  14.  
  15.     void simp() { // simplifier
  16.         int a = num, b = denom;
  17.         while (b != 0) {
  18.             int c = a % b;
  19.             a = b;
  20.             b = c;
  21.         }
  22.         num = num / a;
  23.         denom = denom / a;
  24.     }
  25.  
  26.     void print() {
  27.         cout << num << " / " << denom << endl;
  28.     }
  29. };
  30.  
  31. void add(Fraction f1, Fraction f2) { // addition
  32.     int nod = 0;
  33.     Fraction f3(0, 0);
  34.  
  35.     f3.num = f1.num*f2.denom + f1.denom*f2.num;
  36.     f3.denom = f1.denom * f2.denom;
  37.  
  38.     f3.simp();
  39.    
  40.     f3.print();
  41. }
  42.  
  43. void subtr(Fraction f1, Fraction f2) { // subtraction
  44.     int nod = 0;
  45.     Fraction f3(0, 0);
  46.  
  47.     f3.num = f1.num*f2.denom - f1.denom*f2.num;
  48.     f3.denom = f1.denom * f2.denom;
  49.  
  50.     f3.simp();
  51.  
  52.     f3.print();
  53. }
  54.  
  55. void mult(Fraction f1, Fraction f2) { // multiplication
  56.     Fraction f3(0, 0);
  57.  
  58.     f3.num = f1.num*f2.num;
  59.     f3.denom = f1.denom*f2.denom;
  60.  
  61.     f3.simp();
  62.  
  63.     f3.print();
  64. }
  65.  
  66. void div(Fraction f1, Fraction f2) { // dividing
  67.     if (f1.denom == 0 || f2.denom == 0) cout << "Impossible" << endl;
  68.     else {
  69.         Fraction f3(0, 0);
  70.  
  71.         f3.num = f1.num * f2.denom;
  72.         f3.denom = f1.denom * f2.num;
  73.  
  74.         f3.simp();
  75.  
  76.         f3.print();
  77.     }
  78. }
  79.  
  80. int main()
  81. {
  82.     Fraction f1(4, 7), f2(1, 6);
  83.  
  84.     add(f1, f2);
  85.     subtr(f1, f2);
  86.     mult(f1, f2);
  87.     div(f1, f2);
  88.  
  89.     system("pause");
  90.     return 0;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement