六周任务三

来源:互联网 发布:封装json数据 编辑:程序博客网 时间:2024/04/30 05:24
/* (程序头部注释开始)* 程序的版权和版本声明部分* Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved.* 文件名称:                              * 作    者:   张启立                          * 完成日期:   2012      年   3    月    28   日* 版 本 号:          * 对任务及求解方法的描述部分* 输入描述:   * 问题描述:   设计平面坐标点类,计算两点间的距离,到原点的距离以及关于x轴,y轴,原点对称的点.* 程序输出: * 程序头部的注释结束*/#include < iostream >#include < cmath >using namespace std; enum SymmetricStyle { axisx, axisy, point };//分别表示按x轴, y轴, 原点对称class CPoint{private:double x;  // 横坐标double y;  // 纵坐标public:CPoint ( double xx=0 , double yy=0  ) ;double Distance ( CPoint p ) const;   // 两点之间的距离(一点是当前点,另一点为参数p)double Distance0 ( ) const;          // 到原点的距离CPoint SymmetricAxis ( SymmetricStyle style) const;   // 返回对称点void input ( );  //以x,y 形式输入坐标点void output ( ); //以(x,y) 形式输出坐标点};CPoint :: CPoint(double xx,double yy)  {      x=xx;      y=yy;  }  // 两点之间的距离(一点是当前点,另一点为参数p)double CPoint::Distance ( CPoint p ) const   {return sqrt (( x - p.x ) * ( x - p.x ) + ( y - p.y ) * ( y - p.y ));}// 到原点的距离double CPoint:: Distance0 ( ) const{return sqrt ( x * x + y * y );}// 返回对称点CPoint CPoint::SymmetricAxis ( SymmetricStyle style) const{CPoint p;switch ( style ){case axisx:p.y = -y; break;case axisy:p.x = -x; break;case point:p.x = -x;p.y = -y;}return p;}//以x,y 形式输入坐标点void CPoint:: input ( ){char ch ;while ( 1 ){cin >> x >> ch >> y;if ( ch != ',' ){cout << " 你输入的格式错误 " << endl;}else{break;}}}void CPoint::output ( )//以(x,y) 形式输出坐标点{cout << x <<',' << y << endl;} void main ( ){double distance;CPoint p1 , p2 , p ;cout << "请分别输入两点坐标:格式为  x,y " << endl;p1.input ();p2.input ();distance = p1.Distance ( p2 );cout << "两点的距离为:" << distance << endl;distance = p1.Distance0();cout << "点距离坐标原点的距离为 " << distance << endl;p = p1.SymmetricAxis ( axisx );cout << "关于x轴对称的点:"  << endl;p.output();p = p1.SymmetricAxis ( axisy );cout << "关于y轴对称的点:" << endl;p.output();p = p1.SymmetricAxis ( point );cout<<"关于原点对称的点坐标为: "<<endl;p.output();}

原创粉丝点击