• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Fraction Math

zCool May 3rd, 2012 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <iostream>
2. #include <math.h>
3. using namespace std;
4.
5. int main()
6. {
7. int numeratorOne;
8. int denominatorOne;
9. int numeratorTwo;
10. int denominatorTwo;
11. int numeratorResult;
12. int denominatorResult;
13. int operation;
14.
15.
16. cout << "Enter the Numerator of the first fraction: ";
17. while(true)
18. {
19.         if(cin  >> numeratorOne)
20.         {
21.                 break;
22.         }
23.         else
24.         {
25.                 cin.clear();
26.                 cin.ignore();
27.                 cout << "Sorry I didn't get that, please enter the numerator of the first fraction: ";
28.         }
29. }
30.
31. cout << "Enter Denominator of the first fraction: ";
32. while(true)
33. {
34.         if(cin >> denominatorOne)
35.         {
36.                 if(denominatorOne == 0)
37.                 {
38.                         cout << "Sorry the denominator can not be zero, please enter a non-zero integer: ";
39.                 }
40.                 else
41.                 {
42.                         break;
43.                 }
44.         }
45.         else
46.         {
47.                 cin.clear();
48.                 cin.ignore();
49.                 cout << "Sorry I didn't get that, please enter the denominator of the first fraction: ";
50.         }
51. }
52.
53. cout << "Enter the Numerator of the second fraction: ";
54. while(true)
55. {
56.         if(cin  >> numeratorTwo)
57.         {
58.                 break;
59.         }
60.         else
61.         {
62.                 cin.clear();
63.                 cin.ignore();
64.                 cout << "Sorry I didn't get that, please enter the numerator of the second fraction: ";
65.         }
66. }
67.
68. cout << "Enter Denominator of the second fraction: ";
69. while(true)
70. {
71.         if(cin >> denominatorTwo)
72.         {
73.                 if(denominatorTwo == 0)
74.                 {
75.                         cout << "Sorry the denominator can not be zero, please enter a non-zero integer: ";
76.                 }
77.                 else
78.                 {
79.                         break;
80.                 }
81.         }
82.         else
83.         {
84.                 cin.clear();
85.                 cin.ignore();
86.                 cout << "Sorry I didn't get that, please enter the denominator of the second fraction: ";
87.         }
88. }
89.
90. cout << "Please select which operation you would like to do. "
92.          << "\n" << "2-Subtract"
93.          << "\n" << "3-Multiply"
94.          << "\n" << "4-Divide"
95.          << "\n" << "Select from the above list: ";
96.
97. while(true)
98. {
99.         if(cin >> operation)
100.         {
101.                 if(operation<1 || operation >4)
102.                 {
103.                         cout << "Sorry please select a number from 1-4: ";
104.                 }
105.                 else
106.                 {
107.                         break;
108.                 }
109.         }
110.         else
111.         {
112.                 cin.clear();
113.                 cin.ignore();
114.                 cout << "Sorry I didn't get that, please enter a number between 1-4: ";
115.         }
116. }
117.
119. if (operation == 1)
120. {
121. numeratorResult = (numeratorOne * denominatorTwo + numeratorTwo * denominatorOne);
122. denominatorResult = (numeratorOne * denominatorTwo);
123. cout << numeratorOne << "/" << denominatorOne
124.          << " + "
125.          << numeratorTwo << "/" << denominatorTwo
126.          << " = "
127.          << numeratorResult << "/" << denominatorResult << "\n";
128. }
129.
130. //Subtract
131. if (operation ==2)
132. {
133. numeratorResult = (numeratorOne * denominatorTwo - numeratorTwo * denominatorOne);
134. denominatorResult = (numeratorOne * denominatorTwo);
135. cout << numeratorOne << "/" << denominatorOne
136.          << " - "
137.          << numeratorTwo << "/" << denominatorTwo
138.          << " = "
139.          << numeratorResult << "/" << denominatorResult << "\n";
140. }
141.
142. //Multiply
143. if(operation == 3)
144. {
145. numeratorResult = numeratorOne * numeratorTwo;
146. denominatorResult = denominatorOne * denominatorTwo;
147. cout << numeratorOne << "/" << denominatorOne
148.          << " x "
149.          << numeratorTwo << "/" << denominatorTwo
150.          << " = "
151.          << numeratorResult << "/" << denominatorResult << "\n";
152. }
153.
154. //Divide
155. if(operation == 4)
156. {
157. numeratorResult = numeratorOne * denominatorTwo;
158. denominatorResult = denominatorOne * numeratorTwo;
159. cout << numeratorOne << "/" << denominatorOne
160.          << " / "
161.          << numeratorTwo << "/" << denominatorTwo
162.          << " = "
163.          << numeratorResult << "/" << denominatorResult << "\n";
164. }
165.
166. system("PAUSE");
167.
168. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?