SHOW:
|
|
- or go back to the newest paste.
1 | /** | |
2 | - | #include <math.h> |
2 | + | * Fraction calculator |
3 | - | using namespace std; |
3 | + | * Initial version by 'zer0c00l' - http://pastebin.com/m7PaahKr |
4 | * This version: Constantin 'morpha' Köpplinger | |
5 | * WaitForEnter() based on WaitForEnter() by Rachel 'Moosader' Morris | |
6 | */ | |
7 | - | int numeratorOne; |
7 | + | |
8 | - | int denominatorOne; |
8 | + | #include <string> |
9 | - | int numeratorTwo; |
9 | + | #include <functional> |
10 | - | int denominatorTwo; |
10 | + | |
11 | - | int numeratorResult; |
11 | + | int GCD(int n, int d, unsigned int max_depth = 1000) |
12 | - | int denominatorResult; |
12 | + | |
13 | - | int operation; |
13 | + | if(n == 0) |
14 | { | |
15 | return d; | |
16 | - | cout << "Enter the Numerator of the first fraction: "; |
16 | + | |
17 | - | while(true) |
17 | + | if(--max_depth == 0) |
18 | { | |
19 | - | if(cin >> numeratorOne) |
19 | + | return 0; |
20 | } | |
21 | return GCD(d % n, n, max_depth); | |
22 | } | |
23 | - | else |
23 | + | |
24 | void reduce(int &num, int &den) | |
25 | - | cin.clear(); |
25 | + | |
26 | - | cin.ignore(); |
26 | + | if(int gcd = GCD(num, den)) |
27 | - | cout << "Sorry I didn't get that, please enter the numerator of the first fraction: "; |
27 | + | |
28 | num /= gcd; | |
29 | den /= gcd; | |
30 | } | |
31 | - | cout << "Enter Denominator of the first fraction: "; |
31 | + | |
32 | - | while(true) |
32 | + | |
33 | template <typename TYPE> | |
34 | - | if(cin >> denominatorOne) |
34 | + | void input(TYPE &to, std::function<bool (TYPE data, bool valid_input)> validator) |
35 | { | |
36 | - | if(denominatorOne == 0) |
36 | + | do |
37 | { | |
38 | - | cout << "Sorry the denominator can not be zero, please enter a non-zero integer: "; |
38 | + | std::cin.sync(); |
39 | std::cin.clear(); | |
40 | - | else |
40 | + | std::cin >> to; |
41 | } while(std::cin.fail() || !validator(to, std::cin.good())); | |
42 | - | break; |
42 | + | |
43 | ||
44 | template <typename TYPE> | |
45 | - | else |
45 | + | void input(TYPE &to, std::string const &onError) |
46 | { | |
47 | - | cin.clear(); |
47 | + | do |
48 | - | cin.ignore(); |
48 | + | |
49 | - | cout << "Sorry I didn't get that, please enter the denominator of the first fraction: "; |
49 | + | std::cin.sync(); |
50 | std::cin.clear(); | |
51 | std::cin >> to; | |
52 | if(std::cin.fail()) | |
53 | - | cout << "Enter the Numerator of the second fraction: "; |
53 | + | |
54 | - | while(true) |
54 | + | std::cout << onError; |
55 | continue; | |
56 | - | if(cin >> numeratorTwo) |
56 | + | |
57 | } while(false); | |
58 | } | |
59 | ||
60 | - | else |
60 | + | void WaitForEnter() |
61 | { | |
62 | - | cin.clear(); |
62 | + | std::cin.sync(); |
63 | - | cin.ignore(); |
63 | + | std::cin.clear(); |
64 | - | cout << "Sorry I didn't get that, please enter the numerator of the second fraction: "; |
64 | + | while(std::cin.get() != '\n'){} |
65 | } | |
66 | ||
67 | int main() | |
68 | - | cout << "Enter Denominator of the second fraction: "; |
68 | + | |
69 | - | while(true) |
69 | + | int numeratorOne; |
70 | int denominatorOne; | |
71 | - | if(cin >> denominatorTwo) |
71 | + | int numeratorTwo; |
72 | int denominatorTwo; | |
73 | - | if(denominatorTwo == 0) |
73 | + | int numeratorResult; |
74 | int denominatorResult; | |
75 | - | cout << "Sorry the denominator can not be zero, please enter a non-zero integer: "; |
75 | + | char operation; |
76 | ||
77 | - | else |
77 | + | |
78 | std::cout << "Enter the Numerator of the first fraction: "; | |
79 | - | break; |
79 | + | input<int>(numeratorOne, std::string("Sorry I didn't get that, please enter the numerator of the first fraction: ")); |
80 | ||
81 | std::cout << "Enter Denominator of the first fraction: "; | |
82 | - | else |
82 | + | input<int>(denominatorOne, [](int denom, bool valid) -> bool |
83 | { | |
84 | - | cin.clear(); |
84 | + | if(!valid) |
85 | - | cin.ignore(); |
85 | + | |
86 | - | cout << "Sorry I didn't get that, please enter the denominator of the second fraction: "; |
86 | + | std::cout << "Sorry I didn't get that, please enter the denominator of the first fraction: "; |
87 | return false; | |
88 | } | |
89 | if(denom == 0) | |
90 | - | cout << "Please select which operation you would like to do. " |
90 | + | |
91 | - | << "\n" << "1-Add" |
91 | + | std::cout << "Sorry the denominator can not be zero, please enter a non-zero integer: "; |
92 | - | << "\n" << "2-Subtract" |
92 | + | return false; |
93 | - | << "\n" << "3-Multiply" |
93 | + | |
94 | - | << "\n" << "4-Divide" |
94 | + | return true; |
95 | - | << "\n" << "Select from the above list: "; |
95 | + | }); |
96 | ||
97 | - | while(true) |
97 | + | std::cout << "Enter the Numerator of the second fraction: "; |
98 | input<int>(numeratorTwo, std::string("Sorry I didn't get that, please enter the numerator of the second fraction: ")); | |
99 | - | if(cin >> operation) |
99 | + | |
100 | std::cout << "Enter Denominator of the second fraction: "; | |
101 | - | if(operation<1 || operation >4) |
101 | + | input<int>(denominatorTwo, [](int denom, bool valid) -> bool |
102 | { | |
103 | - | cout << "Sorry please select a number from 1-4: "; |
103 | + | if(!valid) |
104 | { | |
105 | - | else |
105 | + | std::cout << "Sorry I didn't get that, please enter the denominator of the second fraction: "; |
106 | return false; | |
107 | - | break; |
107 | + | |
108 | if(denom == 0) | |
109 | { | |
110 | - | else |
110 | + | std::cout << "Sorry the denominator can not be zero, please enter a non-zero integer: "; |
111 | return false; | |
112 | - | cin.clear(); |
112 | + | |
113 | - | cin.ignore(); |
113 | + | return true; |
114 | - | cout << "Sorry I didn't get that, please enter a number between 1-4: "; |
114 | + | }); |
115 | ||
116 | std::cout << "Please select which operation you would like to do. " | |
117 | << "\n" << "1 - Add [+]" | |
118 | - | //Add |
118 | + | << "\n" << "2 - Subtract [-]" |
119 | - | if (operation == 1) |
119 | + | << "\n" << "3 - Multiply [*, x]" |
120 | << "\n" << "4 - Divide [/, :]" | |
121 | - | numeratorResult = (numeratorOne * denominatorTwo + numeratorTwo * denominatorOne); |
121 | + | << "\n" << "Select from the above list: "; |
122 | - | denominatorResult = (numeratorOne * denominatorTwo); |
122 | + | |
123 | - | cout << numeratorOne << "/" << denominatorOne |
123 | + | input<char>(operation, [](char denom, bool valid) -> bool |
124 | - | << " + " |
124 | + | |
125 | - | << numeratorTwo << "/" << denominatorTwo |
125 | + | if(!valid) |
126 | - | << " = " |
126 | + | |
127 | - | << numeratorResult << "/" << denominatorResult << "\n"; |
127 | + | std::cout << "Sorry please select a number from 1-4 (or the appropriate sign): "; |
128 | return false; | |
129 | } | |
130 | - | //Subtract |
130 | + | if(std::string("1234+-/*:x").find(denom) == std::string::npos) |
131 | - | if (operation ==2) |
131 | + | |
132 | std::cout << "Sorry I didn't get that, please select a number from 1-4 (or the appropriate symbol): "; | |
133 | - | numeratorResult = (numeratorOne * denominatorTwo - numeratorTwo * denominatorOne); |
133 | + | return false; |
134 | - | denominatorResult = (numeratorOne * denominatorTwo); |
134 | + | |
135 | - | cout << numeratorOne << "/" << denominatorOne |
135 | + | return true; |
136 | - | << " - " |
136 | + | }); |
137 | - | << numeratorTwo << "/" << denominatorTwo |
137 | + | |
138 | - | << " = " |
138 | + | switch(operation) |
139 | - | << numeratorResult << "/" << denominatorResult << "\n"; |
139 | + | |
140 | //Add | |
141 | case '1': | |
142 | - | //Multiply |
142 | + | operation = '+'; |
143 | - | if(operation == 3) |
143 | + | case '+': |
144 | numeratorResult = ((numeratorOne * denominatorTwo) + (numeratorTwo * denominatorOne)); | |
145 | - | numeratorResult = numeratorOne * numeratorTwo; |
145 | + | denominatorResult = (denominatorOne * denominatorTwo); |
146 | - | denominatorResult = denominatorOne * denominatorTwo; |
146 | + | |
147 | - | cout << numeratorOne << "/" << denominatorOne |
147 | + | |
148 | - | << " x " |
148 | + | //Subtract |
149 | - | << numeratorTwo << "/" << denominatorTwo |
149 | + | case '2': |
150 | - | << " = " |
150 | + | operation = '-'; |
151 | - | << numeratorResult << "/" << denominatorResult << "\n"; |
151 | + | case '-': |
152 | numeratorResult = ((numeratorOne * denominatorTwo) - (numeratorTwo * denominatorOne)); | |
153 | denominatorResult = (denominatorOne * denominatorTwo); | |
154 | - | //Divide |
154 | + | |
155 | - | if(operation == 4) |
155 | + | |
156 | //Multiply | |
157 | - | numeratorResult = numeratorOne * denominatorTwo; |
157 | + | case '3': |
158 | - | denominatorResult = denominatorOne * numeratorTwo; |
158 | + | operation = 'x'; |
159 | - | cout << numeratorOne << "/" << denominatorOne |
159 | + | case '*': |
160 | - | << " / " |
160 | + | case 'x': |
161 | - | << numeratorTwo << "/" << denominatorTwo |
161 | + | numeratorResult = (numeratorOne * numeratorTwo); |
162 | - | << " = " |
162 | + | denominatorResult = (denominatorOne * denominatorTwo); |
163 | - | << numeratorResult << "/" << denominatorResult << "\n"; |
163 | + | |
164 | ||
165 | //Divide | |
166 | - | system("PAUSE"); |
166 | + | case '4': |
167 | operation = '/'; | |
168 | case '/': | |
169 | case ':': | |
170 | numeratorResult = numeratorOne * denominatorTwo; | |
171 | denominatorResult = denominatorOne * numeratorTwo; | |
172 | break; | |
173 | } | |
174 | ||
175 | reduce(numeratorResult, denominatorResult); | |
176 | ||
177 | std::cout << numeratorOne << "/" << denominatorOne | |
178 | << " " << operation << " " | |
179 | << numeratorTwo << "/" << denominatorTwo | |
180 | << " = " | |
181 | << numeratorResult << "/" << denominatorResult << "\n"; | |
182 | ||
183 | std::cout << "Press RETURN to quit."; | |
184 | WaitForEnter(); | |
185 | ||
186 | return 0; | |
187 | } |