#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=0); double Distance(CPoint p) const; // 两点之间的距离(一点是当前点,另一点为参数p) double Distance0() const; // 到原点的距离 SymmetricAxis(SymmetricStyle style) const; // 返回对称点 void input(); //以x,y 形式输入坐标点 void output(); //以(x,y) 形式输出坐标点 }; double CPoint::Distance(CPoint p) const// 两点之间的距离(一点是当前点,另一点为参数p) { double d; d=sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)); cout<<d<<endl; return 0; } double CPoint::Distance0() const// 到原点的距离 { double d; d=sqrt(x*x+y*y); cout<<d<<endl; return 0; } void CPoint::input()//以x,y 形式输入坐标点 { cout<<"请输入坐标:"<<endl; char c; while (1) { cin>>x>>c>>y; if(c==',') break; else cout<<"格式不对,重输"<<endl; } } /*void CPoint::output()//以(x,y) 形式输出坐标点 { cout<<"("<<x<<','<<y<<')'<<endl; }*/ CPoint::CPoint(double xx,double yy)//构造函数定义,不要加上具体的值 { x=xx; y=yy; } CPoint::SymmetricAxis(SymmetricStyle style) const// 返回对称点 { CPoint p(x,y);// switch (style) { case axisx:p.y=-y;break; case axisy:p.x=-x;break; case point:p.x=-x;p.y=-y;break; } cout<<'('<<p.x<<','<<p.y<<')'<<endl; return 0; } void main() { CPoint p,p1,p2; p1.input(); p2.input(); cout<<"p1到原点 的距离:"<<endl; p1.Distance0(); cout<<"p1到p点的距离:"<<endl; p1.Distance(p);//不能再加上CPoint了。 cout<<"p2到p点 的距离:"<<endl; p2.Distance(p); cout<<"p1关于x轴的对称点:"<<endl; p1.SymmetricAxis(axisx); cout<<"p1到p2点 的距离:"<<endl; p1.Distance(p2); cout<<"p1关于原点的对称点为:"<<endl; p1.SymmetricAxis(point); }
感悟:在没听周一下机后的辅导,自己做程序很有思路,即使有错误,自己也有耐心和信心找出来。可是听了贺老师的点拨以后,自己的思路就没了,即使是小的错误,也没有耐心改正了,总想按老师 的思路做,可是按老师的思路做,自己却不动脑子了。本程序虽然有小的不同,但完全是按自己的思路来的,比按老师 的思路来做感觉要爽。
第六周任务3
最新推荐文章于 2024-01-18 15:20:35 发布