* 程序頭部註釋開始
* 程序的版權和版本聲明部分
* Copyright (c) 2012, 煙臺大學計算機學院學生
* Copyright (c) 2012, 煙臺大學計算機學院學生
* All rights reserved.
* 文件名稱: 座標
* 作 者: 劉程程
* 完成日期: 2012 年3 月 27日
* 版 本 號: v1.0
* 對任務及求解方法的描述部分
* 輸入描述:……
* 問題描述:……
* 程序輸出:……
* 程序頭部的註釋結束
#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 = 0, double yy = 0);
double Distance(Cpoint p) const; //兩點之間的距離
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;
}
//輸入座標點
void Cpoint::input()
{
char ch;
cout << "請輸入座標點(格式x,y):";
while(1)
{
cin >> x >> ch >> y;
if (ch == ',') break;
cout << "輸入的數據格式不符合規範,請重新輸入!";
}
}
//輸出座標點
void Cpoint::output()
{
cout << '(' << x << ',' << ')' <<endl;
}
//求兩點之間的距離
double Cpoint::Distance(Cpoint p) const
{
double d;
d = sqrt((p.x - x)*(p.x - x) + (p.y - y)*(p.y -y));//將(p.x-this->x)可以更便於理解,d是當前點*this和參數給出的點p間的距離
return d;
}
//求點到原點的距離
double Cpoint::Distance0() const
{
double d;
d = sqrt(x*x + y*y);
return d;
}
//求對稱點
Cpoint Cpoint::SymmetricAxis(SymmetricStyle style) const
{
Cpoint p(this->x,this->y);//用複製構造函數時,寫作p(*this)
switch(style)
{
case axisx:
p.y = -y;break;
case axisy:
p.x = -x;break;
case point:
p.x=-x;p.y=-y;
}
return p;
}
void main( )
{
double distance;
Cpoint p1,p2,p;
cout<<"第1個點p1,";
p1.input();
cout<<"第2個點p2,";
p2.input();
distance=p1.Distance(p2);
cout<<"兩點的距離爲:"<<distance<<endl;
distance=p1.Distance0();
cout<<"p1到原點的距離爲:"<<distance<<endl;
p=p1.SymmetricAxis(axisx);
cout<<"p1關於x軸的對稱點爲:";
p.output();
p=p1.SymmetricAxis(axisy);
cout<<"p1關於y軸的對稱點爲:";
p.output();
p=p1.SymmetricAxis(point);
cout<<"p1關於原點的對稱點爲:";
p.output();
system("pause");
}
感悟:認真看枚舉類型,計算兩點之間的距離是重點!!