/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.* 文件名称:
* 作 者: 石丽君
* 完成日期: 2012年3 月27 日
* 版 本 号:
* 对任务及求解方法的描述部分
* 输入描述: 设计平面坐标点类,计算两点之间距离、到原点距离、关于坐标轴和原点的对称点等
* 问题描述:
* 程序输出:
* 程序头部的注释结束*/
#include<iostream>
#include<Cmath>
using namespace std;
enum SymmetricStyle { axisx,axisy,point};//分别表示按x轴, y轴, 原点对称
class CPoint
{
private:
mutable double x; // 横坐标
mutable 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;
}
//以x,y 形式输入坐标点
void CPoint::input()
{
char c;
double xx,yy;
while(1)
{
cout<<"请按照x,y格式输入"<<endl;
cin>>xx>>c>>yy;
if(c!=',')
cout<<"格式不对,请重新输入"<<endl;
else
break;
}
x=xx;
y=yy;
}
// 到原点的距离
double CPoint::Distance0()const
{
double s;
s=sqrt(x*x+y*y);
return s;
}
// 两点之间的距离(一点是当前点,另一点为参数p)
double CPoint::Distance(CPoint p) const
{
double s;
s=sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
return s;
}
// 返回对称点
CPoint CPoint::SymmetricAxis(SymmetricStyle style) const
{
switch(style)
{
case axisx:
y=-y;
cout<<'('<<x<<','<<y<<')'<<endl;break;
case axisy:
x=-x;
cout<<'('<<x<<','<<y<<')'<<endl;break;
case point:
x=-x;
y=-y;
cout<<'('<<x<<','<<y<<')'<<endl;break;
}
return 0;
}
//以(x,y) 形式输出坐标点
void CPoint::output()
{
cout<<'('<<x<<','<<y<<')';
}
int main()
{
CPoint c1(3,4),c2(5,6),c3;
cout<<"这两点";
c1. output();
c2.output();
cout<<"之间的距离为:"<<c1. Distance(c2);
cout<<endl;
c1. output();
cout<<"到原点的距离为:"<<c1.Distance0();
cout<<endl;
c2. output();
cout<<"到原点的距离为:"<<c2.Distance0();
cout<<endl;
c3.input();
cout<<"这两点";
c1. output();
c3.output();
cout<<"之间的距离为:"<<c3. Distance( c1);
cout<<endl;
c3. output();
cout<<"到原点的距离为:"<<c3.Distance0();
cout<<endl;
c3.output();
cout<<"关于x轴的对称点:";
c3.SymmetricAxis(axisx);
cout<<endl;
c1.output();
cout<<"关于y轴的对称点:";
c1.SymmetricAxis(axisy);
cout<<endl;
c2.output();
cout<<"关于原点的对称点:";
c2.SymmetricAxis(point);
cout<<endl;
system("pause");
return 0;
}
感言:用const的成员函数改变私有成员的值。
cout里面不能再加输出函数output.