Класс "комплексные числа"
Добавлено: 29 окт 2009, 11:24
Комплексное число - это (a+b*i), где i - корень из -1.
Класс я написал, но без учета этого i.
Вот его реализация:
Но в задании есть пометка:
Заранее спасибо.
Класс я написал, но без учета этого i.
Вот его реализация:
Код: Выделить всё
class Complex
{
public:
Complex(double the_real, double the_imaginary);
Complex(double real_part);
Complex();
friend bool operator ==(const Complex& first, const Complex& second);
friend Complex operator +(const Complex& first, const Complex& second);
friend Complex operator -(const Complex& first, const Complex& second);
friend Complex operator *(const Complex& first, const Complex& second);
friend istream& operator >>(istream& ins, Complex& number);
friend ostream& operator <<(ostream& outs, const Complex& number);
private:
double real;
double imagine;
};
Complex::Complex(double the_real, double the_imaginary): real(the_real), imagine(the_imaginary)
{
//Тело намеренно оставлено пустым
}
Complex::Complex(double real_part): real(real_part), imagine(0)
{
//Тело намеренно оставлено пустым
}
Complex::Complex(): real(0), imagine(0)
{
//Тело намерено отсавлено пустым
}
bool operator ==(const Complex& first, const Complex& second)
{
return((first.real == second.real) && (first.imagine == second.imagine));
}
Complex operator +(const Complex& first, const Complex& second)
{
Complex temp;
temp.real = first.real + second.real;
temp.imagine = first.imagine + second.imagine;
return temp;
}
Complex operator -(const Complex& first, const Complex& second)
{
Complex temp;
temp.real = first.real - second.real;
temp.imagine = first.imagine - second.imagine;
return temp;
}
Complex operator *(const Complex& first, const Complex& second)
{
Complex temp;
temp.real = (first.real * second.real) - (first.imagine * second.imagine);
temp.imagine = (first.real * second.imagine) + (first.imagine * second.real);
return temp;
}
istream& operator >>(istream& ins, Complex& number)
{
//Формат ввода значений: (1, 2)
char ch1, ch2, ch3;
ins >> ch1;
ins >> number.real;
ins >> ch2;
ins >> number.imagine;
ins >> ch3;
if(ch1 != '(' || ch2 != ',' || ch3 != ')')
{
cout << "Illegal complex number format.\n";
exit(1);
}
return ins;
}
ostream& operator <<(ostream& outs, const Complex& number)
{
outs << '(';
outs << number.real;
outs << ',';
outs << number.imagine;
outs << ')';
return outs;
}
Константу допустим объявил. Тогда, если комплексное число следующее: (1, 2), то 2 еще должно обрабатываться, как 2*i, т.е. 2(0, 1). Или я что-то не так понимаю? Как в указанный класс примостить эту константу и ее использовать в соответствии с представлением (a+b*i)?i — число, представляющее значение корень из -1.
Переменная-член imaginary соответствует числу, умножаемому на i.
Константу i определите так:
const Complex i(0, 1);
Это и будет описанное выше значение i.
Заранее спасибо.