Rafael Arce Nazario 4 years ago
commit
e44f547154
5 changed files with 89 additions and 0 deletions
  1. 2
    0
      Makefile
  2. 1
    0
      README.md
  3. 45
    0
      Rational.cpp
  4. 15
    0
      Rational.h
  5. 26
    0
      main.cpp

+ 2
- 0
Makefile View File

@@ -0,0 +1,2 @@
1
+all:
2
+	g++ -o main main.cpp Rational.cpp

+ 1
- 0
README.md View File

@@ -0,0 +1 @@
1
+### Rational class

+ 45
- 0
Rational.cpp View File

@@ -0,0 +1,45 @@
1
+#include <iostream>
2
+#include <algorithm> 
3
+#include "Rational.h"
4
+
5
+using namespace std;
6
+
7
+
8
+Rational::Rational() {
9
+  num = 0;
10
+  den = 1;
11
+}
12
+
13
+Rational::Rational(int n, int d) { 
14
+  num = n;
15
+  den = d;
16
+}
17
+
18
+void Rational::reduce() { 
19
+  int g = __gcd(num,den);
20
+  num /= g;
21
+  den /= g;
22
+}
23
+
24
+Rational Rational::sum(const Rational &b) const {
25
+  return Rational(num * b.den + den * b.num, den * b.den);
26
+} 
27
+
28
+bool Rational::gt(const Rational &b) const {
29
+/*
30
+  if (num * b.den > den * b.num) {
31
+    return true;
32
+  }
33
+  else {
34
+    return false;
35
+  }
36
+*/
37
+  return num * b.den > den * b.num;
38
+  
39
+}
40
+
41
+
42
+void Rational::display() const {
43
+  cout << num << " / " << den << endl;
44
+}
45
+

+ 15
- 0
Rational.h View File

@@ -0,0 +1,15 @@
1
+#include <iostream>
2
+
3
+using namespace std;
4
+
5
+class Rational {
6
+private:
7
+  int num, den;
8
+public:
9
+  Rational(); 
10
+  Rational(int n, int d);
11
+  void reduce();
12
+  Rational sum(const Rational &b) const;
13
+  bool gt(const Rational &b) const;
14
+  void display() const;
15
+};

+ 26
- 0
main.cpp View File

@@ -0,0 +1,26 @@
1
+#include <iostream>
2
+#include "Rational.h"
3
+
4
+using namespace std;
5
+
6
+int main() { 
7
+  Rational a(1,2);
8
+  Rational b(8,4);
9
+  Rational c = a.sum(b);
10
+ 
11
+
12
+
13
+  a.display();
14
+  b.display();
15
+  c.display();
16
+  c.reduce();
17
+  c.display();
18
+
19
+  a.sum(b).display();
20
+
21
+  a.sum(a).sum(b).sum(c).display();
22
+
23
+  cout << boolalpha << a.gt(b) << endl;
24
+  
25
+  return 0; 
26
+}