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
|
#include <iostream>
using namespace std;
void calc (int c_num1, int c_den1, char c_op, int c_num2, int c_den2, int &c_num, int &c_den)
{
switch(c_op){
case '+':
c_num = c_num1*c_den2 + c_den1*c_num2;
c_den = c_den1*c_den2;
break;
case '-':
c_num = c_num1*c_den2 - c_den1*c_num2;
c_den = c_den1*c_den2;
break;
case '*':
c_num = c_num1*c_num2;
c_den = c_den1*c_den2;
break;
case '/':
c_num = c_num1*c_den2;
c_den = c_den1*c_den2;
break;}
}
int reduce(int r_num1, int r_den1, double &res1, double &res2)
{
if (r_num1 >= r_den1){
//res = r_num1 / r_den1;
//if (res == (int) res)
// return true;
//else
for(int x = r_den1; x >= 2; x--){
res1 = r_num1 / x;
res2 = r_den1 / x;
if (res1 == (int)res1 && res2 == (int)res2)
return true;
else
x--;}}
else{
for(int x = r_num1; x >= 2; x--){
res1 = r_num1 / x;
res2 = r_den1 / x;
if (res1 == (int)res1 && res2 == (int)res2)
return true;
else
x--;}}
}
int main()
{
int n1, d1, n2, d2, num1, den1;
char slash1, slash2, oper;
cout << "Enter a fraction expression: ";
cin >> n1 >> slash1 >> d1 >> oper >> n2 >> slash2 >> d2;
calc(n1, d1,oper, n2, d2, num1, den1);
reduce(num1, den1); //This is where im getting the error!
cout << num1 << "/" << den1;
return 0;
}
|