Humberto Ortiz-Zuazaga 5 jaren geleden
bovenliggende
commit
2f8989ed15
7 gewijzigde bestanden met toevoegingen van 64 en 21 verwijderingen
  1. 8
    2
      Makefile
  2. 34
    9
      Rational.cpp
  3. 9
    0
      Rational.h
  4. BIN
      Rational.o
  5. BIN
      main
  6. 13
    10
      main.cpp
  7. BIN
      main.o

+ 8
- 2
Makefile Bestand weergeven

@@ -1,2 +1,8 @@
1
-all:
2
-	g++ -o main main.cpp Rational.cpp
1
+all: main.o Rational.o
2
+	g++ -o main main.o Rational.o
3
+
4
+main.o: main.cpp
5
+	g++ -c main.cpp
6
+
7
+Rational.o: Rational.cpp
8
+	g++ -c Rational.cpp

+ 34
- 9
Rational.cpp Bestand weergeven

@@ -25,21 +25,46 @@ Rational Rational::sum(const Rational &b) const {
25 25
   return Rational(num * b.den + den * b.num, den * b.den);
26 26
 } 
27 27
 
28
+Rational Rational::operator+(const Rational &b) const {
29
+  return Rational(num * b.den + den * b.num, den * b.den);
30
+} 
31
+
32
+double Rational::operator+(double b) const {
33
+  return real() + b; 
34
+}
35
+
36
+Rational Rational::operator*(const Rational &b) const {
37
+  return Rational(num *  b.num, den * b.den);
38
+} 
39
+
40
+
28 41
 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 42
   return num * b.den > den * b.num;
38
-  
39 43
 }
40 44
 
45
+bool Rational::operator>(const Rational &b) const {
46
+  return num * b.den > den * b.num;
47
+}
48
+
49
+
41 50
 
42 51
 void Rational::display() const {
43 52
   cout << num << " / " << den << endl;
44 53
 }
45 54
 
55
+
56
+double Rational::real() const {
57
+  return static_cast<double>(num) / den;
58
+}
59
+
60
+
61
+double operator+(double a, const Rational &b) {
62
+    return a + b.real();
63
+}
64
+
65
+
66
+
67
+
68
+
69
+
70
+

+ 9
- 0
Rational.h Bestand weergeven

@@ -10,6 +10,15 @@ public:
10 10
   Rational(int n, int d);
11 11
   void reduce();
12 12
   Rational sum(const Rational &b) const;
13
+  Rational operator+(const Rational &b) const;
14
+
15
+  double operator+(double b) const;
16
+
17
+  Rational operator*(const Rational &b) const;
13 18
   bool gt(const Rational &b) const;
19
+  bool operator>(const Rational &b) const;
14 20
   void display() const;
21
+  double real() const;
15 22
 };
23
+
24
+double operator+(double a, const Rational &b);

BIN
Rational.o Bestand weergeven



+ 13
- 10
main.cpp Bestand weergeven

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

BIN
main.o Bestand weergeven