定义一个复数类,通过重载运算符: +,-,*,/,直接实现二个复数之间的乘除运算。
定义一个复数类,通过重载运算符:+,-,*,/,直接实现二个复数之间的乘除运算。编写一个完整的程序,测试重载运算符的正确性。要求加法“+”用成员函数实现重载,减法“-”用...
定义一个复数类,通过重载运算符: +,-,*,/,直接实现二个复数之间的乘除运算。编写一个完整的程序,测试重载运算符的正确性。要求加法“+”用成员函数实现重载,减法“-”用成员函数实现重载。乘法“*”用友元函数实现重载,除法“/”用成员函数实现重载。
⑵上机要求
增加重载复数的加法和减法运算符的功能,实现两个复数的加法,一个复数与一个实数的加法;两个复数的减法,一个复数与一个实数的减法。用成员函数实现加法运算符的重载,用友元函数实现减法运算符的重载。
自已设计主函数,完成程序的调试工作 展开
⑵上机要求
增加重载复数的加法和减法运算符的功能,实现两个复数的加法,一个复数与一个实数的加法;两个复数的减法,一个复数与一个实数的减法。用成员函数实现加法运算符的重载,用友元函数实现减法运算符的重载。
自已设计主函数,完成程序的调试工作 展开
1个回答
展开全部
class VirtualNumber{
private:
int real_number;
int vitual_number;
public:
VirtualNumber(int a,int b);
void show();
VirtualNumberoperator+(VirtualNumber a);
VirtualNumberoperator-(VirtualNumber a);
staticVirtualNumber mul(VirtualNumber a,VirtualNumber b);
VirtualNumberoperator+(int a);
VirtualNumberoperator-(int a);
int getRealNumber();
int getVitualNumber();
};
intVirtualNumber::getRealNumber(){
returnthis->real_number;
}
intVirtualNumber::getVitualNumber(){
returnthis->vitual_number;
}
VirtualNumber::VirtualNumber(int a,int b){
this -> real_number = a;
this -> vitual_number = b;
}
voidVirtualNumber::show(){
cout << "{" <<this->real_number << "+" << this->vitual_number << "i" << "}";
}
VirtualNumberVirtualNumber::operator+(VirtualNumber a){
int vitual = this->vitual_number + a.getVitualNumber();
int real = this->real_number + a.getRealNumber();
VirtualNumber result = VirtualNumber(vitual,real);
return result;
}
VirtualNumberVirtualNumber::operator-(VirtualNumber a){
int vitual = this->vitual_number - a.getVitualNumber();
int real = this->real_number - a.getRealNumber();
VirtualNumber result = VirtualNumber(vitual,real);
return result;
}
VirtualNumberVirtualNumber::operator+(int a){
int vitual = this->vitual_number;
int real = this->real_number + a;
VirtualNumber result = VirtualNumber(vitual,real);
return result;
}
VirtualNumberVirtualNumber::operator-(int a){
int vitual = this->vitual_number;
int real = this->real_number - a;
VirtualNumber result = VirtualNumber(vitual,real);
return result;
}
VirtualNumberVirtualNumber::mul(VirtualNumber a, VirtualNumber b){
int i = 0,j = 0,k = 0,l = 0;
i = a.getRealNumber()*b.getVitualNumber();
j = a.getVitualNumber()*b.getRealNumber();
k = a.getRealNumber()*b.getRealNumber();
l = a.getVitualNumber()*b.getVitualNumber()*-1;
VirtualNumber result = VirtualNumber(k+l,i+j);
return result;
}
int main(){
VirtualNumber a = VirtualNumber(3, 2);
VirtualNumber b = VirtualNumber(2 ,5);
a.show();
cout << "*";//根据需要改+- 除法没做实在是忘记了
b.show();
cout << "=";
(VirtualNumber::mul(a, b)).show();
//乘法没用重载,这里可以写(a+b).show(); 或者 (a-b).show()
return0;
}
//试试看
private:
int real_number;
int vitual_number;
public:
VirtualNumber(int a,int b);
void show();
VirtualNumberoperator+(VirtualNumber a);
VirtualNumberoperator-(VirtualNumber a);
staticVirtualNumber mul(VirtualNumber a,VirtualNumber b);
VirtualNumberoperator+(int a);
VirtualNumberoperator-(int a);
int getRealNumber();
int getVitualNumber();
};
intVirtualNumber::getRealNumber(){
returnthis->real_number;
}
intVirtualNumber::getVitualNumber(){
returnthis->vitual_number;
}
VirtualNumber::VirtualNumber(int a,int b){
this -> real_number = a;
this -> vitual_number = b;
}
voidVirtualNumber::show(){
cout << "{" <<this->real_number << "+" << this->vitual_number << "i" << "}";
}
VirtualNumberVirtualNumber::operator+(VirtualNumber a){
int vitual = this->vitual_number + a.getVitualNumber();
int real = this->real_number + a.getRealNumber();
VirtualNumber result = VirtualNumber(vitual,real);
return result;
}
VirtualNumberVirtualNumber::operator-(VirtualNumber a){
int vitual = this->vitual_number - a.getVitualNumber();
int real = this->real_number - a.getRealNumber();
VirtualNumber result = VirtualNumber(vitual,real);
return result;
}
VirtualNumberVirtualNumber::operator+(int a){
int vitual = this->vitual_number;
int real = this->real_number + a;
VirtualNumber result = VirtualNumber(vitual,real);
return result;
}
VirtualNumberVirtualNumber::operator-(int a){
int vitual = this->vitual_number;
int real = this->real_number - a;
VirtualNumber result = VirtualNumber(vitual,real);
return result;
}
VirtualNumberVirtualNumber::mul(VirtualNumber a, VirtualNumber b){
int i = 0,j = 0,k = 0,l = 0;
i = a.getRealNumber()*b.getVitualNumber();
j = a.getVitualNumber()*b.getRealNumber();
k = a.getRealNumber()*b.getRealNumber();
l = a.getVitualNumber()*b.getVitualNumber()*-1;
VirtualNumber result = VirtualNumber(k+l,i+j);
return result;
}
int main(){
VirtualNumber a = VirtualNumber(3, 2);
VirtualNumber b = VirtualNumber(2 ,5);
a.show();
cout << "*";//根据需要改+- 除法没做实在是忘记了
b.show();
cout << "=";
(VirtualNumber::mul(a, b)).show();
//乘法没用重载,这里可以写(a+b).show(); 或者 (a-b).show()
return0;
}
//试试看
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询