C + + Fractional addition __c++

Source: Internet
Author: User

Use class knowledge to re-examine fractional addition

/*1. Scores are added, two fractions are 1/5 and 7/20, and they add up to 11/20.
The method is to first find out the LCM of the two fractional denominator, after the Tong, the sum of two molecules, and finally the numerator and denominator of the result fraction (if two of the result is 4/8, must be reduced to the simplest fraction of form 1/2), that is, the numerator and denominator of the gcd separate numerator and denominator. One way to find M and n gcd is to assign a small number of M and N to the variable k, and then remove M and n by the number (decrement) in {k,k-1,k-2,...,1}, and the first number to divide M and N is the GCD of M and N.
Assuming that the GCD of M and N are V, their LCM is m*n/v. Try to establish a fractional class fract to complete the function of adding two fractions. The specific requirements are as follows: (1) Private data members.
int num,den; num is numerator, Den is the denominator. (2) Public member function. fract (int a=0,int b=1): constructor, initialize numerator num, denominator den with A and B respectively. int ged (int m,int N): Find the GCD of M and N. This function is called by the Member Add () function ... Fract Add (fract f): Adds the parameter fraction f to the object itself, returning the fractional object after the reduction ...
void Show (): Displays a score on the screen in Num/den form. (3) in the main program to define two fractional objects F1 and F2, the initial value is 1/5 and 7/20, through the F1 Call member function add complete F1 and F2 add, the resulting score assigned to the object F3, show the Fractional object F3.
* * #include <iostream> using namespace std;
    Class Fract {private:int num, den; public:fract (Int,int);
    int ged (int, int);
    Fract Add (fract);
void Show ();

};
    Fract::fract (int a = 0,int b = 1) {num = A;
den = b;  int fract::ged (int m, int n) {for (int i = (m>n?m:n); I >= 2; i--) {if (m%i = 0 && n%i == 0) return i;
return 0;
    } fract Fract::add (fract f) {fract ll;
    Ll.den = Den*f.den;
    Ll.num = F.den*num + den*f.num;
    int oo = GED (Ll.den, Ll.num);
    Ll.den/= Oo;
    Ll.num/= Oo;
return ll;
    } void Fract::show () {cout<< "The result is" <<endl;
cout << num << "/" << den << Endl;
    int main () {fract F1 (1,5), F2 (7,20), F3;
    F3=f1.add (F2);
    F3.show ();
    System ("pause");
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.