Project 2-The prototype of the fractional class

Source: Internet
Author: User

A variety of basic data types are available in C + +. In fact, these are far from satisfying our needs, such as the plural (most of the examples in chapter 10th are dealing with complex numbers), as well as fractions. We can customize the classes to support these data types.
This task will design a simple fractional class that completes several operations on fractions. A method to consolidate object-based programming, and two to accumulate some perceptual knowledge for operator overloading.

The declaration of the fractional class is:

Class Cfraction{private:    int nume;  Molecular    int Deno;  Denominator public:    cfraction (int nu=0,int de=1);   constructor, initialized with    void set (int nu=0,int de=1);    Value, change the value with    void input (),//Enter void simplify () in the form "nu/de", such as "5/2"    ;//simplify (so that numerator denominator has no common factor)    void Amplify (int n);//magnify n times, such as 2/3 magnification 5 times times 10/3    void output (int style=0);//output: Take 8/6 For example, style is 0 o'clock, as-is output 8/6;//style is 1 o'clock, the output is reduced after the form of 4/3;// Style is 2 o'clock, Output 1 (1/3) Form, represents one and One-third;//style is 3 o'clock, output in decimal form, such as 1.3333;//default mode 0};

Project requirements: Complete the design of the fractional class, and define the object in the main () function, call each member function, complete the basic test.
Practice Strategy:
(1) It is not recommended to implement all member functions once and then debug, but to implement one, test one;
(2) The implementation of this project and the test sequence can be: the first constructor and output function (can be implemented only one output mode), then set function, then input function, and so on.
In the future we will be able to:
(1) An int i,j is defined, and the values of I and J can be entered on the keyboard with cin>>i>>j;. Later, the definition of cfraction C1, c2, can be used cin>>c1>>c2; input fractions, with cout<<c1<<c2; output fractions.
(2) I+j complete the two integer number of the addition, we can define the member function, with C1+C2, C1/C2, C1&GT;C2, and so on, to achieve the arithmetic of the score, comparison, the countdown and so on.

/** Copyright (c) 2015, Yantai University School of Computer * All right reserved.* Shao * file: demo.cpp* finish: April 01, 2015 * Version number: v1.0*/#include <iostream  > #include <cmath>using namespace Std;class cfraction{private:int nume;  molecular int Deno;   Denominator public:cfraction (int nu=0,int de=1);    constructor, initialized with void set (int nu=0,int de=1); Value, change the value with void input (),//Enter void simplify () in the form "nu/de", such as "5/2";//simplify (so that numerator denominator has no common factor) void amplify (int n);//enlarge n times, such as 2 /3 magnification 5 times times for 10/3 void output (int style=0);//output: 8/6 For example, style is 0 o'clock, as is output 8/6,//style is 1 o'clock, output simple form 4/3,//style is 2 o'clock, Output 1 (1/3) Form, table //style One-third; 3 o'clock, Output in decimal form, e.g. 1.3333;//default 0};int gcd (int m, int n);        cfraction::cfraction (int nu,int de) {if (de!=0) {nume=nu;    Deno=de; } else cout<< "A fatal error has occurred! "<<endl;}        void Cfraction::set (int nu,int de) {if (de!=0) {nume=nu;    Deno=de; } else cout<< "A fatal error has occurred! "<<endl;}    void Cfraction::input () {int nu,de;    Char A; cout<< "Please enter the score (A/b): ";        while (1) {cin>>nu>>a>>de;            if (a!= '/') {cout<< "input format is incorrect, please re-enter ~" <<endl;        Continue            } else {nume=nu;            Deno=de;        Break    }}}void cfraction::simplify () {int n=gcd (Deno, Nume);     Deno/=n; simplification nume/=n;}    int gcd (int m, int n)//This function can be defined as a member function of a class, or it can be a general function {int r;        if (m<n) {r=m;        M=n;    N=r;        } while (r=m%n)//M,n Greatest common divisor {m=n;    N=r; } return n;} void cfraction::amplify (int n) {deno*=n;}    void cfraction::output (int style) {int n;        switch (style) {case 0:cout<< "as-is score:" <<nume<< '/' <<deno<<endl;    Break        Case 1:N=GCD (Deno, Nume);        cout<< "Simplification as:" <<nume/n<< "/" <<deno/n<<endl;    Break Case 2:cout<< "true score:" <<nume/deno<< "(' <<nume%deno<< '/' <<deno<< ') ' <<endl;    Break        Case 3:cout<< "Decimals:" <<nume/double (Deno) <<endl;    Break    default:cout<< "Input Error ~" <<endl;    }}int main ()//test function from the old {cfraction c1,c2 (8,6);    cout<< "About C1:" <<endl;    C1.output (0);   cout<< "Change C1:" <<endl;    Test set function C1.set (2,7);    C1.output ();    cout<< "Input C1:" <<endl;//test input function c1.input ();    C1.output (0);    cout<< "About C2:" <<endl;//test output function c2.output (0);    C2.output (1);    C2.output (3);    C2.output (3);    C2.output ();    cout<< "will C2 simplification:" <<endl;//test simplify function c2.simplify ();    C2.output (0);    cout<< "C2 magnification:" <<endl;//test Amplify function c2.amplify (5);    C2.output (0);    C2.output (1); return 0;}

Operation Result:



@ Mayuko

Project 2-The prototype of the fractional class

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.