现在的位置: 首页 > 综合 > 正文

平面坐标类

2013年03月09日 ⁄ 综合 ⁄ 共 1434字 ⁄ 字号 评论关闭
/* 
* 程序的版权和版本声明部分 
* Copyright (c)2012, 烟台大学计算机学院学生 
* All rightsreserved. 
* 文件名称: object.cpp 
* 作者:刘明亮 
* 完成日期: 2013年 4 月6日 
* 版本号: v1.0 
* 输入描述: 
* 问题描述: 
* 程序输出: 
*/  

#include<iostream>
#include<Cmath>
using namespace std;
class CPoint
{
private:
	double x;  // 横坐标
	double y;  // 纵坐标
public:
	CPoint(double xx=0,double yy=0);
	void Distance(CPoint p) const;   // 两点之间的距离(一点是当前点,另一点为参数p)
	void Distance0() const;          // 到原点的距离
	void 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;
}
//以x,y 形式输入坐标点
void CPoint::input()
{
	char n,s,t;
	cout<<"输入点的坐标,(如:(x,y)):";
	while(1)
	{
	cin>>n>>x>>s>>y>>t;
	if(n=='(' || t==')' || s==',')break;
	cout<<"格式错误,请重新输入!"<<endl;
	}
}
 //以(x,y) 形式输出坐标点
void CPoint::output()
{
	cout<<"("<<x<<","<<y<<")"<<endl;
}
// 两点之间的距离(一点是当前点,另一点为参数p)
void CPoint::Distance(CPoint p)const
{
	cout<<"两点间的距离为:"<<sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y))<<endl;
}
// 到原点的距离
void CPoint::Distance0()const
{
	cout<<"到原点的距离是:"<<sqrt(x*x+y*y)<<endl;
}
//style取'x','y'和'o'分别表示按x轴, y轴, 原点对称
void CPoint::SymmetricAxis(char style)const
{
	switch(style)
	{
	case '0':
		cout<<"点1关于原点的对称点为:"<<"("<<-x<<","<<-y<<")"<<endl;
		break;
	case 'x':
		cout<<"点1关于x轴的对称点为:"<<"("<<x<<","<<-y<<")"<<endl;
		break;
	case 'y':
		cout<<"点1关于y轴的对称点为:"<<"("<<-x<<","<<y<<")"<<endl;
		break;
	default:
		cout<<"输入错误,请重新输入:"<<endl;
		break;
	}
}


int main()
{
	CPoint p1,p2;
	cout<<"点1,";
	p1.input();
	cout<<"点2,";
	p2.input();
	p1.Distance(p2);
	p1.Distance0();
	p1.SymmetricAxis('0');
	p1.SymmetricAxis('x');
	p1.SymmetricAxis('y');
	return 0;
}

抱歉!评论已关闭.