I've been working on this class for about 2-3 weeks and I got everything done except the reducing part. I can't figure it out , I know you have to use the GCD algorithm , but I cannot figure out how to use it :o , Can someone please tell me how to use it ? I know I have to use it in the reduce member but cannot figure out how to use it or how the syntax is supposed to be. Heres my Fraction.H , Fraction.cpp , and main.cpp files
// Fraction.H //
#include <iostream>
#include <fstream>
using namespace std;
class Fraction
{
public:
Fraction(void);
Fraction(int n, int d);
void plus(Fraction second);
void minus(Fraction second);
void times(Fraction second);
void divide(Fraction second);
void reduce();
double todecimal();
void scan(istream& in);
void print(ostream& out);
~Fraction();
private:
int num,den;
};
// Fraction.cpp
#include "Fraction.h"
Fraction::Fraction()
{
num=0;
den=1;
}
Fraction::Fraction(int n, int d){
num=n;
den=d;
}
void Fraction::plus(Fraction second){
int tempden= den*second.den;
num=num*second.den + den*second.num;
den=tempden;
}
void Fraction::minus(Fraction second){
int tempden= den*second.den;
num=num*second.den - den*second.num;
den = tempden;}
void Fraction::times(Fraction second){
num = num*second.num;
den=second.den*den;
}
void Fraction::divide(Fraction second){
num = num*second.den;
den= den*second.num;
}
void Fraction::scan(istream & in){
char slash;
in >> num >> slash >> den;
}
void Fraction::print(ostream & out){
out << num <<" / " << den;}
double Fraction::todecimal()
{double dec;
dec=(num*1.00)/(den*1.00);
return dec;
}
void Fraction::reduce()
{
}
Fraction::~Fraction()
{
}
//main.cpp
#include "Fraction.h"
using namespace std;
int main(){
Fraction first;
Fraction d(1,2);
cout << " Fraction class menu" << endl;
cout << " " << endl;
cout << " Please enter fraction in form of 1/2"<<endl;
first.scan(cin);
cout << " What would you like to do ? (fraction will automatically be reduced and given in decimals)"<< endl;
cout << "1. plus" << endl;
cout << "2. minus" << endl;
cout << "3. times"<<endl;
cout << "4. divide"<<endl;
cout << " Make your choice now" << endl;
int choice;
cin >> choice;
switch (choice)
{
case 1: {
cout << "Enter a second fraction to be added to";
d.scan(cin);
first.plus(d);
first.reduce();
cout << endl;
first.print(cout);
cout << endl << "Decimal is " << first.todecimal();
break;}
case 2: {
cout << " Enter a second fraction to be subtracted from";
d.scan(cin);
first.minus(d);
first.reduce();
cout << endl;
first.print(cout);
cout << endl << "Decimal is " << first.todecimal();
break;}
case 3: {
cout << "Enter a second fraction to be timed by" << endl;
d.scan(cin);
first.times(d);
first.reduce();
cout << endl;
first.print(cout);
cout << endl << "Decimal is " << first.todecimal();
break;}
case 4:{ cout << "Enter a second fraction to be divided by" <<endl;
d.scan(cin);
first.divide(d);
first.reduce();
cout << endl;
first.print(cout);
cout << endl << "Decimal is " << first.todecimal();
break;}
default: cout << "ERROR : Menu Choice";
break;
}
}
/* Sample output
Addition
// Fraction class menu
Please enter fraction in form of 1/2
1/2
What would you like to do ? (fraction will automatically be reduced and given i
n decimals)
1. plus
2. minus
3. times
4. divide
Make your choice now
1
Enter a second fraction to be added to1/2
4 / 4
Decimal is 1Press any key to continue
subtraction
Fraction class menu
Please enter fraction in form of 1/2
8/9
What would you like to do ? (fraction will automatically be reduced and given i
n decimals)
1. plus
2. minus
3. times
4. divide
Make your choice now
2
Enter a second fraction to be subtracted from8/9
0 / 81
Decimal is 0Press any key to continue
Times
Fraction class menu
Please enter fraction in form of 1/2
1/2
What would you like to do ? (fraction will automatically be reduced and given i
n decimals)
1. plus
2. minus
3. times
4. divide
Make your choice now
3
Enter a second fraction to be timed by
1/2
1 / 4
Decimal is 0.25Press any key to continue
divide
Fraction class menu
Please enter fraction in form of 1/2
1/2
What would you like to do ? (fraction will automatically be reduced and given i
n decimals)
1. plus
2. minus
3. times
4. divide
Make your choice now
4
Enter a second fraction to be divided by
1/2
2 / 2
Decimal is 1Press any key to continue
*/
Basically I am getting the right answers, just that the fractions do not reduce :( , thanks to anyone in advance that can help , really need help before friday because that is the last day for this semester :o