-
Notifications
You must be signed in to change notification settings - Fork 0
/
project8.cpp
114 lines (102 loc) · 3.15 KB
/
project8.cpp
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
107
108
109
110
111
112
113
114
/*
* Keita Nonaka
* CS255:01
* 7th: pp 519-520 #11.10
* 3/29/2018
* c++ 17
*
* I created a rational number class to calculate rational number with overloading.
* There are two cpp files and one header file.
*/
#include "Rational_Overload.h"
#include <iostream>
using namespace std;
void printAll(Rational_Overload first, Rational_Overload second, Rational_Overload c, char sign){ // function
first.print();
cout << " " << sign << " ";
second.print();
cout << " = ";
c.print();
cout << " or ";
c.printFloat();
cout << endl;
}
void compare(Rational_Overload first, Rational_Overload second, char message[100]){
first.print();
cout << message;
second.print();
cout << endl;
}
int main(){
int a = 0;
int b = 0;
char again = 'n';
Rational_Overload c; // instantiate Rational object
do {
cout << "Enter the numerator of the first fraction: ";
cin >> a;
cout << "Enter the denominator of the first fraction: ";
cin >> b;
Rational_Overload first(a, b); // instantiate Rational object
cout << "First fraction: ";
first.print();
cout << " or ";
first.printFloat();
cout << "\nEnter the numerator of the second fraction: ";
cin >> a;
cout << "Enter the denominator of the second fraction: ";
cin >> b;
Rational_Overload second(a, b); // instantiate Rational object
cout << "Second fraction: ";
second.print();
cout << " or ";
second.printFloat();
cout << endl << endl;
c = first + second;
printAll(first, second, c, '+');
c = first - second;
printAll(first, second, c, '-');
c = first * second;
printAll(first, second, c, '*');
c = first / second;
printAll(first, second, c, '/');
c = (first - second) / (second * first);
cout << "(";
first.print();
cout << " - ";
second.print();
cout << ") / (";
second.print();
cout << " * ";
first.print();
cout << ") = ";
c.print();
cout << " or ";
c.printFloat();
cout << endl;
if(first > second) // greater
compare(first, second, " is greater than ");
if(first >= second) // greater or equal
compare(first, second, " is greater than or equal to ");
if(first < second) // less
compare(first, second, " is less than ");
if(first <= second) // less or equal
compare(first, second, " is less than or equal to ");
if(first == second) // equal
compare(first, second, " is equal to ");
if(first != second) // not equal
compare(first, second, " is not equal to ");
first.print();
cout << " negated is ";
c = -first; // negation
c.print();
cout << endl;
cout << "The reciprocal of ";
second.print();
cout << " is ";
c = !second; // reciprocal
c.print();
cout << "\nEnter a y/n to test more fractions: ";
cin >> again;
}while(again == 'y'); // loop if again is 'y'
}