当前位置:网站首页>OOP 向量加减(友元+拷贝构造)
OOP 向量加减(友元+拷贝构造)
2022-06-25 04:01:00 【SZU治愈系BUG】
目录
题目描述
定义一个CVector类如下:
class CVector
{
private:
int *data; //存储n维向量
int n; //向量维数
public:
CVector(int n1, int *a);
CVector(CVector &c);
~CVector() { delete data; }
void print();
friend CVector Add(const CVector V1, const CVector V2);
friend CVector Sub(const CVector V1, const CVector V2);
};
要求如下:
根据输入和输出实现CVector类的成员函数,并分别实现友元函数Add、Sub来实现两向量的相加和相减。
输入
第一行,输入测试次数t
每组测试数据格式如下:
向量维数n
第一个n维向量值
第二个n维向量值
输出
对每组测试数据,输出它们相加和相减后的结果
输入样例1
2
3
1 2 3
4 5 6
5
1 2 3 4 5
-1 2 4 6 10
输出样例1
5 7 9
-3 -3 -3
0 4 7 10 15
2 0 -1 -2 -5
AC代码
#include<iostream>
#include<string>
#include<algorithm>
#include<cmath>
#include<iomanip>
using namespace std;
class CVector {
private:
int *data; //存储n维向量
int n; //向量维数
public:
CVector(int n1, int *a);
CVector(const CVector &c);
~CVector() {
delete data;
}
void print();
friend CVector Add(const CVector V1, const CVector V2);
friend CVector Sub(const CVector V1, const CVector V2);
};
CVector::CVector(int n1,int *a){
n=n1;
data=new int[n1];
for(int i=0;i<n1;i++)
data[i]=a[i];
}
CVector::CVector(const CVector &c){
n=c.n;
data=new int[c.n];
for(int i=0;i<c.n;i++)
data[i]=c.data[i];
}
void CVector::print(){
for(int i=0;i<n-1;i++)
cout<<data[i]<<' ';
cout<<data[n-1]<<endl;
}
CVector Add(const CVector V1, const CVector V2){
CVector temp(V1);
for(int i=0;i<temp.n;i++)
temp.data[i]=V1.data[i]+V2.data[i];
return temp;
}
CVector Sub(const CVector V1, const CVector V2){
CVector temp(V1);
for(int i=0;i<temp.n;i++)
temp.data[i]=V1.data[i]-V2.data[i];
return temp;
}
int main() {
int t,n,i;
cin>>t;
while(t--){
cin>>n;
int *p=new int [n];
for(i=0;i<n;i++)
cin>>p[i];
CVector a(n,p);
for(i=0;i<n;i++)
cin>>p[i];
CVector b(n,p);
Add(a,b).print();
Sub(a,b).print();
delete[] p;
}
}边栏推荐
- Laravel document sorting 3. CSRF protection
- LeetCode 劍指Offer II 091 粉刷房子[動態規劃] HERODING的LeetCode之路
- Blob page in gbase 8s
- 什么是存储引擎以及MySQL常见的三种数据库存储引擎
- CTF_ Web: advanced problem WP (5-8) of attack and defense world expert zone
- CTF_ Web: Advanced questions of attack and defense world expert zone WP (1-4)
- SQL注入详解
- 无法安装redis接口
- CTF_ Web: how to recognize and evaluate a regular expression
- [kubernetes series] installation and use of Helm
猜你喜欢

Gbase 8s index R tree

i. Max development board learning record

简单的恶意样本行文分析-入门篇

CTF_ Web: how to recognize and evaluate a regular expression

cnpm : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\cnpm.ps1,因为在此系统上禁止运行脚本。

Record small knowledge points

CTF_ Web: basic 12 questions WP of attack and defense world novice zone
![Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding](/img/ad/69fce7cf064479a0ddd477fb935de2.png)
Leetcode points to the leetcode road of offering II 091 house painting [dynamic planning] heroding

A detailed summary of TCP connection triple handshake

Unit test coverage
随机推荐
第二十五周记录
2020.3.3 notes async/await and promise and Then processes and threads
Shutter fittedbox component
Openmmlab environment configuration
jsz中的join()
Should I use on or where for the left join
js中的concat()
GBASE 8s的并行操作问题场景描述
Use of deferred environment variable in gbase 8s
微信小程序父子组件之间传值
Smart contract learning materials
What is persistence? What are RDB and AOF in redis persistence?
Classification of gbase 8s locks
GBASE 8s的包
Value transfer between parent and child components of wechat applet
关于TCP连接四次握手(或者叫四次挥手)的详细总结
Cnpm: unable to load file c:\users\administrator\appdata\roaming\npm\cnpm PS1 because running scripts is prohibited on this system.
Nodejs 通过Heidisql连接mysql出现ER_BAD_DB_ERROR: Unknown database 'my_db_books'
ThinkPHP is integrated with esaywechat. What's wrong with wechat payment callback without callback?
CTF_ Web: Advanced questions of attack and defense world expert zone WP (9-14)