自考新教材-p176_5(2)

源程序:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class point
{
private:
double x, y;
public:
point() {};
point(double a, double b) :x(a), y(b) {};
friend ostream & operator<<(ostream &os,const point &c); //友元,流插入
friend istream & operator>>(istream &is,point &c); //友元,流提取
};

ostream &operator<<(ostream &os, const point &c)
{
//if (c.y >= 0)
// os << c.x << "," << c.y << endl;
//else
// os << c.x << "," << (-c.y) << endl;

os << sqrt(c.x*c.x + c.y*c.y);

return os;
}

istream &operator>>(istream & is, point &c)
{
is >> c.x >> c.y;
return is;
}

int main()
{
point c, c1;
int n;
//cout << "输入两个坐标点和一个整数,以空格分隔" << endl;
cout << "输入第一个坐标点:";
cin >> c;
cout << "输入第二个坐标点:";
cin >> c1;
cout << "输入一个整数:";
cin >> n;

//cin >> c >> c1 >> n;
cout << "第一个坐标点到原点的距离:" << c << endl << "整数为:"<<n << endl<< "第二个坐标点到原点的距离:" <<c1<<endl;
system("pause");
return 1;

}

 

运行结果:

 

posted @ 2020-02-08 11:20  bobo哥  阅读(180)  评论(0编辑  收藏  举报