1001. Elements and MyVector 2016 4 8

#include<iostream>
#include<string>
using namespace std;

class Elements{

private:

int value;

static int numberOfObjects;

public:

Elements();

Elements(const int &value);//set value

Elements(const Elements &elem);

~Elements();//descructor

Elements & operator=(const Elements &elem);//assignment

Elements & operator=(const int &value);//assignment

friend istream& operator>>(istream& is, Elements & elem);

//input one integer, and set the value

friend ostream& operator<<(ostream& os, const Elements & elem);

//output one integer to ostream

void setValue(const int &value);

//set value

int getValue()const;

//get value

Elements & operator+=(const Elements &elem);

Elements & operator+=(const int &value);

Elements operator+(const Elements &elem)const;

Elements operator+(const int &value)const;

bool operator ==(const Elements &elem)const;

bool operator ==(const int &value)const;

bool operator <(const Elements &elem)const;

bool operator <(const int &value)const;

operator int()const;//converts Elementes to int

static int getNumberOfObjects();//return the number of objects

};

class MyVector{

private:

Elements *elem;

int _size;

public:

MyVector();//default constructor

MyVector(int n);// constructor an Array of n Elements

MyVector(const MyVector &vec);//copy constructor

~MyVector();//destructor

const MyVector & operator=(const MyVector &vec);//assignment

Elements &operator[](int i);//return the references of elem[i]

Elements operator[](int i) const; //return the copy of elem[i]

int size()const;//return the size

bool operator == (const MyVector &vec)const;//judge if it is equal

friend istream& operator>>(istream& is, MyVector & vec);

//first input n as a _size, then input elem[0],elem[1],…,elem[n-1];

friend ostream& operator<<(ostream& os, const MyVector & vec);

//output elem[0],elem[1],elem[2],..,elem[_size-1] in a line, separated by a

//space. e.g  _size = 2 and elem[0]=5,elem[1]=6, you should output

//”5 6” in a line

};

int Elements::numberOfObjects=0;

Elements::Elements(){
   value=0;
   numberOfObjects++;
 
}

Elements::Elements(const int &value){
      this->value = value;
   numberOfObjects++;
}//set value

Elements::Elements(const Elements &elem){
   this->value = elem.value;
   numberOfObjects++;
}

Elements::~Elements(){
   numberOfObjects--;
   numberOfObjects--;
}

Elements& Elements::operator=(const Elements &elem){
   this->value = elem.value;
   return *this;
}

Elements& Elements::operator=(const int &value){
   this->value = value;
   return *this;
}

istream& operator>>(istream& is, Elements & elem){
   is>>elem.value;
   return is;
}

//input one integer, and set the value
ostream& operator<<(ostream& os, const Elements & elem){
   os<<elem.value;
   return os;
}

//output one integer to ostream

void Elements::setValue(const int &value){
   this->value = value;
}

//set value

int Elements::getValue()const{
     return value;
}

Elements & Elements::operator+=(const Elements &elem){
     this->value = this->value + elem.value;
  return *this;
}

Elements & Elements::operator+=(const int &value){
  this->value = this->value + value;
  return *this;
}

Elements Elements::operator+(const Elements &elem)const{
    
     Elements a(this->value);
     a.value =a .value + elem.value;  
     return a;
    
}

Elements Elements::operator+(const int &value)const{
  
     Elements a(this->value);
     a.value =a .value + value;  
     return a;  
  
}

bool Elements::operator ==(const Elements &elem)const{
   
     if(this->value==elem.value) return true;
     else return false;
    
}

bool Elements::operator ==(const int &value)const{

if(this->value==value) return true;
     else return false;
    
}

bool Elements::operator <(const Elements &elem)const{
     if(this->value < elem.value) return true;
     else return false;

}

bool Elements::operator <(const int &value)const{
 
     if(this->value < value) return true;
     else return false;

}

Elements::operator int()const{
 
    return value;
   
}
 
int Elements::getNumberOfObjects(){
 
 return numberOfObjects;
}

MyVector::MyVector(){
 
 elem = NULL;
 _size = 0;
 
}

MyVector::MyVector(int n){
 
 _size = n;
 elem = new Elements[_size];
 
}

MyVector::MyVector(const MyVector &vec){
 
 _size = vec._size;
 elem = new Elements[_size];
 for( int i=0;i<_size;i++){
  elem[i].setValue(vec.elem[i].getValue());
 }
 
}

MyVector::~MyVector(){
 
 delete [] elem;
 
}

const MyVector& MyVector::operator=(const MyVector &vec){
 
 _size = vec._size;
 if(this == &vec) return *this;
 delete [] elem;
 elem = new Elements[_size];
 for( int i=0;i<_size;i++){
  elem[i].setValue(vec[i].getValue());
 }
 return *this;
 
}

Elements& MyVector::operator[](int i){
 
 return elem[i];
 
}

Elements MyVector::operator[](int i) const{
  
   return elem[i];
   
}

int MyVector::size()const{
  
   return this->_size;
  
}

bool MyVector::operator == (const MyVector &vec)const{
 
     if(this->_size != vec._size ) return false;
     else {
      for(int i=0;i<vec._size;i++){
       if(vec.elem[i].getValue()!=this->elem[i].getValue())
       return false;
      }
      return true;
     }
    
}

istream& operator>>(istream& is, MyVector & vec){
 
 is>>vec._size;
 //if(vec._size<=0) exit(1);
 delete [] vec.elem;
 vec.elem = new Elements[vec._size];
 for(int i=0;i<vec._size;i++){
  is>>vec.elem[i];
 }
 return is;
}

//first input n as a _size, then input elem[0],elem[1],…,elem[n-1];

ostream& operator<<(ostream& os, const MyVector & vec){
 
 for(int i=0;i<vec._size-1;i++){
  os<<vec.elem[i]<<" ";
 }
 os<<vec.elem[vec._size-1];
 return os;
}

Elements getSum(const MyVector &vec)

{

Elements sum = 0;

for(int i=0;i<vec.size();i++)

{

sum += vec[i];

}

return sum;

}

void sample()

{

MyVector vec;

while( cin >> vec )

{

cout << vec << endl;

MyVector vec2 = vec;

Elements sum = getSum(vec);

int average = (int)sum / vec.size();

for(int i=0;i<vec.size();i++)

{

if( vec[i] < average )

{

vec [i] = average;

}

}

cout << vec << endl;

if( vec == vec2 )

{

cout << "They are equal" << endl;

}

}

}

int main()

{

sample();

if(Elements::getNumberOfObjects()!=0)

{

cout << "Error: occupied memory" << endl;

cout << "Too young too simple, sometimes naive!" << endl;

}

return 0;

}

时间: 2025-01-11 15:28:17

1001. Elements and MyVector 2016 4 8的相关文章

(甲)PAT-1001

1001. A+B Format (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B Calculate a + b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). Input Each input fi

I.MX6 Linux udev porting

/*********************************************************************** * I.MX6 Linux udev porting * 声明: * 在嵌入式产品上,我们可以使用mdev来解决热插拔的问题,同时也经常看到 * udev,所以尝试来移植一下,但是最终发现她会丢失内核阶段产生的uevent, * 这导致无法生成内核阶段产生的设备节点,目前采用了mdev来做完成内核阶段的 * 设备节点生成,之后使用udev完成热插拔,这

第22章 变易算法

  第22章 变易算法  Modifying  sequence operations    22.1 元素复制copycopy  Copy range of elements (function template)      22.2 反向复制copy_backwardcopy_backward  Copy range of elements backwards (function template)      22.3 元素交换swapswap  Exchange values of two

NGINX由入门到精通:编译安装nginx

一.环境准备1.系统和内内核版本 [[email protected] ~]# cat /etc/redhat-release CentOS release 6.8 (Final) [[email protected] ~]# uname -r 2.6.32-642.el6.x86_64 2.主机名称和IP地址 [[email protected] ~]# hostname  linux-node1.ourplm.com [[email protected] ~]# hostname -I 19

PHP引擎php.ini参数优化

参数以外常见的PHP优化方法手段 1)php引擎缓存加速优化 #xcache.ZendOpcache.eaccelerator #http://www.lichengbing.cn/archivers/270.html 2) 使用tmpfs作为缓存加速缓存的文件目录 tmpfs,基于内存的文件系统,加快转存暂存文件的速度 [[email protected] ~]# mount -t tmpfs tmpfs /dev/shm -o size=256m [[email protected] ~]#

聊一聊前端模板与渲染那些事儿

欢迎大家收看聊一聊系列,这一套系列文章,可以帮助前端工程师们了解前端的方方面面(不仅仅是代码): https://segmentfault.com/blog/frontenddriver 作为现代应用,ajax的大量使用,使得前端工程师们日常的开发少不了拼装模板,渲染模板.我们今天就来聊聊,拼装与渲染模板的那些事儿. 如果喜欢本文请点击右侧的推荐哦,你的推荐会变为我继续更文的动力 1 页面级的渲染 在刚有web的时候,前端与后端的交互,非常直白,浏览器端发出URL,后端返回一张拼好了的HTML串

使用 Raspberry Pi 上的传感器在 Node.js 中创建一个 IoT Bluemix 应用程序

先决条件 一个IBM Bluemix 帐号,一个 Raspberry Pi 2 或 3,一个 PIR 运动传感器 适用于本文的 Github 存储库 如果您是一位精明的 Bluemix 开发人员,您可能只想看看如何在 node.js 中与 IoT 建立连接,或者只想了解如何从此 github 存储库中拉取我的代码. git clone https://github.com/nicolefinnie/iot-nodejs-tutorial 以下是实现与 IBM IoT 平台连接在一起的 4 个 R

hdu 5868 2016 ACM/ICPC Asia Regional Dalian Online 1001 (burnside引理 polya定理)

Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 208    Accepted Submission(s): 101 Problem Description You may not know this but it's a fact that Xinghai Square is

2016年中国大学生程序设计竞赛(合肥)-重现赛1001 HDU 5961

传递 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 42    Accepted Submission(s): 16 Problem Description 我们称一个有向图G是传递的,当且仅当对任意三个不同的顶点a,,若G中有 一条边从a到b且有一条边从b到c ,则G中同样有一条边从a到c.我们称图G是一个竞赛图,当且仅当它是一个有