-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_rational.cpp
143 lines (130 loc) · 3 KB
/
test_rational.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
/*
Mar 6, 2020
Mizuki Hashimoto
7th edition p427 Ex9.6
This is a program to test the function of Rational class.
Receives two rational numbers from the user and perform addition, subtraction, multiplication, and division.
For each operation, returns the value in num/den form and d.dd form (ex. 1/2 and 0.5)
*/
#include "rational.h" // include definition of Rational class from rational.h
#include <iostream>
using namespace std;
int main() {
int n, d; // numerator, denominator
Rational temp; // temporally variable for storing the original rational value
char ans; // user answer for yes/no question
do {
cout << "Enter a rational number separating the numerator and denominator with a space: ";
cin >> n >> d;
Rational r(n, d); // instantiate object r of Rational class
cout << "Enter another rational number: ";
cin >> n >> d;
Rational s(n, d); // instantiate object s of Rational class
// addition num/den form
temp = r;
r.print();
cout << " + ";
s.print();
cout << " = ";
r.add(s);
r.print();
cout << endl;
r = temp;
// addition d.dd form
temp = r;
r.printf();
cout << " + ";
s.printf();
cout << " = ";
r.add(s);
r.printf();
cout << endl;
r = temp;
// subtraction num/den form
temp = r;
r.print();
cout << " - ";
s.print();
cout << " = ";
r.sub(s);
r.print();
cout << endl;
r = temp;
// subtraction d.dd form
temp = r;
r.printf();
cout << " - ";
s.printf();
cout << " = ";
r.sub(s);
r.printf();
cout << endl;
r = temp;
// multiplication num/den form
temp = r;
r.print();
cout << " * ";
s.print();
cout << " = ";
r.mul(s);
r.print();
cout << endl;
r = temp;
// multiplication d.dd form
temp = r;
r.printf();
cout << " * ";
s.printf();
cout << " = ";
r.mul(s);
r.printf();
cout << endl;
r = temp;
// division num/den form
temp = r;
r.print();
cout << " / ";
s.print();
cout << " = ";
r.div(s);
r.print();
cout << endl;
r = temp;
// division d.dd form
temp = r;
r.printf();
cout << " / ";
s.printf();
cout << " = ";
r.div(s);
r.printf();
cout << endl;
r = temp;
cout << "Would you like to enter new rational numbers? (y/n): ";
cin >> ans;
} while(ans == 'y'); // loop if user enter y
}
/*
Enter a rational number separating the numerator and denominator with a space: 2 4
Enter another rational number: 4 10
1/2 + 2/5 = 9/10
0.5 + 0.4 = 0.9
1/2 - 2/5 = 1/10
0.5 - 0.4 = 0.1
1/2 * 2/5 = 1/5
0.5 * 0.4 = 0.2
1/2 / 2/5 = 5/4
0.5 / 0.4 = 1.25
Would you like to enter new rational numbers? (y/n): y
Enter a rational number separating the numerator and denominator with a space: 0 -4
Enter another rational number: 5 -5
0/1 + -1/1 = -1/1
0 + -1 = -1
0/1 - -1/1 = 1/1
0 - -1 = 1
0/1 * -1/1 = 0/1
0 * -1 = 0
0/1 / -1/1 = 0/1
0 / -1 = 0
Would you like to enter new rational numbers? (y/n): n
*/