"Project 2-prototype of fractional class"
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 as an example, style is 0 o'clock, as is output 8/6;//style is 1 o'clock, output simplified form 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};
<span style= "Color:rgb (85, 85, 85); font-family: ' Microsoft Yahei '; font-size:15.5555562973022px; line-height:35px; " > 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. </span><br style= "Color:rgb (85, 85, 85); font-family: ' Microsoft Yahei '; font-size:15.5555562973022px; line-height:35px; "/><span style=" Color:rgb (85, 85, 85); font-family: ' Microsoft Yahei '; font-size:15.5555562973022px; line-height:35px; " > Practice Strategy: </span><br style= "Color:rgb (85, 85, 85); font-family: ' Microsoft Yahei '; font-size:15.5555562973022px; line-height:35px; "/><span style=" Color:rgb (85, 85, 85); font-family: ' Microsoft Yahei '; font-size:15.5555562973022px; line-height:35px; " > (1) It is not recommended to implement all member functions once and then debug, but to implement one, test one; </span><br style= "Color:rgb (85, 85, 85); font-family: ' Microsoft Yahei '; font-size:15.5555562973022px; line-height:35px; "/><span style=" Color:rgb (85, 85, 85); font-family: ' Microsoft Yahei '; font-size:15.5555562973022px; line-height:35px; " > (2) The implementation of this project and the test order can be: the first constructor and output function (can be implemented only one output mode), then set function, then input function, and so on. </span><p style= "margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; Color:rgb (85, 85, 85); font-family: ' Microsoft Yahei '; font-size:15.5555562973022px; line-height:35px; " ></p><p style= "margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; Color:rgb (85, 85, 85); font-family: ' Microsoft Yahei '; font-size:15.5555562973022px; line-height:35px; " > Later we will be able to: (1) define the int i,j, can use the cin>>i>>j; on the keyboard to enter the values of I and 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>C2, and so on, to achieve the arithmetic of the score, comparison, the countdown and so on. </p><p style= "margin-top:0px; margin-bottom:0px; padding-top:0px; padding-bottom:0px; Color:rgb (85, 85, 85); font-family: ' Microsoft Yahei '; font-size:15.5555562973022px; line-height:35px; " ></p><p style= "margin-top:0px; margin-bottom:0px; padding-top:0px;padding-bottom:0px; Color:rgb (85, 85, 85); font-family: ' Microsoft Yahei '; font-size:15.5555562973022px; line-height:35px; " ><pre name= "code" class= "CPP" > #include <iostream> #include <Cmath> #include <cstdlib>using namespace Std;int gcd (int m, int n); class cfraction{private:int Nume; int deno;public:cfraction (int nu=0,int de=1); void set (int nu=0,int de=1); void input (); void simplify (); void Amplify (int n); void output (int style=0);}; cfraction::cfraction (int nu,int de) {if (de!=0) {nume=nu; Deno=de; } else {cerr<< "error occurred during initialization, program exited \ n"; Exit (0); }}void cfraction::set (int nu,int de) {if (de!=0) {nume=nu; Deno=de; }}void cfraction::input () {int nu,de; char c; while (1) {cout<< "input fraction (m/n):"; cin>>nu>>c>>de; if (c!= '/') cout<< "input format is wrong! \ n "; else if (de==0) cout<< "denominator cannot be zero! \ n "; else break; } Nume=nu; Deno=de;} void Cfraction::simplify () {int n=gcd (Deno, Nume); Deno/=n; Nume/=n;} int gcd (int m, int n) {int r; if (m<n) {r=m; M=n; N=r; } while (r=m%n) {m=n; N=r; } return n;} void cfraction::amplify (int n) {nume*=n;} void cfraction::output (int style) {int n; switch (style) {case 0:cout<< "as is:" <<nume<< '/' <<deno<<endl; Break Case 1:N=GCD (Deno, Nume); cout<< "simplified form:" <<nume/n<< "/" <<deno/n<<endl; Break Case 2:cout<< "with fractional form:" <<nume/deno<< "(' <<nume%deno<< '/' <<deno<< ') ' <& Lt;endl; Break Case 3:cout<< "Approximate value:" <<nume/double (Deno) <<endl; Break default:cout<< "Default as is:" <<nume<< '/' <<deno<<endl; }}int Main () {CFraction C1,C2 (8, 6); cout<< "About C1:" <<endl; C1.output (0); cout<< "Change C1:" <<endl; C1.set (2,7); C1.output (); cout<< "Input C1:" <<endl; C1.input (); C1.output (0); cout<< "About C2:" <<endl; C2.output (0); C2.output (1); C2.output (3); C2.output (3); C2.output (); cout<< "will C2 Jane:" <<endl; C2.simplify (); C2.output (0); cout<< "C2 magnification:" <<endl; C2.amplify (5); C2.output (0); C2.output (1); return 0;}
Operation Result:
Project two-the prototype of the fractional class