1.使用运算符重载,完成复数运算
复数四则运算法则如下:
具体代码如下
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
using namespace std;
class Complex{
private:
double a;
double b;
public:
Complex(double a,double b)
:a(a),b(b)
{
this->a=a;
this->b=b;
}
double geta()const{return a;}
double getb()const{return b;}
void show()
{
cout<<a<< ((b>=0)?("+"):("-")) <<abs(b)<<"i"<<endl;
}
};
Complex operator+(const Complex& c1,const Complex& c2)
{
Complex c3(c1.geta()+c2.geta(),c1.getb()+c2.getb());
return c3;
}
Complex operator-(const Complex& c1,const Complex& c2)
{
Complex c3(c1.geta()-c2.geta(),c1.getb()-c2.getb());
return c3;
}
Complex operator*(const Complex& c1,const Complex& c2)
{
Complex c5(c1.geta()*c2.geta()-c1.getb()*c2.getb(),
c1.getb()*c2.geta()+c1.geta()*c2.getb());
return c5;
}
Complex operator/(const Complex& c1,const Complex& c2)
{
double ob=c2.geta()*c2.geta()+c2.getb()*c2.getb();
if(ob==0)
{
cout<<"不存在"<<endl;
return Complex(0,0);
}
else
{
Complex c6(
(c1.geta()*c2.geta()+c1.getb()*c2.getb())/ob,
(c1.getb()*c2.geta()-c1.geta()*c2.getb())/ob);
return c6;
}
}
int main(int argc,const char** argv){
Complex c1(1,2);
Complex c2(3,-4);
cout<<" c1= ";c1.show();
cout<<" c2= ";c2.show();
Complex c3=c1+c2;
cout<<"c1+c2= ";c3.show();
Complex c4=c1-c2;
cout<<"c1-c2= ";c4.show();
Complex c5=c1*c2;
cout<<"c1*c2= ";c5.show();
Complex c6=c1/c2;
cout<<"c1/c2= ";c6.show();
}
运行截图;
2. +,=,+=,[]的使用
其中,=,+=,[] 需要在类中声明,缘由是他们是等待赋值,
2.1以 = 赋值为例:
mystring& operator=(const mystring& other);
我们知道,在这里隐藏一个this指针,指向它原本的对象,这是我们就有,this指向的对象,和other两个对象
mystring& mystring::operator=(const mystring& other)
{
if(this==&other)
{
return *this;
}
delete[] str;
str=(char*)malloc(other.getlen()+1);
strcpy(str,other.getstr());
this->len=other.getlen();
return *this;
}
我们把other对象所对应的值赋给this,所指向的对象就可以完成赋值
2.2再以 [] 下标为例:
char& operator[](int i); 实现声明
char& mystring::operator[](int i){
return this->str[i];//通过访问需要修改的下表的引用
}
str[0]='H';//这地方的本质为, this->str[0]=''H;精准通过下表进行修改。
具体代码如下:
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
#include <sstream>
#include <vector>
#include <memory>
using namespace std;
class mystring{
private:
char* str;
int len;
public:
mystring(const char* s);
~mystring();
void show();
char* getstr()const{return str;}
int getlen()const{return len;}
mystring& operator=(const mystring& other);
mystring& operator+=(const mystring& other);
char& operator[](int i);
};
mystring::mystring(const char* s)
{
len=strlen(s);
str=(char*)malloc(len+1);
strcpy(str,s);
}
mystring::~mystring()
{
free(str);
str=NULL;
}
void mystring::show()
{
cout<<str<<endl;
}
mystring operator+(const mystring& s1,const mystring& s2)
{
int len=strlen(s1.getstr())+strlen(s2.getstr());
char* arr=(char*)malloc(len+1);
strcpy(arr,s1.getstr());
strcat(arr,s2.getstr());
return mystring(arr);
}
mystring& mystring::operator=(const mystring& other)
{
if(this==&other)
{
return *this;
}
delete[] str;
str=(char*)malloc(other.getlen()+1);
strcpy(str,other.getstr());
this->len=other.getlen();
return *this;
}
mystring& mystring::operator+=(const mystring& other)
{
char* temp=NULL;
temp=(char*)malloc(this->len);
strcpy(temp,str);
delete[] str;
this->len=strlen(str)+other.getlen();
str=(char*)malloc(this->len+1);
strcpy(str,temp);
strcat(str,other.getstr());
free(temp);
temp=NULL;
return *this;
}
char& mystring::operator[](int i){
return this->str[i];
}
int main(int argc,const char** argv){
mystring str="hello";
mystring ptr="world";
str.show();
ptr.show();
str=str+ptr;
str.show();
str+=ptr;
str.show();
str[0]='H';
str.show();
return 0;
}