【c++ 复数类】

2024-11-09 10:22:57
推荐回答(5个)
回答1:

#include
using namespace std;
class Complex
{
private:
double a,b;
public:
void Seta(double x=0)
{
a=x;
}
void Setb(double x=0)
{
b=x;
}
double Geta()
{
return a;
}
double Getb()
{
return b;
}
void out()
{
cout<<"("< }
};
int main()
{
Complex x;
double a,b;
x.Seta(1.369);
x.Setb();
a=x.Geta();
b=x.Getb();
cout<<"实部:"< cout<<"虚部:"< x.out();
system("pause");
return 0;
}

回答2:

class complex
{
public:
complex(float r =1., float i = 1.) : real(r), img(i) {}
float getReal() const { return real; }
void setReal(float r) { real = r; }
float getImg() const { return img; }
void setImg(float i) { img = i; }
void output() { cout << real << ' ' << img << endl; }
private:
float real, img;
};

回答3:

#include
#include

using namespace std;

class Complex : public complex
{
public:
Complex():complex(){}
Complex(double r, double i):complex(r,i){}
void setReal(double r){_Val[_RE]=r;}
void setImag(double i){_Val[_IM]=i;}
void print()const{cout<};

int main()
{
Complex z1(1.0, 2.0);
Complex z2(2.0, 1.0);

z1.print();
cout< z2.print();
cout< z1.setImag(3);
z2.setReal(4);

z1.print();
cout< z2.print();
cout<
return 0;
}

回答4:

很久没写C++代码了,访问权限这块可能有问题,你自己改改就行。
public class Num
{
public Num()
{
real = 0;
img = 0;
}
public Num(int x,int y)
{
real = x;
img = y;
}
private int real;//实部
private int img;//虚部
public void setReal(int x)
{
real = x;
}
public void setImg(int y)
{
img = y;
}
public int getReal()
{
return real;
}
public int getImg()
{
return img;
}
public void displayNum()
{
cout<<"该复数为:"< }
}

回答5:

2个数组不行吗?