<html>

<head><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- MyFirstUnitAd -->
<ins class="adsbygoogle"
     style="display:inline-block;width:970px;height:250px"
     data-ad-client="ca-pub-5778386704669218"
     data-ad-slot="1503492166"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>

<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Population</title>
</head>

<body>



<p align="center"><span lang="en-ca"><font size="6" color="#FF0000"><b>Cartesian</b></font></span></p>

<div align="left">
  <pre><b><font color="#ff0000" size="5">A.<span lang="en-ca">First</span> Edition</font></b></pre>
</div>
<div align="left">
  <pre><b>This is <span lang="en-ca">my first edition of a simple assignment of C++.</span> And I hesitate for some time to decide whether to add it.</b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">B</span>.<span lang="en-ca"><a name="problem"></a>The problem</span></font></b></pre>
</div>
<div align="left">
  <pre>	Assignment #2
		=============

Create a class Fraction which is composed of private &quot;numerator&quot; and 
&quot;denominator&quot; data members.

Your class must contain the following:

1. constructor (including default arguments numerator = denominator = 1)
2. print method (print in the form &quot;numerator/denominator&quot;
3. add function
4. subtract function
5. multiply function
6. divide function

* The add, subtract, multiply, and divide functions should be &quot;friends&quot; of the
  Fraction class.  Each function must receive two constant references to 
  Fraction objects as arguments and return a Fraction.  Furthermore, these 
  functions must reduce the resulting fraction to the lowest common denominator.

* The constructor should also reduce the fraction to the lowest common 
  denominator. (Hint: You may want to write a private function to reduce 
  to the LCD.)

* Note that negative fractions are allowed.  The negative sign should always 
  be associated with the numerator.

**************************************************************************
* Ensure that constant Fraction object may be created, used and printed. *
**************************************************************************

* Remember that a denominator should never be zero! Don't forget to handle it!

* Document (i.e. comment) your code well!!!!!!!

* You MUST use the driver program given below!!!</pre>
</div>
<div align="left">
  <b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.<span lang="en-ca">The
  </span></font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>idea of 
  program</b></font></span></div>
<div align="left">
  <pre><b>There is only a few points worth mentioning.</b></pre>
</div>
<div align="left">
  <pre><b>1. How to find LCM? My way is to find GCD and then use theorem that the product of two number is equal to product</b></pre>
</div>
<div align="left">
  <pre><b>of their LCM and GCD. As for GCD, it is very easy to get by Euclidean Algorithm.</b></pre>
</div>
<div align="left">
  <pre><b>2. How to make const objects possible? You must implement all those functions that might be used by const objects</b></pre>
</div>
<div align="left">
  <pre><b>and make sure they are all const. Or in other words, they all have a const key word to make &quot;this&quot; pointer const.</b></pre>
</div>
<div align="left">
  <pre><b>Remember const must also appear in definition of functions.</b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5">D.<span lang="en-ca"><a name="Method"></a>The </span>major functions</font></b></pre>
</div>
<div align="left">
  <pre><b>1.<span lang="en-ca"> </span></b><span lang="en-ca"><b>int GCD(int first, int second)</b></span></pre>
</div>
<div align="left">
  <pre><b>This is another one for Euclidean Algorithm, I remember that at least I implemented it for three times.(including</b></pre>
</div>
<div align="left">
  <pre><b>assembly.)</b></pre>
</div>
<div align="left">
  <pre><b><font color="#ff0000" size="5"><span lang="en-ca">C</span>.</font></b><span lang="en-ca"><font size="5" color="#FF0000"><b>Further improvement</b></font></span></pre>
</div>
<div align="left">
  <pre><b>1.<span lang="en-ca"> </span>All the four friend functions return an object and this implies a dynamic allocated object must be created. </b></pre>
</div>
<div align="left">
  <pre><b>Then who would delete them?</b></pre>
</div>
<div align="left">
  <pre><b>2. Friend functions cannot access member functions of class. And I have to make GCD a friend function either </b></pre>
</div>
<div align="left">
  <pre><b>which is meaningless.</b></pre>
</div>
<pre>กก</pre>
<pre>#include &lt;iostream&gt;

using namespace std;

enum FractionName
{Numerator, Denominator};

class Fraction
{
	friend Fraction&amp; add(const Fraction&amp; first, const Fraction&amp; second);
	friend Fraction&amp; subtract(const Fraction&amp; first, const Fraction&amp; second);
	friend Fraction&amp; multiply(const Fraction&amp; first, const Fraction&amp; second);
	friend Fraction&amp; divide(const Fraction&amp; first, const Fraction&amp; second);
	friend int GCD(int first, int second);
private:
	int denominator;
	int numerator;
	void reduceForm();
	

public:
	void print() const;
	Fraction(int num=1, int den=1);
};

int main()
{
	Fraction F;

   // TEST ZERO DEMONINATOR
    Fraction f0(1,0) ;              // uh-oh! constructor should handle it
    cout &lt;&lt; &quot;Fraction f0(1,0) = &quot; ;
    f0.print() ;                   			//should NOT print &quot;1/0&quot;
    cout &lt;&lt; endl ;

    // TEST REDUCE LOWEST COMMON DENOMINATOR
    Fraction f1(2,6) ;              		// should reduce to 1/3
    cout &lt;&lt; &quot;Fraction f1(2,6) = &quot; ;
    f1.print() ;                    		// should print &quot;1/3&quot;
    cout &lt;&lt; endl ;

    Fraction f2(1024,8) ;           		// reduces to 128/1
    cout &lt;&lt; &quot;Fraction f2(1024,8) = &quot; ;
    f2.print() ;					// should print &quot;128&quot;
    cout &lt;&lt; endl ;

	// TEST NEGATIVE FRACTION
    Fraction fneg(3,-7) ;             
    cout &lt;&lt; &quot;Fraction fneg(3,-7) = &quot; ;
    fneg.print() ;                    		// should print &quot;-3/7&quot;
    cout &lt;&lt; endl ;

    // TEST const FRACTION CREATION
    const Fraction half(1,2) ;
    cout &lt;&lt; &quot;const Fraction half(1,2) = &quot; ;
    half.print() ;					// should print &quot;1/2&quot;
    cout &lt;&lt; endl ;

    // TEST ADD FUNCTION
    Fraction Fa ;					// defaults to 1/1 
    Fa = add( f1, f2 ) ;				// memberwise copy
    cout &lt;&lt; &quot;Fa = add( f1, f2 ) = &quot; ;
    Fa.print() ;					// should print &quot;385/3&quot;
    cout &lt;&lt; endl ;

    cout &lt;&lt; &quot;add( f1, half ).print() = &quot; ;
    add( f1, half ).print() ;			// should print &quot;5/6&quot;
    cout &lt;&lt; endl ;

    // TEST SUBTRACT FUNCTION
    cout &lt;&lt; &quot;subtract( half, fneg ).print() = &quot; ;
    subtract( half, fneg ).print() ;		// should print &quot;13/14&quot;
    cout &lt;&lt; endl ;

	cout &lt;&lt; &quot;subtract( f1, Fa ).print() = &quot; ;
    subtract( f1, Fa ).print() ;			// should print &quot;-128&quot;
    cout &lt;&lt; endl ;

    // TEST MULTIPLY FUNCTION
    cout &lt;&lt; &quot;multiply( f1, fneg ).print() = &quot; ;
    multiply( f1, fneg ).print() ;			// should print &quot;-1/7&quot;
    cout &lt;&lt; endl ;

    // TEST DIVIDE FUNCTION
    cout &lt;&lt; &quot;divide( f1, fneg ).print() = &quot; ;
    divide( f1, Fa ).print() ;			// should print &quot;1/385&quot;
    cout &lt;&lt; endl ;

	return 0;
}

//denominator may be input 0 or negative, which should be handled
Fraction::Fraction(int num, int den)
{
	numerator = num;
	if (den==0)
	{
		cout&lt;&lt;&quot;uh-oh!Denominator is 0! And it is set to 1 now.\n&quot;;
		denominator = 1;
	}
	else
	{
		//I need to keep denominator to be positive always
		if (den&lt;0)
		{
			denominator = den*(-1);
			numerator *= -1;//the sign should be carried by numerator
		}
		else
		{
			denominator = den;
		}
	}
	reduceForm();
}

//I need to find the GCD which must be positive
void Fraction::reduceForm()
{
	int divisor;
	//the divisor must be positive
	divisor = GCD(abs(numerator), denominator);
	if (divisor!=1)
	{
		numerator /= divisor;
		denominator /= divisor;
	}
}

void Fraction::print() const
{
	cout&lt;&lt;(numerator&lt;0?'-':' ')&lt;&lt;abs(numerator)&lt;&lt;'/'&lt;&lt;denominator&lt;&lt;endl;
}

//Using Euclidean Algorithms to find GCD
int GCD(int first, int second)
{
	div_t result; //this is a built-in struct
	int divider = first;
	int divisor = second;
	result = div(divider, divisor);
	while (result.rem!=0)
	{	
		divider = divisor;
		divisor = result.rem;
		
		result = div(divider, divisor);
	}

	return divisor;
}

Fraction&amp; add(const Fraction&amp; first, const Fraction&amp; second)
{
	int gcd, lcm;
	int newNum;
	gcd = GCD(first.denominator, second.denominator);
	//a theorem: product of two number equal product of their GCD and LCM
	lcm = first.denominator * second.denominator / gcd;
	
	newNum = lcm / first.denominator * first.numerator;
	newNum += lcm/second.denominator * second.numerator;

	Fraction* Result = new Fraction (newNum, lcm);
	return *Result;
}

Fraction&amp; subtract(const Fraction&amp; first, const Fraction&amp; second)
{
	int gcd, lcm;
	int newNum;
	gcd = GCD(first.denominator, second.denominator);
	//a theorem: product of two number equal product of their GCD and LCM
	lcm = first.denominator * second.denominator / gcd;
	
	newNum = lcm / first.denominator * first.numerator;
	newNum -= lcm/second.denominator * second.numerator;

	Fraction* Result = new Fraction(newNum, lcm);
	return *Result;
}

Fraction&amp; multiply(const Fraction&amp; first, const Fraction&amp; second)
{
	Fraction* Result = 
		new Fraction(first.numerator*second.numerator, first.denominator*second.denominator);
	return *Result;
}

Fraction&amp; divide(const Fraction&amp; first, const Fraction&amp; second)
{
	Fraction* Result = 
		new Fraction (first.numerator*second.denominator, first.denominator*second.numerator);
	return *Result;
}



</pre>

<pre></pre>

<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                   
&nbsp;&nbsp;&nbsp; <a href="WhoAmI.htm">                  







                       <img src="picture/back.gif" style="border: medium none" alt="back.gif (341 bytes)" width="32" height="35"></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<a href="index.htm"><img src="picture/up.gif" style="border: medium none" alt="up.gif (335 bytes)" width="35" height="32"></a>       &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                         
<img src="picture/next.gif" style="border: medium none" alt="next.gif (337 bytes)" width="32" height="35">          


</p>

</body>

</html>