<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">
  <span lang="en-ca"><b>Question 1:<br>
  ----------<br>
  <br>
  Using the structure for a Cartesian point given below, write a function that
  <br>
  receives two Cartesian points and returns the distance between the two points.<br>
  <br>
  struct CartesianPoint {<br>
  float x ;<br>
  float y ;<br>
  float z ;<br>
  } ;<br>
  <br>
  Use the function prototype:<br>
  <br>
  float distance( CartesianPoint , CartesianPoint ) ;<br>
  <br>
  Write a driver program to test your function.</b></span></div>
<div align="left">
  กก</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><span lang="en-ca">There is only one thing worth mentioning: It solves the &quot;cin&quot; problem---suppose you want to cin&gt;&gt;aInteger, and </span></b></pre>
</div>
<div align="left">
  <pre><b><span lang="en-ca">the naughty user input a character, then you program crashed. I want to solve it for long time. The instructor</span></b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>showed me the correct way, though still some detail not figured out.</b></span></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>void inputCart(CartesianPoint&amp; theCart)</b></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>	bool doInput(float&amp; number)</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>It is the only reason that I include this program in my collection---it solves the failed input for cin.</b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>You need to check cin.fail() first and then clear the fail bit. But I don't understand the cin.get()! If it </b></span></pre>
</div>
<div align="left">
  <pre><span lang="en-ca"><b>is not included, the program is crashing like before.</b></span></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"> Are you kidding? For such a trivial program?</span></b></pre>
</div>
<pre>กก</pre>
<pre>/*		Assignment #1
		=============

Question 1:
----------

Using the structure for a Cartesian point given below, write a function that 
receives two Cartesian points and returns the distance between the two points.

struct CartesianPoint {
	float x ;
	float y ;
	float z ;
} ;

Use the function prototype:

float distance( CartesianPoint , CartesianPoint ) ;

Write a driver program to test your function.
*/
#include &lt;iostream&gt;
#include &lt;cmath&gt;

using namespace std;

struct CartesianPoint {
	float x ;
	float y ;
	float z ;
} ;

float distance(CartesianPoint&amp; cart1, CartesianPoint&amp; cart2);

void inputCart(CartesianPoint&amp; theCart);

int main()
{

	CartesianPoint cart1, cart2;
	char choice;
	
	do 
	{
		//definitely the user must input correct cartesian to go to next
		//and this is done by checking each input
		cout&lt;&lt;&quot;Now enter first cartesian\n&quot;;	
		inputCart(cart1);
		cout&lt;&lt;&quot;Now enter second cartesian\n&quot;;
		inputCart(cart2);

		cout&lt;&lt;&quot;then the distance between two cartesian point is:\n&quot;;
		cout&lt;&lt;distance(cart1,cart2)&lt;&lt;endl;
		
		//a menu choice for user to choose if continue
		cout&lt;&lt;&quot;enter 'q' to quit...&quot;;
		cin&gt;&gt;choice;
	}
	while(choice!='q');

	return 0;
}

//this does the job of input a cartesian by prompt user to 
//input three float, if either one fails, all fail
void inputCart(CartesianPoint&amp; theCart)
{
	//a small sub-routine to save repeat code
	bool doInput(float&amp; number);

	do 
	{
		cout&lt;&lt;&quot;\nnow enter x:&quot;;
	}
	while (!doInput(theCart.x));

	do
	{
		cout&lt;&lt;&quot;\nnow enter y:&quot;;
	}
	while (!doInput(theCart.y));
	do
	{
		cout&lt;&lt;&quot;\nnow enter z:&quot;;
	}
	while (!doInput(theCart.z));
}

//this is a sub-routine from &quot;inputCart&quot; as I don't want to write 
//same code three times
bool doInput(float&amp; number)
{
	cin&gt;&gt;number;
	//be frank with you, before I wrote mail to you, I did try this way, 
	//but I was cheated by my other small bugs
	if (cin.fail())
	{
		cin.clear();	
		cout&lt;&lt;&quot;input error! try again!.\n&quot;;
		cin.get();//this line I do have doubt, 
		//why do we need this? how can we clear buffer?
				
		return false;
	}
	else 
	{
		return true;
	}
}

//I change the function proto type from passing by value to by reference
//because I think it saves some processing time by not copying structure
float distance(CartesianPoint&amp; cart1, CartesianPoint&amp; cart2)
{
	int sumSquare=0;
	//we only need to calc square, so order of two cart doesn't matter
	sumSquare += (cart1.x - cart2.x)* (cart1.x - cart2.x);
	sumSquare += (cart1.y - cart2.y)* (cart1.y - cart2.y);
	sumSquare += (cart1.z - cart2.z)* (cart1.z - cart2.z);

	return sqrt(sumSquare);
}
</pre>
<pre>
</pre>
<pre>


	</pre>
<pre></pre>
<pre>			
</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>