(程序頭部註釋開始) * 程序的版權和版本聲明部分 * Copyright (c) 2011, 煙臺大學計算機學院學生 * All rights reserved. * 文件名稱: Student.cpp * 作 者: 計114-4 張馨 * 完成日期: 2012年 3 月 28日 * 版 本 號: V 1.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<<"輸入的數據格式不符合規範,請重新輸入\n";
}
}
// 輸出座標點
void CPoint::output()
{
cout<<"("<<x<<", "<<y<<")"<<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-x)更改爲(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");
}
上級感言:經過老師的講解還算有點頭緒了。。。