여기에 몇 가지 문제가 있습니다. main 함수에서 result2와 result3에 대한 계산을 작성하는 방법을 알아낼 수 없습니다. 다음 그들은 :클래스를 사용하는 함수에 값을 전달하는 중
result2 계산 (Z2 + Z3)/(Z3 + Z2)
result3 테스트를 볼 경우 (Z1)의 X (Z2 + Z3) 및 (Z1 X Z2) + (Z1 X Z3) 같다.
result2는 항상 1이어야하고 result3은 항상 같아야한다고 가정하고 있지만 어떻게 작성해야하는지 이해하지 못합니다. 어떤 아이디어? 여기에 지금까지이 작업은 다음과 같습니다. (이 첫 번째 질문입니다)
class Complex
{
private:
double real;
double imag;
public:
Complex()
{
real = 0.0;
imag = 0.0;
}
Complex(double r, double i)
{
real = r;
imag = i;
}
Complex add(const Complex& z) const
{
Complex sum;
sum.real = (real + z.real);
sum.imag = (imag + z.imag);
return sum;
}
Complex conjugate() const
{
Complex conj;
conj.real = real;
conj.imag = imag * -1;
return conj;
}
bool equals(const Complex& z) const
{
if (real == z.real && imag == z.imag)
return true;
else
return false;
}
Complex inverse() const
{
Complex inv;
inv.real = (real/(pow(real, 2) + pow(imag, 2)));
inv.imag = -1 * (imag/(pow(real, 2) + pow(imag, 2)));
return inv;
}
double modulus() const
{
double mod;
mod = sqrt(pow(real, 2) + pow(imag, 2));
return mod;
}
Complex multiply(const Complex& z) const
{
Complex prod;
prod.real = (real * z.real) - (imag * z.imag);
prod.imag = (real * z.imag) + (imag * z.real);
return prod;
}
Complex subtract(const Complex& z) const
{
Complex diff;
diff.real = real - z.real;
diff.imag = imag - z.imag;
return diff;
}
string str() const
{
stringstream sout;
sout<<fixed<<setprecision(6);
if (real == 0 && imag == 0)
sout<<"0";
else if (imag == 0)
sout<<real;
else if (real == 0 && imag == 1)
sout<<"i";
else if (real == 0 && imag == -1)
sout<<"-i";
else if (imag == 1)
sout<<real<<"+i";
else if (imag == -1)
sout<<real<<"-i";
else if (real == 0)
sout<<real<<"i";
else if (imag > 0)
sout<<real<<"+"<<imag<<"i";
else if (imag < 0)
sout<<real<<imag<<"i";
return sout.str();
}
};
int main()
{
double r1, r2, r3, c1, c2, c3;
cout<<endl;
cout<<"Enter the real and imaginary part of the first complex number-> ";
cin>>r1>>c1;
cout<<"Enter the real and imaginary part of the second complex number-> ";
cin>>r2>>c2;
cout<<"Enter the real and imaginary part of the third complex number-> ";
cin>>r3>>c3;
Complex z1(r1, c1);
Complex z2(r2, c2);
Complex z3(r3, c3);
cout<<endl;
cout<<"z1 = "<<z1.str()<<endl;
cout<<"z2 = "<<z2.str()<<endl;
cout<<"z3 = "<<z3.str()<<endl;
cout<<endl;
Complex result1 = z2.subtract(z3).multiply(z1);
Complex result2 = (z2.add(z3)); //not sure what goes next
Complex result3 =
cout<<"(z2 - z3) x z1 = "<<result1.str()<<endl;
cout<<"(z2 + z3)/(z3 + z2) = "<<result2.str()<<endl;
cout<<"z1 x (z2 + z3) and (z1 x z2) + (z1 x z3) "<<result3.str()<<endl;
return 0;
}