C ++ score class

Source: Internet
Author: User

Class Fraction
{
PRIVATE:
Int above; // molecule
Int below; // denominator
Void assignment (); // minute
Fraction makecommond (fraction); // points

Public:
Fraction (int A = 0, int B = 1) {// Constructor
Above = A; below = B;
}
Fraction add (fraction); // Add two scores
Fraction sub (fraction); // This score minus the real score
Fraction MUL (fraction); // multiply two scores
Fraction Div (fraction); // This score is divided by the real score
Fraction reciprocal (); // calculates the reciprocal
Bool equal (fraction); // equals the operation
Bool greaterthan (fraction); // greater than the operation
Bool lessthan (fraction); // operation smaller
Void display (); // display the score
Void input (); // input score
};

Void Fraction: ction ()
{
Int I, comdiv, small, Max;

If (this-> abve <this-> below ){
Small = This-> above;
Max = This-> below;
} Else {
Small = This-> below;
Max = This-> above;
}

For (I = small; I> 1; I --){
If (small % I = 0 & max % I = 0)
Break;
}
Comdiv = I; // maximum Common Divisor

If (I! = 0 ){
This-> above/= I;
This-> below/= I;
}
}
Fraction fraction: makecommond (fraction frac)
{
Int b1 = This-> below, b2 = Frac. below, M, S;
If (b1> B2 ){
M = b1 % B2;
S = B2;
}
Else {
M = b2 % B1;
S = b1;
}
 
While (M> 0 ){
Int res = S % m;
S = m, M = res;
}
 

Int small = (b1 * B2)/s;
This-> above = This-> above * (small/This-> below );
Frac. Above = Frac. above * (small/Frac. below );
This-> below = small;
Frac. Below = small;
Return frac;
}

Fraction fraction: add (fraction fr)
{
Fraction myfraction;
Myfraction. abve = This-> abve * Fr. below + Fr. abve * This-> below;
Myfraction. Below = This-> below * Fr. below;
 
Myfraction. ction ();

Return myfraction;
}
Fraction fraction: Sub (fraction fr)
{
Fraction myfraction;
Myfraction. abve = This-> abve * Fr. below-fr.above * This-> below;
Myfraction. Below = This-> below * Fr. below;
 
Myfraction. ction ();

Return myfraction;
}

Fraction fraction: MUL (fraction fr)
{
Fraction myfraction;
 
Myfraction. abve = This-> abve * Fr. abve;
Myfraction. Below = This-> below * Fr. below;
 
Myfraction. ction ();

Return myfraction;
}
Fraction fraction: div (fraction fr)
{
Fraction myfraction;
 
Myfraction. Above = This-> above * Fr. below;
Myfraction. Below = This-> below * Fr. abve;
 
Myfraction. ction ();

Return myfraction;
}

Fraction fraction: reciprocal ()
{
Fraction myfraction (this-> abve, this-> below );
If (this-> abve! = 0 ){
Int temp = myfraction. abve;
Myfraction. Above = myfraction. below;
Myfraction. Below = temp;
}
Return myfraction;
}

Bool fraction: equal (fraction frac) // equals operation
{
Bool result = false;
If (this-> sub (frac). Above = 0)
Result = true;

Return result;
}
Bool fraction: greaterthan (fraction frac) // greater than the operation
{
Bool result = false;
If (this-> sub (frac). abve> 0)
Result = true;
Return result;

}
Bool fraction: lessthan (fraction frac) // operation smaller
{
Bool result = false;

If (this-> sub (frac). abve <0)
Result = true;

Return result;

}

Void Fraction: Display ()
{
Cout <this-> above <Endl <"---" <Endl <this-> below <Endl;
}
Void Fraction: input () // input score
{
Cout <"Enter the score numerator :";
Cin> This-> abve;
While (true)
{
Cout <"Enter the denominator of the score (not equal to zero ):";
Cin> This-> below;
If (this-> below = 0)
{
Cout <"the denominator cannot be zero! ";
}
Else
Break;
};

}

Int main ()
{
// Fraction frac (5, 24), frac2 (7, 16 );
// Fraction frac (7, 9), frac2 (8, 11 );
Fraction frac (5, 20), frac2 (50,200 );
Frac. Input ();
Frac2.input ();

Frac. Display ();
Frac2.display ();
Cout <"score addition" <Endl;
Frac. Add (frac2). Display ();
 
Cout <"score subtraction" <Endl;
Frac. sub (frac2). Display ();

Cout <"fractional multiplication" <Endl;
Frac. Mul (frac2). Display ();

Cout <"score division" <Endl;
Frac. Div (frac2). Display ();

Cout <"score reciprocal" <Endl;
Frac. Reciprocal (). Display ();

Char * STR;

Cout <"Equal score comparison :";
If (Frac. Equal (frac2 ))
STR = "equal ";
Else
STR = "not equal ";
Cout <STR <Endl;

Cout <"score greater than comparison :";
If (Frac. greaterthan (frac2 ))
STR = "large ";
Else
STR = "small ";
Cout <STR <Endl;

Cout <"score less than comparison :";
If (Frac. lessthan (frac2 ))
STR = "small ";
Else
STR = "large ";
Cout <STR <Endl;

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.