第六周实验报告 任务三

来源:互联网 发布:宁波知豆电动汽车租赁 编辑:程序博客网 时间:2024/06/12 01:23
#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=1,double yy=2):x(xx),y(yy){} double Distance(CPoint p) const;   // 两点之间的距离(一点是当前点,另一点为参数p) double Distance0() const;          // 到原点的距离 CPoint SymmetricAxis(SymmetricStyle style);   // 返回对称点 void input();  //以x,y 形式输入坐标点 void output(); //以(x,y) 形式输出坐标点};void main(){  CPoint a1; CPoint a2; a1.input(); a1.output(); a1.Distance0(); a1.Distance(a2); a1.SymmetricAxis(axisx); a1.SymmetricAxis(axisy); a1.SymmetricAxis(point); system("pause");} void CPoint::input()  //以x,y 形式输入坐标点{ char a; cout<<"请以“x,y ”形式输入坐标点:"<<endl; cin>>x>>a>>y; if(a!=',') {  exit(0); }}void CPoint::output() //以(x,y) 形式输出坐标点{ cout<<"("<<x<<","<<y<<")"<<endl;}double CPoint::Distance0() const  // 到原点的距离{ cout<<"到原点的距离:"<<endl; cout<<sqrt(x*x+y*y)<<endl; return 0;}double CPoint::Distance(CPoint p) const    cout<<"与默认点的距离为:"<<endl; cout<<sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y))<<endl; return 0;}CPoint CPoint::SymmetricAxis(SymmetricStyle style)  // 返回对称点{ if(style == axisx) {  y = -y; }  if(style == axisy) {  x = -x; }  if(style == point) {  x = -x;  y = -y; } switch(style) { case axisx:cout<<"关于x轴的对称点:("<<x<<","<<y<<")"<<endl;break; case axisy:cout<<"关于y轴的对称点:("<<x<<","<<y<<")"<<endl;break; case point:cout<<"关于原点的对称点:("<<x<<","<<y<<")"<<endl;break; default:break; } return 0;}
this指针运用不太会,代码部分借鉴..
原创粉丝点击