OJ第六批——Problem A: A代码完善--向量的运算

问题及代码:

Problem A: A代码完善--向量的运算

Time Limit: 1 Sec   Memory Limit: 128 MB
Submit: 208   Solved: 127
[ Submit][ Status][ Web Board]

Description

注:本题只需要提交填写部分的代码,请按照C++方式提交。

对于二维空间中的向量,实现向量的减法和取负运算。如向量A(x1,y1)和B(x2,y2),
则 A-B 定义为 (x1-x2,y1-y2) , -A 定义为 (-x1,-y1) 。

#include <stdio.h>
#include <iostream>
using namespace std;
class Vector
{
private :
    int x,y;
public:
    void setValue(int x,int y)
    {
        this->x=x;
        this->y=y;
    }
    void output()
    {
        cout<<"x="<<x<<",y="<<y<<endl;
    }
    Vector operator-();
    friend Vector operator- (Vector &v1,Vector &v2);
};

int  main()
{
    Vector A,B,C;
    int x,y;
    cin>>x>>y;
    A.setValue(x,y);
    cin>>x>>y;
    B.setValue(x,y);
    C = A - B;
    C.output();
    C = -C;
    C.output();
    return 0;
}
/*
 请在该部分补充缺少的代码

*/

Input

两个向量

Output

向量减法和向量取负运算后的结果

Sample Input

10 20 15 25

Sample Output

x=-5,y=-5
x=5,y=5

HINT

#include <stdio.h> 
#include <iostream> 
using namespace std; 
class Vector 
{ 
private : 
    int x,y; 
public: 
    void setValue(int x,int y) 
    { 
        this->x=x; 
        this->y=y; 
    } 
    void output() 
    { 
        cout<<"x="<<x<<",y="<<y<<endl; 
    } 
    Vector operator-(); 
    friend Vector operator- (Vector &v1,Vector &v2); 
}; 
  
int  main() 
{ 
    Vector A,B,C; 
    int x,y; 
    cin>>x>>y; 
    A.setValue(x,y); 
    cin>>x>>y; 
    B.setValue(x,y); 
    C = A - B; 
    C.output(); 
    C = -C; 
    C.output(); 
    return 0; 
} 
Vector operator- (Vector &v1,Vector &v2) 
{ 
    Vector v; 
    v.x=v1.x-v2.x; 
    v.y=v1.y-v2.y; 
    return v; 
} 
  
Vector Vector::operator-() 
{ 
    Vector v; 
    v.x=-x; 
    v.y=-y; 
    return v; 
} 


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值