#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;
}
来源:https://www.cnblogs.com/sysu-eeman-yang/p/1202716-smie-yang.html