-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject11.cpp
147 lines (130 loc) · 4.11 KB
/
project11.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* Keita Nonaka
* CS255:01
* 5/1/2018
* c++ 17
*
* I created a rational number class to calculate rational number with overloading.
* If divided by 0, throw exception.
*/
#include "Rational_Overload.h"
#include <iostream>
using namespace std;
void printAll(Rational_Overload c, char sign){ // function
if(!(c.getNumer() == 0 && sign == '/')) {
c.print();
cout << " or ";
c.printFloat();
cout << endl;
}
}
void compare(Rational_Overload first, Rational_Overload second, string msg){
first.print();
cout << msg;
second.print();
cout << endl;
}
int main(){
int a = 0;
int b = 0;
char again = 'n';
Rational_Overload c; // instantiate Rational object
do {
cout << "Enter information for Rational a" << endl;
cout << "Enter numerator: ";
cin >> a;
do {
try { // try catch
cout << "Enter denominator: ";
cin >> b;
if (b == 0)
throw invalid_argument("Cannot set denominator to be 0. Please try again");
} catch(exception& e) {
cout << e.what() << endl;
}
}while(b == 0);
Rational_Overload first(a, b); // instantiate Rational object
cout << "Enter information for Rational b" << endl;
cout << "Enter numerator: ";
cin >> a;
do{
try{
cout << "Enter denominator: ";
cin >> b;
if (b == 0)
throw invalid_argument("Cannot set denominator to be 0. Please try again");
}catch (exception& e){
cout << e.what() << endl;
}
}while (b == 0);
Rational_Overload second(a, b); // instantiate Rational object
cout << endl;
cout << "a is ";
first.print();
cout << endl;
cout << "b is ";
second.print();
cout << endl << endl;
cout << "a+b is: ";
c = first + second;
printAll(c, '+');
cout << "a-b is: ";
c = first - second;
printAll(c, '-');
cout << "a*b is: ";
c = first * second;
printAll(c, '*');
cout << "a/b is: ";
try {
if(second.getNumer() == 0)
throw overflow_error("Division results in denominator of 0.");
c = first / second;
printAll(c, '/');
}catch (exception& e){
cout << e.what() << endl;
}
cout << "(a+b)/(a-b) is: ";
try{
if((first - second).getNumer() == 0)
throw overflow_error("Operation results in denominator of 0.");
c = (first + second) / (first - second);
c.print();
cout << " or ";
c.printFloat();
cout << endl;
}catch (exception& e){
cout << e.what() << endl;
}
cout << "-a is: ";
c = -first; // negation
c.print();
cout << " or ";
c.printFloat();
cout << endl;
cout << "!b (reciprocal of b) is: ";
try{
if (c.getNumer() == 0)
throw overflow_error("Operation results in denominator of 0.");
c = !second; // reciprocal
c.print();
cout << " or ";
c.printFloat();
}catch(exception& e) {
cout << e.what() << 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 ");
cout << "\nEnter a y/n to test more fractions: ";
cin >> again;
}while(again == 'y'); // loop if again is 'y'
}