Week 5 Project 2

Source: Internet
Author: User

[Cpp]
/*
* Copyright and version Declaration of the program
* Copyright (c) 2012, a student from the computer College of Yantai University
* All rightsreserved.
* File name: cf. cpp
* Author: Wang liqiang
* Completion date: January 1, April 3, 2013
* Version: v1.0
* Input Description: omitted
* Problem description: omitted
* Program output: omitted
*/
# Include <iostream>
# Include <Cmath>
Using namespace std;
Int gcd (int m, int n );
Class CFraction
{
Private:
Int nume; // molecule
Int deno; // denominator
Public:
CFraction (int nu = 0, int de = 1); // constructor, initialized
Void set (int nu = 0, int de = 1); // set the value.
Void input (); // in the format of "nu/de", such as "5/2"
Void simplify (); // simplification (removing the public factor from the numerator denominator)
Void amplify (int n); // enlarge n times, for example, 2/3 to 5 times to 10/3
Void output (int style = 0 );
};

CFraction: CFraction (int nu, int de) // constructor, used for initialization
{
If (de! = 0)
{
Nume = nu;
Deno = de;
}
Else
{
Cerr <"initialization error, program exited \ n ";
System ("pause ");
Exit (0 );
}
}

Void CFraction: set (int nu, int de) // set the value.
{
If (de! = 0) // If not, the value change operation is invalid.
{
Nume = nu;
Deno = de;
}
}

Void CFraction: input () // follow the format of "nu/de", for example, "5/2"
{
Int nu, de;
Char c;
While (1)
{
Cout <"input score (m/n ):";
Cin> nu> c> de;
If (c! = '/')
Cout <"incorrect input format! \ N ";
Else if (de = 0)
Cout <"the denominator cannot be zero! \ N ";
Else
Break;
}
Nume = nu;
Deno = de;
}

// Simplify the score so that the denominator does not have a common factor
Void CFraction: simplify ()
{
Int n = gcd (deno, nume );
Deno/= n; // simplified
Nume/= n;
}

// Calculate the maximum public approx. of m and n
Int gcd (int m, int n) // this function can be defined as a member function of the class or a general function.
{
Int r;
If (m <n) {r = m; m = n; n = r ;}
While (r = m % n) // evaluate the maximum public approx. of m, n
{
M = n;
N = r;
}
Return n;
}

Void CFraction: amplify (int n) // enlarge n times, for example, 2/3 to 5 times to 10/3
{
Nume * = n;
}
// Output score: Take/6 as an example.
// If the style is 0, 8/6 is output as is;
// When the style is 1, the output format is simplified to 4/3;
// When the style is 2, the output format is 1 (1/3), indicating one or more 1/3;
// When the style is 3, it is output in decimal form, for example, 1.3333;
Void CFraction: output (int style)
{
Int n;
Switch (style)
{
Case 0:
Cout <"unchanged:" <nume <'/' <deno <endl;
Break;
Case 1:
N = gcd (deno, nume );
Cout <"simplified form:" <nume/n <'/' <deno/n <endl; // output simplified form, not simplified
Break;
Case 2:
Cout <"with score form:" <nume/deno <'(' <nume % deno <'/' <deno <')' <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 <"simplify c2:" <endl;
C2.simplify ();
C2.output (0 );

Cout <"doubles c2:" <endl;
C2.amplify (5 );
C2.output (0 );
C2.output (1 );

System ("pause ");
Return 0;
}

/*
* Copyright and version Declaration of the program
* Copyright (c) 2012, a student from the computer College of Yantai University
* All rightsreserved.
* File name: cf. cpp
* Author: Wang liqiang
* Completion date: January 1, April 3, 2013
* Version: v1.0
* Input Description: omitted
* Problem description: omitted
* Program output: omitted
*/
# Include <iostream>
# Include <Cmath>
Using namespace std;
Int gcd (int m, int n );
Class CFraction
{
Private:
Int nume; // molecule
Int deno; // denominator
Public:
CFraction (int nu = 0, int de = 1); // constructor, initialized
Void set (int nu = 0, int de = 1); // set the value.
Void input (); // in the format of "nu/de", such as "5/2"
Void simplify (); // simplification (removing the public factor from the numerator denominator)
Void amplify (int n); // enlarge n times, for example, 2/3 to 5 times to 10/3
Void output (int style = 0 );
};
 
CFraction: CFraction (int nu, int de) // constructor, used for initialization
{
If (de! = 0)
{
Nume = nu;
Deno = de;
}
Else
{
Cerr <"initialization error, program exited \ n ";
System ("pause ");
Exit (0 );
}
}
 
Void CFraction: set (int nu, int de) // set the value.
{
If (de! = 0) // If not, the value change operation is invalid.
{
Nume = nu;
Deno = de;
}
}
 
Void CFraction: input () // follow the format of "nu/de", for example, "5/2"
{
Int nu, de;
Char c;
While (1)
{
Cout <"input score (m/n ):";
Cin> nu> c> de;
If (c! = '/')
Cout <"incorrect input format! \ N ";
Else if (de = 0)
Cout <"the denominator cannot be zero! \ N ";
Else
Break;
}
Nume = nu;
Deno = de;
}
 
// Simplify the score so that the denominator does not have a common factor
Void CFraction: simplify ()
{
Int n = gcd (deno, nume );
Deno/= n; // simplified
Nume/= n;
}
 
// Calculate the maximum public approx. of m and n
Int gcd (int m, int n) // this function can be defined as a member function of the class or a general function.
{
Int r;
If (m <n) {r = m; m = n; n = r ;}
While (r = m % n) // evaluate the maximum public approx. of m, n
{
M = n;
N = r;
}
Return n;
}
 
Void CFraction: amplify (int n) // enlarge n times, for example, 2/3 to 5 times to 10/3
{
Nume * = n;
}
// Output score: Take/6 as an example.
// If the style is 0, 8/6 is output as is;
// When the style is 1, the output format is simplified to 4/3;
// When the style is 2, the output format is 1 (1/3), indicating one or more 1/3;
// When the style is 3, it is output in decimal form, for example, 1.3333;
Void CFraction: output (int style)
{
Int n;
Switch (style)
{
Case 0:
Cout <"unchanged:" <nume <'/' <deno <endl;
Break;
Case 1:
N = gcd (deno, nume );
Cout <"simplified form:" <nume/n <'/' <deno/n <endl; // output simplified form, not simplified
Break;
Case 2:
Cout <"with score form:" <nume/deno <'(' <nume % deno <'/' <deno <')' <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 <"simplify c2:" <endl;
C2.simplify ();
C2.output (0 );
 
Cout <"doubles c2:" <endl;
C2.amplify (5 );
C2.output (0 );
C2.output (1 );
 
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.