/*
* Copyright (c) 2011, 煙臺大學計算機學院
* All rights reserved.
* 作 者:王靜
* 完成日期:2013 年 4 月 9 日
* 版 本 號:v1.0
* 輸入描述:
* 問題描述:
* 程序輸出:
* 問題分析:
* 算法設計:略
*/
#include <iostream>
#include <cmath>
using namespace std;
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(char style)const;//style取'x','y'和'o'分別表示按x軸, y軸, 原點對稱
void input(); //以x,y 形式輸入座標點
void output(); //以(x,y) 形式輸出座標點
};
CPoint::CPoint(double xx,double yy)
{
x=xx;
y=yy;
}
double CPoint::Distance(CPoint p) const
{
double d;
d=sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
return d;
}
double CPoint::Distance0() const
{
double d;
d=sqrt(x*x+y*y);
return d;
}
CPoint CPoint::SymmetricAxis(char style)const
{
CPoint p(x,y);
switch(style)
{
case('x'):p.y=-y;break;
case('y'):p.x=-x;break;
case('o'):p.x=-x;p.y=-y;break;
}
return p;
}
void CPoint::input() //以x,y 形式輸入座標點
{
int a,b;
char s;
cout<<"請輸入座標"<<endl;
cin>>a>>s>>b;
if(s==':'){
x=a;y=b;
}else{
cout<<"格式錯誤"<<endl;
input();
}
}
void CPoint::output() //以(x,y) 形式輸出座標點
{
cout<<'('<<x<<','<<y<<')'<<endl;
}
int main()
{
CPoint A,B;
A.input();B.input();
cout<<"A,B點的距離爲:"<<A.Distance(B)<<endl;
cout<<"A關於原點的對稱點:"<<endl;
(A.SymmetricAxis('o')).output();
return 0;
}
運行結果:
(貼圖)
心得體會: