//
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <cstring>
class array
{
public:
//friend std::ostream &operator<<(std::ostream &os, const array &a);
//friend array& operator+(const array& a, const array& b);
array& operator+(const array& a);
array &operator+=(const array &a);
bool operator>(const array &a) const;
//friend bool operator<=(const array&a,const array&b);
bool operator<=(const array&a) const;
bool operator>=(const array&a) const;
bool operator<(const array&a) const;
bool operator==(const array&a) const;
int& operator[](int index);
//array();
//array();
void operator=(const array& a);
array(const array & a);
array(int n);
//void operator=(const array &a);
~array();
void print();
private:
int* arr_;
int len_;
};
#endif
#include "array.h"
using namespace std;
int& array::operator[](int index)
{
return *(arr_+index);
}
// array::array():arr_(new int[1])
// {
// }
void array::operator=(const array& a)
{
int l=a.len_;
arr_=new int[l];
for(int i=0;i<l;++i)
{
*(arr_+i)=*(a.arr_+i);//cout<<"exe"<<endl;
}
}
array::array(const array & a)
{
int l=a.len_;
arr_=new int[l];
for(int i=0;i<l;++i)
{
*(arr_+i)=*(a.arr_+i);//cout<<"exe"<<endl;
}
}
// void array::operator=(const array &a)
// {
// arr_=new int[1];
// }
array::array(int n)
{
arr_=new int[n];
len_=n;
}
array::~array()
{
delete[] arr_;
}
void array::print()
{
cout<<*arr_<<endl;
}
// ostream &operator<<(std::ostream &os, const array &a)
// {
// for(int i=0;i<a.len_;++i)
// {
// os<<*(a.arr_+i)<<" ";
// }
// }
array & array::operator+=(const array &a)
{
// "foo" + "bar"
int *pt=new int[a.len_+len_];
int j=0;
for(int i=0;i<a.len_+len_;++i)
{
if(i<len_)
{
*(pt+i)=*(arr_+i);
}else
{
*(pt+i)=*(a.arr_+j);
j++;
}
}
delete[] arr_;
arr_=pt;
return *this;
}
array& array::operator+(const array& a)
{
array* c=new array(a.len_+len_);
int *pt=new int[a.len_+len_];
int j=0;
for(int i=0;i<a.len_+len_;++i)
{
if(i<len_)
{
*(pt+i)=*(arr_+i);
}else
{
*(pt+i)=*(a.arr_+j);
j++;
}
}
c->arr_=pt;
return *c;
}
//bool array::operator<=(const array&a,const array&b)
//{
// string bs="";
// string as="";
// char buf[5];
// for(int i=0;i<a.len_;++i)
// {
// memset(buf,0,10);
// sprintf(buf,"%d",*(a.arr_+i));
// as+=buf;
// }
// for(int i=0;i<b.len_;++i)
// {
// memset(buf,0,10);
// sprintf(buf,"%d",*(b.arr_+i));
// bs+=buf;
// }
// return as<=bs;
// return ture;
//}
bool array::operator<=(const array &a) const
{
string ps="";
string as="";
char buf[1];
for(int i=0;i<a.len_;++i)
{
memset(buf,0,1);
sprintf(buf,"%d",*(a.arr_+i));
as+=buf;
}
for(int i=0;i<len_;++i)
{
memset(buf,0,1);
sprintf(buf,"%d",*(arr_+i));
ps+=buf;
}
return ps<=as;
}
bool array::operator>(const array &a) const
{
string ps="";
string as="";
char buf[1];
for(int i=0;i<a.len_;++i)
{
memset(buf,0,1);
sprintf(buf,"%d",*(a.arr_+i));
as+=buf;
}
for(int i=0;i<len_;++i)
{
memset(buf,0,1);
sprintf(buf,"%d",*(arr_+i));
ps+=buf;
}
return ps>as;
}
bool array::operator>=(const array&a) const
{
string ps="";
string as="";
char buf[1];
for(int i=0;i<a.len_;++i)
{
memset(buf,0,1);
sprintf(buf,"%d",*(a.arr_+i));
as+=buf;
}
for(int i=0;i<len_;++i)
{
memset(buf,0,1);
sprintf(buf,"%d",*(arr_+i));
ps+=buf;
}
return ps>=as;
}
bool array::operator<(const array&a) const
{
string ps="";
string as="";
char buf[1];
for(int i=0;i<a.len_;++i)
{
memset(buf,0,1);
sprintf(buf,"%d",*(a.arr_+i));
as+=buf;
}
for(int i=0;i<len_;++i)
{
memset(buf,0,1);
sprintf(buf,"%d",*(arr_+i));
ps+=buf;
}
return ps<as;
}
bool array::operator==(const array&a) const
{
string ps="";
string as="";
char buf[1];
for(int i=0;i<a.len_;++i)
{
memset(buf,0,1);
sprintf(buf,"%d",*(a.arr_+i));
as+=buf;
}
for(int i=0;i<len_;++i)
{
memset(buf,0,1);
sprintf(buf,"%d",*(arr_+i));
ps+=buf;
}
return ps==as;
}
///
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
class complex
{
public:
complex();
complex(int real, int img);
complex(const complex &c);
friend std::ostream &operator<<(std::ostream &os,const complex &c);
//std::ostream &operator<<(std::ostream &os);
complex& operator=(const complex &c);
complex operator+(const complex &c);
complex operator-(const complex &c);
complex operator*(const complex &c);
void print();
int mod();
private:
int real_;
int img_;
};
#endif
#include "complex.h"
using namespace std;
complex::complex()
{
real_=0;
img_=0;
}
complex::complex(int real, int img):real_(real),img_(img)
{
}
void complex::print()
{
cout<<real_<<"+"<<img_<<"i"<<endl;
}
complex::complex(const complex &c)
{
real_=c.real_;
img_=c.img_;
}
complex& complex::operator=(const complex &c)
{
real_=c.real_;
img_=c.img_;
return *this;
}
complex complex::operator+(const complex &c)
{
complex ns;
ns.real_=real_+c.real_;
ns.img_=img_+c.img_;
return ns;
}
complex complex::operator-(const complex &c)
{
complex ns;
ns.real_=real_-c.real_;
ns.img_=img_-c.img_;
return ns;
}
complex complex::operator*(const complex &c)
{
complex ns;
ns.real_=real_*c.real_;
ns.img_=img_*c.img_;
return ns;
}
int complex::mod()
{
return real_*real_+img_*img_;
}
// ostream &complex::operator<<(ostream &os)
// {
// os<<real_<<"+"<<img_<<"i";
// return os;
// }
ostream& operator<<(ostream &os,const complex &c)
{
os<<c.real_<<"+"<<c.img_<<"i"<<endl;
return os;
}
二元操作写成友元的形式更好,便于查看