2014秋C++第19周 项目4参考 点和距离

课程主页在 http://blog.csdn.net/sxhelijian/article/details/39152703,课程资源在 云学堂“贺老师课堂”同步展示,使用的帐号请到课程主页中查看。 


【项目4-点和距离】
读程序,写出函数的定义,注意其中枚举类型的用法

enum SymmetricStyle {axisx,axisy,point};//分别表示按x轴, y轴, 原点对称
struct Point{
	double x;  // 横坐标
	double y;  // 纵坐标
};
double distance1(Point p1, Point p2);   // 两点之间的距离,如果用distance,将会与命名空间std中也已经定义的distance函数重名
double distance0(Point p1);
Point symmetricAxis(Point p,SymmetricStyle style);   //返回对称点
int main( ){
	Point p1={1,5},p2={4,1},p;
	cout<<"两点的距离为:"<<distance1(p1,p2)<<endl;
	cout<<"p1到原点的距离为:"<<distance0(p1)<<endl;
	p=symmetricAxis(p1,axisx);
	cout<<"p1关于x轴的对称点为:"<<"("<<p.x<<", "<<p.y<<")"<<endl;
	p=symmetricAxis(p1,axisy);
	cout<<"p1关于y轴的对称点为:"<<"("<<p.x<<", "<<p.y<<")"<<endl;
	p=symmetricAxis(p1,point);
	cout<<"p1关于原点的对称点为:"<<"("<<p.x<<", "<<p.y<<")"<<endl;
	return 0;
}
// 求两点之间的距离  
double distance1(Point p1,Point p2)  
{  
    double d;  
    ……  
    return d;  
}  
  
// 求点到原点的距离  
double distance0(Point p)  
{  
    double d;  
    ……  
    return d;  
}  
  
// 求对称点  
Point symmetricAxis(Point p1,SymmetricStyle style)  
{  
    Point p;  
    ……
    return p;  
}  


参考解答:

#include <iostream>
#include <Cmath>
using namespace std;

enum SymmetricStyle {axisx,axisy,point};//分别表示按x轴, y轴, 原点对称
struct Point
{
	double x;  // 横坐标
	double y;  // 纵坐标
};
double distance1(Point p1, Point p2);   // 两点之间的距离
double distance0(Point p1);
Point symmetricAxis(Point p,SymmetricStyle style);   //返回对称点

int main( )
{
	Point p1={1,5},p2={4,1},p;
	cout<<"两点的距离为:"<<distance1(p1,p2)<<endl;
	cout<<"p1到原点的距离为:"<<distance0(p1)<<endl;
	p=symmetricAxis(p1,axisx);
	cout<<"p1关于x轴的对称点为:"<<"("<<p.x<<", "<<p.y<<")"<<endl;
	p=symmetricAxis(p1,axisy);
	cout<<"p1关于y轴的对称点为:"<<"("<<p.x<<", "<<p.y<<")"<<endl;
	p=symmetricAxis(p1,point);
	cout<<"p1关于原点的对称点为:"<<"("<<p.x<<", "<<p.y<<")"<<endl;
	return 0;
}

// 求两点之间的距离
double distance1(Point p1,Point p2)
{
	double d;
	d=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
	return d;
}

// 求点到原点的距离
double distance0(Point p)
{
	double d;
	d=sqrt(p.x*p.x+p.y*p.y);
	return d;
}

// 求对称点
Point symmetricAxis(Point p1,SymmetricStyle style)
{
	Point p;
	p.x=p1.x;
	p.y=p1.y;
	switch(style)
	{
	case axisx:
		p.y=-p1.y; break;
	case axisy:
		p.x=-p1.x; break;
	case point:
		p.x=-p1.x;p.y=-p1.y;
	}
	return p;
}

感谢单昕昕发现distance在命名上的问题。





=================== 迂者 贺利坚 CSDN博客专栏=================
|== IT学子成长指导专栏 专栏文章的分类目录(不定期更新) ==|
|== C++ 课堂在线专栏  贺利坚课程教学链接(分课程年级) ==|
|== 我写的书——《逆袭大学——传给IT学子的正能量》    ==|
===== 为IT菜鸟起飞铺跑道,和学生一起享受快乐和激情的大学 =====

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迂者-贺利坚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值