jeudi 26 janvier 2017

How do implement LCD into this program?

I'm trying to add two fractions which prints out their total with their least common denominator. I found this formula online but I don't know how to put it into this program. If someone could suggest a place to put it and how to format it that would be fantastic.

int lcd(int a, int b)
{
    if (a == 0) return b;
    return lcd(b%a, a);
} (This is the LCD formula ^^^)

class Fraction { //Creates class Fraction

public:

Fraction(int n, int d) {
   setNum(n);
   setDem(d);

}
Fraction()
:Fraction(0, 1) {
}
void setNum(int n) {

 num = n;
  }
  void setDem(int d) {
      if (d != 0)
         denm = d;
    else denm = 1;

}
Fraction &add(Fraction& c) {

   int plus = num*c.denm + denm*c.num;
   int plus2 = denm*c.denm;

Fraction temp(plus, plus2);

return temp;
}

void print() {

   cout << num << " / " << denm;

} 

private: //Makes data members private

 int num;
 int denm;

 };
int main()
{

int num;
int denm;
int num2;
int denm2;
int plus;
int plus2;


cout << "Please enter the numerator and denominator of the first fraction: "        << endl; 
cin >> num >> denm; 

cout << "\n";

Fraction a(num, denm);
a.print();

cout << "\n";

cout << "Please enter the numerator and denominator of the second fraction: " <<    endl;
cin >> num2 >> denm2;

Fraction b(num2, denm2);
b.print();

cout << "\n";

Fraction c;

cout << "These added together equals: "; 

c = a.add(b);
c.print();


cout << "\n";
cin.get();

  }

Aucun commentaire:

Enregistrer un commentaire