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
|
//WIP trying to combine them
#include <iostream>
using namespace std;
double gcd (int, int);
int main (){
char op, slash, repeat;
int num1, den1, num2, den2, resNum1, resNum2, big, small, ans;
do{
cout << "Enter the first fraction: ";
cin >> num1 >> slash >> den1;
while (den1 == 0){
cout << "Denominators cannot be equal to zero. Please reenter entire fraction with valid input: ";
cin >> num1 >> slash >> den1;
};
cout << "Enter operation: ";
cin >> op;
cout << "Enter the second fraction: ";
cin>> num2 >> slash >> den2;
while (den2 == 0){
cout << "Denominators cannot be equal to zero. Please reenter entire fraction with valid input: ";
cin>> num2 >> slash >> den2;
};
switch (op){
case '+':
resNum1 = (num1*den2 + num2*den1);
resNum2 = (den1*den2);
if (resNum1 > resNum2){
big = num1;
small = num2;
}
else {
small = resNum1;
big = resNum2;
}
ans = gcd (small, big);
cout << resNum1/ans << "/" << resNum2/ans;
break;
case '-':
resNum1 = (num1*den2 - num2*den1);
resNum2 = (den1*den2);
if (resNum1 > resNum2){
big = num1;
small = num2;
}
else {
small = resNum1;
big = resNum2;
}
ans = gcd (small, big);
cout << resNum1/ans << "/" << resNum2/ans;
break;
case '/':
resNum1 = (num1*den2);
resNum2 = (num2*den1);
if (resNum1 > resNum2){
big = num1;
small = num2;
}
else {
small = resNum1;
big = resNum2;
}
ans = gcd (small, big);
cout << resNum1/ans << "/" << resNum2/ans;
break;
case '*':
resNum1 = (num1*num2);
resNum2 = (den1*den2);
if (resNum1 > resNum2){
big = num1;
small = num2;
}
else {
small = resNum1;
big = resNum2;
}
ans = gcd (small, big);
cout << resNum1/ans << "/" << resNum2/ans;
break;
default: break;
}
do{
cout << "\nContinue? (y/n): ";
cin >> repeat;
} while (repeat != 'y' && repeat != 'Y' && repeat != 'n' && repeat != 'N');
} while (repeat == 'y' || repeat == 'Y');
return 0;
}
double gcd (int small, int big){
int rem;
rem = big%small;
while (rem != 0){
small = rem;
rem = big%rem;
}
return (small);
}
|