1.定义一个复数类,通过重载运算符,:+,-,*,/,直接实现两个复数之间的算术运算 用C++编程
3个回答
展开全部
我的有重载+-*/,楼上的没有,只是复制过来的,那个是例子...囧
#include<iostream>
using namespace std;
class op
{
public:
op(double,double);
op operator+(op &o);
op operator-(op &o);
op operator*(op &o);
op operator/(op &o);
void display();
private:
double real;
double image;
};
op::op(double r,double i)
{
real=r;
image=i;
};
op op::operator+(op &o)
{
return op(real+o.real,image+o.image);
}
op op::operator-(op &o)
{
return op(real-o.real,image-o.image);
}
op op::operator*(op &o)
{
return op(real*o.real,image*o.image);
}
op op::operator/(op &o)
{
return op(real/o.real,image/o.image);
}
void op::display()
{
cout<<"("<<real<<","<<image<<")\n";
}
void main()
{
op o1(1.1,2.2);
op o2(2.2,3.3);
op o3(0.0,0.0);
cout<<"o1=";
o1.display();
cout<<"o2=";
o2.display();
cout<<"o1+o2=";
o3=o1+o2;
o3.display();
cout<<"o1-o2=";
o3=o1-o2;
o3.display();
cout<<"o1*o2=";
o3=o1*o2;
o3.display();
cout<<"o1/o2=";
o3=o1/o2;
o3.display();
getchar();
}
#include<iostream>
using namespace std;
class op
{
public:
op(double,double);
op operator+(op &o);
op operator-(op &o);
op operator*(op &o);
op operator/(op &o);
void display();
private:
double real;
double image;
};
op::op(double r,double i)
{
real=r;
image=i;
};
op op::operator+(op &o)
{
return op(real+o.real,image+o.image);
}
op op::operator-(op &o)
{
return op(real-o.real,image-o.image);
}
op op::operator*(op &o)
{
return op(real*o.real,image*o.image);
}
op op::operator/(op &o)
{
return op(real/o.real,image/o.image);
}
void op::display()
{
cout<<"("<<real<<","<<image<<")\n";
}
void main()
{
op o1(1.1,2.2);
op o2(2.2,3.3);
op o3(0.0,0.0);
cout<<"o1=";
o1.display();
cout<<"o2=";
o2.display();
cout<<"o1+o2=";
o3=o1+o2;
o3.display();
cout<<"o1-o2=";
o3=o1-o2;
o3.display();
cout<<"o1*o2=";
o3=o1*o2;
o3.display();
cout<<"o1/o2=";
o3=o1/o2;
o3.display();
getchar();
}
展开全部
#include "iostream.h"
class CComplex
{
private:
double real;
double imag;
public:
CComplex(double r=0, double i=0);
void Print();
CComplex operator +(CComplex c);
CComplex operator -(CComplex c);
};
CComplex::CComplex (double r, double i)
{
real = r;
imag = i;
}
void CComplex::Print()
{
cout << "(" << real << "," << imag << ")" << endl;
}
CComplex CComplex::operator +(CComplex c)
{
CComplex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
CComplex CComplex::operator -(CComplex c)
{
CComplex temp;
temp.real = real - c.real;
temp.imag = imag - c.imag;
return temp;
}
void main(void)
{
CComplex a(1, 2), b(3.0, 4.0), c,d;
c = a+b;
d = a-b;
cout << "c = ";
c.Print();
cout << "d = ";
d.Print();
}
class CComplex
{
private:
double real;
double imag;
public:
CComplex(double r=0, double i=0);
void Print();
CComplex operator +(CComplex c);
CComplex operator -(CComplex c);
};
CComplex::CComplex (double r, double i)
{
real = r;
imag = i;
}
void CComplex::Print()
{
cout << "(" << real << "," << imag << ")" << endl;
}
CComplex CComplex::operator +(CComplex c)
{
CComplex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
CComplex CComplex::operator -(CComplex c)
{
CComplex temp;
temp.real = real - c.real;
temp.imag = imag - c.imag;
return temp;
}
void main(void)
{
CComplex a(1, 2), b(3.0, 4.0), c,d;
c = a+b;
d = a-b;
cout << "c = ";
c.Print();
cout << "d = ";
d.Print();
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询