实验三

来源:互联网 发布:hp打印机软件 编辑:程序博客网 时间:2024/04/29 21:12

1.实验名称:实验三计算用户输入的两点确定的直线长度和斜率。

2.实验时间:2008-4-1

3.实验目的:编程用面向对象设计思想分析、设计并实现实验三的内容。

4.实验内容:两点决定一条直线,请编程首先计算由起点(12)与终点(2649)确定的直线长度和斜率,然后,计算用户输入的两点确定的直线长度和斜率。在程序中首先声明一个类Point,然后利用它声明一个组合类Line,并按要求编写测试程序。

//程序源代码

//Line.h

#include <iostream.h>

#include <math.h>

class Point

{

private:

       float x,y;

public:

       Point(float a,float b)

       {

              x=a;

              y=b;

       }

       float getX()

       {

              return x;

       }

       float getY()

       {

              return y;

       }

};

class Line

{

private:

       Point Pa,Pb;

public:

       Line(float x1,float y1,float x2,float y2);

       float getLength();

       float getSlope();

};

Line::Line(float x1,float y1,float x2,float y2):Pa(x1,y1),Pb(x2,y2)

{

}

float Line::getLength()

{

       return (float)sqrt(double((Pa.getX()-Pb.getX())*(Pa.getX()-Pb.getX())+(Pa.getY()-Pb.getY())*(Pa.getY()-Pb.getY())));

}

float Line::getSlope()

{

       return (Pa.getY()-Pb.getY())/(Pa.getX()-Pb.getX());

}

void main()

{

       float x1,y1,x2,y2;

       Line L1(1,2,26,49);

       cout<<"the length of two points is:"<<L1.getLength()<<endl;

       cout<<"the slope of the line is:"<<L1.getSlope()<<endl;

       cout<<"please inputs the information of two ponits which like '1 2 26 49' is separated by  a blank space!"<<endl;

       cin>>x1>>y1>>x2>>y2;

       Line L2(x1,y1,x2,y2);

       cout<<"the length of two points of user's inputs is:"<<L2.getLength()<<endl;

       cout<<"the slope of the line of user's inputs is:"<<L2.getSlope()<<endl;

}//end main()

/**************************************************************************

 * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *

 * Pearson Education, Inc. All Rights Reserved.                           *

 *                                                                        *

 * DISCLAIMER: The authors and publisher of this book have used their     *

 * best efforts in preparing the book. These efforts include the          *

 * development, research, and testing of the theories and programs        *

 * to determine their effectiveness. The authors and publisher make       *

 * no warranty of any kind, expressed or implied, with regard to these    *

 * programs or to the documentation contained in these books. The authors *

 * and publisher shall not be liable in any event for incidental or       *

 * consequential damages in connection with, or arising out of, the       *

 * furnishing, performance, or use of these programs.                     *

 **************************************************************************/

//实验结果

/*result:

the length of two points is:53.2353

the slope of the line is:1.88

please inputs the information of two ponits which like '1 2 26 49' is separated

by  a blank space!

1 1 99 99

the length of two points of user's inputs is:138.593

the slope of the line of user's inputs is:1

Press any key to continue

*/

5.实验总结:对了sqrt();有三种情况,放到它上面就会看到从13的小三角按钮!

第一种是int;第二种是array...&;第三种是double

对组合类的使用有进一步深入。