第7周实验报告2

实验目的:利用成员函数,友函数和一般函数实现求两点间距离


* 程序头部注释开始
* 程序的版权和版本声明部分
* 烟台大学计算机学院学生
* 文件名称:         求两点间距离               
* 作    者:          胡斌                
* 完成日期:  2012 年 4月2 
* 版本号:     v1.0    

* 程序头部的注释结束(此处也删除了斜杠)

#include <iostream>
#include <cmath> 
using namespace std;
class CPoint  
{private:  
    double x;  // 横坐标 
    double y;  // 纵坐标 
 public: 
    CPoint(double xx=0,double yy=0):x(xx),y(yy){}  
   //请继续写需要的代码
	double getx(){return x;}
	double gety(){return y;}
    void input();  
	double Distance1(CPoint p);//成员函数
	friend double Distance2(CPoint &p);//友元函数
};
double Distance3(CPoint &p);//一般函数

void CPoint::input()  //以x,y 形式输入坐标点  
    {  
        char ch;  
        cout<<"请输入坐标点(格式x,y ):";  
        while(1)  
        {  
            cin>>x>>ch>>y;  
            if(ch != ',')  
            {cout<<"输入格式不对";}  
            else  
                break;  
        }  
    }  
double CPoint::Distance1(CPoint p)
{
 return (sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)));  
}
double Distance2(CPoint &p)
{
   CPoint p1;
   cout<<"请输入一个点:";  
   p1.input(); 

   return(sqrt((p1.x-p.x)*(p1.x-p.x)+(p1.y-p.y)*(p1.y-p.y)));

}
double Distance3(CPoint &p)
{
CPoint p1;
   cout<<"请输入一个点:";  
   p1.input(); 

   double a,b,c,d;
   a=p1.getx();
   b=p1.gety();
   c=p.getx();
   d=p.gety();
  
   return (sqrt((a-c)*(a-c)+(b-d)*(b-d)));
}
int main()
{
   double distance;

   CPoint p(5,4);
   CPoint p1(0,0);

   distance=p.Distance1( p1);  
   cout<<"两点间点的距离为:"<<distance<<endl; 

   distance=Distance2(p1);
   cout<<"两点间点的距离为:"<<distance<<endl;

   distance=Distance3(p1);
   cout<<"两点间点的距离为:"<<distance<<endl;

   system("pause");

   return 0;


}

 

截图:


 

 

友函数用的不习惯!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值