#include <iostream>
#include <cmath>
using namespace std;
class Complex
{
int rel;
int vir;
public:
Complex(){}
Complex(int rel,int vir):rel(rel),vir(vir)
{}
Complex operator+(const Complex c1);
Complex operator/(const Complex c1);
Complex operator%(const Complex c1);
bool operator>(const Complex c1);
bool operator<(const Complex c1);
bool operator==(const Complex c1);
bool operator&&(const Complex c1);
Complex operator++();
Complex operator--();
Complex operator^(const Complex c1);
Complex operator~();
operator int()
{
return this->rel;
}
void show()
{
cout << "rel=" << rel << " vir=" << vir << endl;
}
int operator()(int a,int b)
{
this ->show();
return a>b?a:b;
}
friend Complex operator-(const Complex c1,const Complex c2);
friend Complex operator*(const Complex c1,const Complex c2);
friend bool operator>=(const Complex c1,const Complex c2);
friend bool operator<=(const Complex c1,const Complex c2);
friend bool operator!=(const Complex c1,const Complex c2);
friend bool operator!(const Complex c1);
friend bool operator||(const Complex c1,const Complex c2);
friend Complex operator++(Complex &c1,int);
friend Complex operator--(Complex &c1,int);
friend ostream &operator<<(ostream &out,Complex c1);
friend istream &operator>>(istream &in,Complex &c1);
friend Complex operator>>(Complex &c1,int pos);
friend Complex operator<<(Complex &c1,int pos);
};
Complex ret(0,0);
Complex Complex::operator+(const Complex c1)
{
Complex temp;
temp.rel=this->rel+c1.rel;
temp.vir=this->vir+c1.vir;
return temp;
}
Complex Complex::operator/(const Complex c1)
{
Complex temp(this->rel/c1.rel,this->vir/c1.vir);
return temp;
}
Complex operator-(const Complex c1,const Complex c2)
{
Complex temp(c1.rel-c2.rel,c2.vir-c2.vir);
return temp;
}
Complex operator*(const Complex c1,const Complex c2)
{
Complex temp;
temp.rel=c1.rel*c2.rel;
temp.vir=c1.vir*c2.vir;
return temp;
}
Complex Complex::operator%(const Complex c1)
{
Complex temp;
temp.rel=this->rel%c1.rel;
temp.vir=this->vir%c1.vir;
return temp;
}
bool Complex::operator>(const Complex c1)
{
return this->rel>c1.rel;
}
bool Complex::operator<(const Complex c1)
{
return this->rel<c1.rel;
}
bool operator>=(const Complex c1,const Complex c2)
{
return c1.rel >= c2.rel;
}
bool operator<=(const Complex c1,const Complex c2)
{
return c1.rel <= c2.rel;
}
bool operator!=(const Complex c1,const Complex c2)
{
return c1.rel != c2.rel;
}
bool Complex::operator==(const Complex c1)
{
return this->rel==c1.rel;
}
bool operator!(const Complex c1)
{
return !(c1.rel||c1.vir);
}
bool Complex::operator&&(const Complex c1)
{
return this->rel&&c1.rel&&this->vir&&c1.vir;
}
bool operator||(const Complex c1,const Complex c2)
{
return c1.rel||c2.rel||c1.vir||c2.vir;
}
Complex Complex::operator++()
{
++this->rel;
++this->vir;
return *this;
}
Complex Complex::operator--()
{
--this->rel;
--this->vir;
return *this;
}
Complex operator++(Complex &c1,int)
{
Complex temp(c1.rel++,c1.vir++);
return temp;
}
Complex operator--(Complex &c1,int)
{
Complex temp(c1.rel--,c1.vir--);
return temp;
}
ostream &operator<<(ostream &out,Complex c1)
{
out << c1.rel << "+" << c1.vir << "i" << endl;
return out;
}
istream &operator>>(istream &in,Complex &c1)
{
in >> c1.rel >> c1.vir;
return in;
}
Complex Complex::operator^(const Complex c1)
{
Complex temp;
temp.rel=this->rel^c1.rel;
temp.vir=this->vir^c1.vir;
return temp;
}
Complex operator>>(Complex &c1,int pos)
{
int m=sizeof(c1.rel)*8;
if(pos>m)
{
cout << "位置不合理" << endl;
return ret;
}
c1.rel/=pow(2,pos);
c1.vir/=pow(2,pos);
Complex temp(c1.rel,c1.vir);
return temp;
}
Complex operator<<(Complex &c1,int pos)
{
int m=sizeof(c1.rel)*8;
if(pos>m)
{
cout << "位置不合理" << endl;
return ret;
}
c1.rel*=pow(2,pos);
c1.vir*=pow(2,pos);
Complex temp(c1.rel,c1.vir);
return temp;
}
Complex Complex::operator~()
{
this->rel=~this->rel;
this->vir=~this->vir;
return *this;
}
int main()
{
Complex c1(4,10),c2(10,4);
cout << c1 << c2;
Complex s1=c1.operator^(c2);
cout << s1;
operator>>(s1,2);
cout << s1;
operator<<(s1,2);
cout << s1;
s1.operator~();
cout << s1;
s1.operator~();
cout << s1;
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
char c = '\0';
class myString
{
private:
char *str; //记录c风格的字符串
int size; //记录字符串的实际长度
public:
//无参构造
myString():str(new char),size(0)/*:str(new char),size(0)*/
{}
//有参构造
myString(char *p,int size):str(new char[size+1]),size(size)
{
strcpy(str,p);
}
myString(string s1):str(new char[s1.size()+1]),size(s1.length())
{
strcpy(str,s1.c_str());
}
//拷贝构造
myString(const myString &other):str(new char[other.size+1]),size(other.size)
{
strcpy(str,other.str);
}
//拷贝赋值函数
myString &operator=(const myString &other)
{
if(&other!=this)
{
delete []str;
str = new char[other.size+1];
strcpy(str,other.str);
this->size = other.size;
}
return *this;
}
//析构函数
~myString()
{
delete []str;
}
//判空函数
bool empty()
{
return size==0;
}
//size函数
int size_()
{
return size;
}
//c_str函数
const char *c_str()
{
return str;
}
//at函数
char &at(int pos)
{
//判断位置合理性
if(pos<0||pos>=size)
{
cout << "位置不合理" << endl;
return c;
}
return str[pos];
}
void show()
{
cout << "str=" << str << " size=" << size << endl;
}
myString operator+(const myString s1);
bool operator>(const myString s1);
bool operator==(const myString s1);
myString operator&&(const myString s1);
friend ostream &operator<<(ostream &out,myString s1);
friend istream &operator>>(istream &in,myString &s1);
};
bool myString::operator==(const myString s1)
{
if(strcmp(this->str,s1.str)==0)
{
return 1;
}
else
return 0;
}
bool myString::operator>(const myString s1)
{
if(strcmp(this->str,s1.str)==1)
{
return 1;
}
else
return 0;
}
myString myString::operator+(const myString s1)
{
myString temp;
strcpy(temp.str,this->str);
strcat(temp.str,s1.str);
temp.size=this->size+s1.size;
return temp;
}
ostream &operator<<(ostream &out,myString s1)
{
out << "str=" << s1.str << " size=" << s1.size << endl;
return out;
}
istream &operator>>(istream &in,myString &s1)
{
s1.str=new char[32];
in>>s1.str;
s1.size=strlen(s1.str);
return in;
}
int main()
{
char str1[]="hello";
myString s1(str1,5);
char str2[]="world";
myString s2(str2,5);
s1.show();
s2.show();
myString s3=s1.operator+(s2);
s3.show();
cout << s1.operator>(s2) << endl;
cout << s1.operator==(s1) << endl;
cout << s1 << s2;
myString s6;
cin >> s6 ;
cout << s6;
return 0;
}