YTU 2945: 编程:五元向量的运算
2945: 编程:五元向量的运算
时间限制: 1 Sec 内存限制: 128 MB
提交: 151 解决: 85
题目描述
用习惯了的运算符操作新定义的类对象,这是OO方法给我们带来的便利。下面要为的五元向量(每个向量有5个元素)装备加法、数乘和输入输出的运算功能,请在begin至end部分,写上需要的代码并提交。
#include <iostream>
using namespace std;
const int N=5; //向量中存储的数据个数,这里确定为5元向量
class MyVector
{
public:
MyVector(){}; //构造函数
MyVector(const MyVector &v); //复制构造函数
friend istream &operator>>(istream &input, MyVector &d); //输入向量
friend ostream &operator<<(ostream &output, const MyVector &d); //输出向量
MyVector operator+(const MyVector &d); //向量相加,当前对象与d对应位置上的元素相加
MyVector operator*(int n); //向量数乘,当前对象的每个元素与n相乘
private:
int data[N]; //在data数组中存储数据
};
MyVector::MyVector(const MyVector &d)
{
for(int i=0; i<N; ++i)
data[i]=d.data[i];
}
//**********begin*****************
//**********end*****************
int main()
{
MyVector d1,d2;
int n;
cin>>d1;
cin>>d2;
cin>>n;
cout<<"+: "<<d1+d2<<endl;
cout<<"*: "<<d1*n<<endl;
return 0;
}
输入
第一个五元向量,五个数之间用空格隔开
第二个五元向量,五个数之间用空格隔开
一个整数
输出
两个五元向量的和: 格式:前后加括号,两数之间用加逗号和一个空格
第一个五元向量与整数的数乘结果,格式同前
(运算规则:和运算为向量对应位置元素相加,数乘运算为各元素分别乘以给定的数。)
样例输入
1 25 41 16 98
5 67 9 55 121
3
样例输出
+: (6, 92, 50, 71, 219)
*: (3, 75, 123, 48, 294)
你 离 开 了 , 我 的 世 界 里 只 剩 下 雨 。 。 。
#include <iostream>
using namespace std;
const int N=5; //向量中存储的数据个数
class MyVector
{
public:
MyVector() {}; //构造函数
MyVector(const MyVector &v); //复制构造函数
friend istream &operator>>(istream &input, MyVector &d);
friend ostream &operator<<(ostream &output, const MyVector &d);
MyVector operator+(const MyVector &d2); //向量相加,当前对象与d2对应位置上的元素相加
MyVector operator*(int n); //向量数乘,当前对象的每个元素与n相乘
private:
int data[N]; //在data数组中存储数据
};
MyVector::MyVector(const MyVector &d)
{
for(int i=0; i<N; ++i)
data[i]=d.data[i];
}
istream &operator>>(istream &input, MyVector &d)
{
for(int i=0; i<5; i++)
input>>d.data[i];
return input;
}
ostream &operator<<(ostream &output, const MyVector &d)
{
int i;
output<<"("<<d.data[0]<<", ";
for(i=1; i<4; i++)
{
output<<d.data[i]<<", ";
}
output<<d.data[i]<<")";
return output;
}
MyVector MyVector::operator+(const MyVector &d)
{
MyVector a;
for(int i=0; i<5; i++)
a.data[i]=d.data[i]+data[i];
return a;
}
MyVector MyVector::operator*(int n)
{
MyVector a;
for(int i=0; i<5; i++)
a.data[i]=n*data[i];
return a;
}
int main()
{
MyVector d1,d2;
int n;
cin>>d1;
cin>>d2;
cin>>n;
cout<<"+: "<<d1+d2<<endl;
cout<<"*: "<<d1*n<<endl;
return 0;
}
#include <iostream>
using namespace std;
const int N=5; //向量中存储的数据个数
class MyVector
{
public:
MyVector() {}; //构造函数
MyVector(const MyVector &v); //复制构造函数
friend istream &operator>>(istream &input, MyVector &d);
friend ostream &operator<<(ostream &output, const MyVector &d);
MyVector operator+(const MyVector &d2); //向量相加,当前对象与d2对应位置上的元素相加
MyVector operator*(int n); //向量数乘,当前对象的每个元素与n相乘
private:
int data[N]; //在data数组中存储数据
};
MyVector::MyVector(const MyVector &d)
{
for(int i=0; i<N; ++i)
data[i]=d.data[i];
}
istream &operator>>(istream &input, MyVector &d)
{
for(int i=0; i<5; i++)
input>>d.data[i];
return input;
}
ostream &operator<<(ostream &output, const MyVector &d)
{
int i;
output<<"("<<d.data[0]<<", ";
for(i=1; i<4; i++)
{
output<<d.data[i]<<", ";
}
output<<d.data[i]<<")";
return output;
}
MyVector MyVector::operator+(const MyVector &d)
{
MyVector a;
for(int i=0; i<5; i++)
a.data[i]=d.data[i]+data[i];
return a;
}
MyVector MyVector::operator*(int n)
{
MyVector a;
for(int i=0; i<5; i++)
a.data[i]=n*data[i];
return a;
}
int main()
{
MyVector d1,d2;
int n;
cin>>d1;
cin>>d2;
cin>>n;
cout<<"+: "<<d1+d2<<endl;
cout<<"*: "<<d1*n<<endl;
return 0;
}
YTU 2945: 编程:五元向量的运算的更多相关文章
- YTU 2611: A代码完善--向量的运算
2611: A代码完善--向量的运算 时间限制: 1 Sec 内存限制: 128 MB 提交: 256 解决: 168 题目描述 注:本题只需要提交填写部分的代码,请按照C++方式提交. 对于二维 ...
- R语言编程艺术# 数据类型向量(vector)
R语言最基本的数据类型-向量(vector) 1.插入向量元素,同一向量中的所有的元素必须是相同的模式(数据类型),如整型.数值型(浮点数).字符型(字符串).逻辑型.复数型等.查看变量的类型可以用t ...
- Linux网络编程(五)
/*Linux网络编程(五)——多路IO复用之select() 网络编程中,使用IO复用的典型场合: 1.当客户处理多个描述字时(交互式输入以及网络接口),必须使用IO复用. 2.一个客户同时处理多个 ...
- C++模板元编程 - 1 基本数据类型和运算
这是博客开通前几天做的,C++的模板没办法存方便的浮点数,算了. 基本类型的设计参考了vczh轮子叔模仿boost的MPL的设计. 话说template和typename写多了真是无限烦人啊,不得已定 ...
- R语言入门:向量的运算
向量之间的加减乘除运算: > x <- 1 > x [1] 1 2 3 4 5 6 7 8 9 10 > x=x+1 > x [1] 2 3 4 5 6 7 8 9 10 ...
- socket编程五种模型
客户端:创建套接字,连接服务器,然后不停的发送和接收数据. 比较容易想到的一种服务器模型就是采用一个主线程,负责监听客户端的连接请求,当接收到某个客户端的连接请求后,创建一个专门用于和该客户端通信的套 ...
- python高级编程之元类(第3部分结束)
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #元编程 #new-style类带来了一种能力,通过2个特殊方法(_ ...
- C++面向对象高级编程(五)类与类之间的关系
技术在于交流.沟通,转载请注明出处并保持作品的完整性. 本节主要介绍一下类与类之间的关系,也就是面向对象编程先介绍两个术语 Object Oriented Programming OOP面向对象编 ...
- YTU 2598: 编程题B-小平智斗自动售货机
2598: 编程题B-小平智斗自动售货机 时间限制: 1 Sec 内存限制: 128 MB 提交: 268 解决: 69 题目描述 LYH自动售货机在销售商品时,具有自动找钱功能.但是找零的最小单 ...
随机推荐
- Web框架下安全漏洞的测试反思
此文已由作者王婷英授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 在平时的测试中,一般情况下,我们都是比较关注功能业务测试,以及对应的接口测试,很少去关注对应的业务设计上存在 ...
- bzoj 1787 Meet 紧急集合
Meet 紧急集合 这个题是在脖子oj(清北某奆佬给起的名字)八中oj(大视野在线评测)上的. 给出bzoj链接. 这个题还是求最近公共祖先的问题. 而该题不同于别的题,它是需要求三个点的最近公共祖先 ...
- 【Kubernetes】Kubernetes删除namespace后持续terminating状态
删除isti和foo的配置文件之后,namespace持续terminating状态,此时也无法再创建istio-system的namespace namespace "istio-syst ...
- 数列分段Section II(二分)
洛谷传送门 输入时处理出最小的答案和最大的答案,然后二分答案即可. 其余细节看代码 #include <iostream> #include <cstdio> using na ...
- (在线工具)JSON字符串转换成Java实体类(POJO)
http://www.bejson.com/json2javapojo/ 付代码代码转换示例: public static FixMixedOrderResponse serialization(St ...
- [转]使用fdisk磁盘分区和 Linux 文件系统
概述 在本文中,学习磁盘分区和 Linux 文件系统相关内容.学习: 创建分区 使用 mkfs 命令来设置 ext2.ext3.ext4.xfs.Reiser v3 和 vfat 文件系统 创建和管理 ...
- Xcode waring: no rule to process file *** 警告提示
在编译程序的时候,Xcode给出了警告:warning: no rule to process file *** 类似的警告, 解决方法: 在[build Phases] -> [Compile ...
- CodeForces 599B Spongebob and Joke
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- eclipse bug之'<>'operator is not allowed for source level below 1.7
eclipse中导入工程,报这个错'<>'operator is not allowed for source level below 1.7,把jdk改成1.7后,提示Android r ...
- 多线程调用COM组件的体会(CoInitialize)
调用任何COM组件之前,你必须首先初始化COM套件环境,即调用CoInitialize或CoInitializeEx.COM套件环境在线程的生存周期内有效,线程退出前需要调用CoUninitializ ...