✔ 最佳答案
a)
To write a program calculating LCM, the simplest and the best way is to use Eucildean Algorithm:
http://hk.knowledge.yahoo.com/question/?qid=7006122601457
To calculate GCF, notice that for two integers a and b, |a*b| = LCM(a,b) * GCF(a,b)
This is easy to check using the method of finding LCM and GCF by factorization
b)
To add two fractions, first you must find the "common multiple" for the denominators
Then add two numinators, then simplify it:
Fraction Add(Fraction &a, Faction &b) {
int gcf = GCF(a.denominator, b.denominator)
int new_numinator = a. numinator*(gcf/a.denominator) + b.numinator*(gcf/b.denominator)
int lcm = LCM(gcf, new_numinator);
gcf = gcf / lcm;
new_numinator = new_numinator/lcm;
return new Fraction(new_numinator, gcf);
}