Hi, I have a problem with my assignment in c++, i did first part but i have a problem with second part where I need to reduce fraction from part 1 and return it to main. I am beginner in c++ :D
Thank you
here is my assignment:
Write a program that reads a fraction, followed by a '+', '-', '*' or '/', followed by another fraction, like so: 1/2 + 1/4 (No spaces between the numerator and slash or the slash and the denominator). It then passes both fractions - numerator and denominator - and the character indicating the operation to be performed, to a function called calc() which calculates the result by adding, subtracting, multiplying or dividing the fractions depending on whether the character passed to it was '+', '-', '*' or '/', respectively, and assigns the resulting fraction's numerator and denominator to a pair of parameters which are used to pass these values back to main.
Have main pass the numerator and denominator of the resulting fraction to a reduce function which reduces the original fraction passed to it. The reduce function returns true if the fraction was modified (reduced) and false if not (e.g.: 3/4 cannot be reduced so it would return false indicating no change). The main function then prints the reduced fraction along with either "The result was reduced" or "The result was not reduced." depending on the return value of the reduce function.
Then, the main() function asks the user to continue or not, and if the answer is 'y' or 'Y', reads another expression such as the one shown above, and the above process continues. It stops when the user answers with a 'n' or 'N'. Have the program wait for a key to be pressed before ending.
Put your name, CSIT 839 and assignment number on top of your source code. Add comments where the code may not be very clear and to explain what variables are for what purpose such as resulting fraction, input fraction, etc.
The following is a sample interaction between the user and the program:
Enter a fraction expression: 1/2 + 1/4
3/4. The result was reduced.
More [y/n]: y
Enter a fraction expression: 1/2 * 1/4
1/8. The result was not reduced.
More [y/n]: y
Enter a fraction expression: 5/4 - 1/4
1/1. The result was reduced.
More [y/n]: n
Press any key to continue.
Hint: If n1/d1 and n2/d2 are two fractions,
n1/d1 + n2/d2 = n1*d2 + d1*n2 / d1*d2
n1/d1 - n2/d2 = n1*d2 - d1*n2 / d1*d2
n1/d1 * n2/d2 = n1*n2 / d1*d2
n1/d1 / n2/d2 = n1*d2 / d1*n2
To reduce a fraction, try dividing the larger of the numerator/denominator by the smaller of the two; if not divisible, try the next lower number until you reach 1. For example, if the fraction is 12/9, try dividing 12 by 9, if it were divisible, you'd be done, but since it's not you try 12 over 8, 7, 6, ..., 3; when you get to 3, they're both divisible so you divide both by 3 getting 4/3.
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
|
/*
Write a program that reads a fraction, followed by a '+', '-', '*' or '/', followed by another fraction, like so:
1/2 + 1/4 (No spaces between the numerator and slash or the slash and the denominator).
It then passes both fractions - numerator and denominator - and the character indicating the operation to be performed,
to a function called calc() which calculates the result by adding, subtracting, multiplying or dividing the fractions
depending on whether the character passed to it was '+', '-', '*' or '/', respectively,
and assigns the resulting fraction's numerator and denominator to a pair of parameters which are used to pass these
values back to main.
Have main pass the numerator and denominator of the resulting fraction to a reduce function which reduces
the original fraction passed to it. The reduce function returns true if the fraction was modified (reduced)
and false if not (e.g.: 3/4 cannot be reduced so it would return false indicating no change).
\The main function then prints the reduced fraction along with either
"The result was reduced" or "The result was not reduced." depending on the return value of the reduce function.
Then, the main() function asks the user to continue or not, and if the answer is 'y' or 'Y',
reads another expression such as the one shown above, and the above process continues.
It stops when the user answers with a 'n' or 'N'. Have the program wait for a key to be pressed before ending.
Put your name, CSIT 839 and assignment number on top of your source code.
Add comments where the code may not be very clear and to explain what variables are for what
purpose such as resulting fraction, input fraction, etc.*/
#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);
cout << num1 << "/" << den1;
return 0;
}
|
I SOLVED IT