第五周上机实践项目1——体验常成员函数
2015-04-05 13:03
225 查看
问题及代码
运行结果:
/* * Copyright (c) 2015, 烟台大学计算机学院 * All rights reserved. * 文件名称:test.cpp * 作 者:辛彬 * 完成日期:2015 年 4 月 5 日 * 版 本 号:v1.0 * * 问题描述:设计平面坐标点类,计算两点之间距离、到原点距离、关于坐标轴和原点的对称点等。 * 输入描述:平面坐标。 * 程序输出:两点之间距离、到原点距离、关于坐标轴和原点的对称点。 */ #include <iostream> #include <cmath> using namespace std; class CPoint { private: double x; // 横坐标 double y; // 纵坐标 public: double Distance1(CPoint p) const; //两点之间的距离(一点是当前点——想到this了吗?,另一点为p) double Distance0() const; // 到原点(0,0)的距离 CPoint SymmetricAxis(char style) const;//style取'x','y'和'o'分别表示按x轴, y轴, 原点对称 void input(); //以x,y 形式输入坐标点 void output(); //以(x,y) 形式输出坐标点 }; double CPoint::Distance1(CPoint p) const { double dx=x-p.x,dy=y-p.y; return sqrt(dx*dx+dy*dy); } double CPoint::Distance0() const { return sqrt(x*x+y*y); } CPoint CPoint::SymmetricAxis(char style) const { CPoint p; if(style=='x') { p.x=x; p.y=-y; } else if(style=='y') { p.x=-x; p.y=y; } else if(style=='o') { p.x=-x; p.y=-y; } return p; } void CPoint::input() { char e; double xx,yy; cout<<"请输入坐标:"<<endl; cin>>e>>xx>>e>>yy>>e; x=xx; y=yy; } void CPoint::output() { cout<<"("<<x<<","<<y<<")"<<endl; } int main() { CPoint p1,p2,p; p1.input(); p2.input(); cout<<"两点之间的距离为:"<<p1.Distance1(p2)<<endl; cout<<"到原点之间的距离为:"<<p2.Distance0()<<endl; p=p2.SymmetricAxis('x'); cout<<"与X轴对称的坐标为:"; p.output(); p=p2.SymmetricAxis('y'); cout<<"与Y轴对称的坐标为:"; p.output(); p=p2.SymmetricAxis('o'); cout<<"与原点对称的坐标为:"; p.output(); return 0; }
运行结果:
相关文章推荐
- 第五周上机实践项目1-体验常成员函数
- 第五周 课后实践:项目一——体验常成员函数
- 第五周上机项目1 体验常成员函数
- 第五周上机项目1体验常成员函数
- 第六周上机实践项目——体验常成员函数
- 第五周上机实践项目——分数类的雏形
- 第19周上机实践项目1——体验动态链表(6)
- 第七周上机实践项目——体验深复制函数
- 第五周项目1 体验常成员函数
- 第19周上机实践项目1——动态链表体验(1)
- 第19周上机实践项目1——动态链表体验(5)
- 第13周上机实践项目6——体验文件操作(2-实战)
- 第五周上机实践项目——三角形拓展之矩形
- 第五周项目一 体验常成员函数
- 第五周上机实践项目2——对象作为数据成员
- 第19周上机实践项目1——动态链表体验(4)
- 第七周上机实践项目——深复制体验
- 第六周上机项目一 体验常成员函数.
- 第十四周上机实践项目——体验虚析构函数
- 第五周上机实践项目——C#之上浮下沉