E.运算算符重载的练习1

E.运算算符重载的练习1
Time Limit: 1000 MSMemory Limit: 32768 K
Total Submit: 200 (78 users)Total Accepted: 80 (76 users)Special Judge: No
Description
利用面向对象设计方法解决问题。
    在二维直角坐标系中设计点Point类,属性为点在X、Y方向的坐标值(实数)。
    设有点p1与点p2:
    p2=p1+n表示点p1在X、Y方向上位移n(n为实数)后得到点p2。
    r=p1-p2表示点p1与点p2间的距离r(r为实数)。
    根据主函数,完成程序。
Input
只有一组测试数据。
    第一行输入的是点p1在X、Y方向的坐标值以及位移n,数据之间空格分隔。
Output
输出3行数据。
    第一行按照格式输出点p1的坐标。
    第二行按照格式输出点p2的坐标。
    第三行按照格式输出点p1与点p2间的距离。
Sample Input
0 0 10
Sample Output
p1:(0,0)
p2:(10,10)
distance(p1,p2):14.14
Hint
int main()
{
  Point p1,p2;
  double r,n;
  cin>>p1>>n;
  p2=p1+n;
  r=p1-p2;
  cout<<"p1:"<<p1<<endl;
  cout<<"p2:"<<p2<<endl;
  cout<<"distance(p1,p2):"<<setiosflags(ios::fixed)<<setprecision(2)<<r<<endl;
  return 0;
}

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

class Point
{
public:
    double x, y;
};

istream& operator>>(istream &cin,Point& p)
{
    cin >> p.x >> p.y;
    return cin;
}

Point operator+(Point& p,double a)
{
    Point temp;
    temp.x = p.x + a;
    temp.y = p.y + a;
    return temp;
}

double operator-(Point& p1, Point& p2)
{
    double x, y;
    x = pow(p1.x - p2.x, 2);
    y = pow(p1.y - p2.y, 2);
    double z = sqrt(x + y);
    return z;
}

ostream& operator<<(ostream& cout, Point& p)
{
    cout << '(' << p.x << ',' << p.y << ')';
    return cout;
}

int main()
{
    Point p1, p2;
    double r, n;
    cin >> p1 >> n;
    p2 = p1 + n;
    r = p1 - p2;
    cout << "p1:" << p1 << endl;
    cout << "p2:" << p2 << endl;
    cout << "distance(p1,p2):" << setiosflags(ios::fixed) << setprecision(2) << r << endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值