мое кунг-фу стало быстрее и сильнее =8-)
Комплексное число состоит из двух частей: вещевещественной и мнимой. Одним из способов записи мнимнимого числа является такой: (3.0, 4.0i). Здесь 3.0 - вещественная часть, а 4.0 - мнимая. Предположим, что а = (A,Bi) и с = (C,Di). Вот некоторые операоперации, выполняемые над комплексными числами: • Сложение: а + с = (А + С, (В + D)i) • Вычитание: а - с = (А - С, (В - D)i) • Умножение: а*с = (А*С- B*D,(A*D + B*C)i) • Умножение: (х - вещественное число): х * с = (x*C,x*Di) • Поиск сопряженного числа: ~а = (A, -Bi) Определите класс, оперирующий комплексными числами, чтобы приводимая ниже программа могла воспользоваться им и получать правильные результарезультаты. Обратите внимание на то, что должна выполнятьвыполняться перегрузка операций << и >>. Многие системы уже включают поддержку комплексных чисел в заголовочном файле complex.h, поэтому воспользуйтесь файлом complex0.h, чтобы избежать проблем. Используйте ключевое слово const там, где это оправдано.
#include <iostream>
#include <cstdlib>
using namespace std;
#include "complex0.h" // чтобы избежать путаницы с файлом complex.h
int main ()
{
complex a(3.0, 4.0); // инициализация посредством (3,4i)
complex c;
cout << "Enter a complex number (q to quit):\n";
while (cin >> c)
{
cout << "c is " << c << "\n" ;
cout << "complex conjugate is " << ~c << "\n";
cout << "a + c is " << a + c << "\n";
cout << "a - c is " << a - c << "\n";
cout << "a * c is " << a * c << "\n";
cout << "2 * c is " << 2 * c << "\n";
cout << "Enter a complex number (q to quit):\n";
}
cout << "Done!\n";
system("PAUSE");
return 0;
}
Результаты выполнения программы. (Обратите внивнимание на то, что оператор cin >> с благодаря перегрузке теперь требует ввода вещественной части и мнимой части.)
Enter a complex number (q to quit) :
real: 10
imaginary: 12
с is (10,12i)
complex conjugate is (10,-12i)
a + с is (13,16i)
a - с is (13,16i)
a * с is (-18,76i)
2 * с is (20,24i)
Enter a complex number (q to quit) :
real: q
Done!
------------------------------------------------------------
complex0.h
// complex0.h - определение класса complex0
#ifndef _COMPLEX0_H_
#define _COMPLEX0_H_
class complex
{
private:
double v; // вещественное число
double m; // мнимое число
public:
complex();
complex(double a , double b);
~complex();
complex operator+(const complex & b) const;
complex operator-(const complex & b) const;
complex operator~() const;
complex operator*(double n)const;
complex operator*(const complex & b)const;
friend complex operator*(double n, const complex & b);
friend ostream & operator<<(ostream & os, const complex & b);
friend istream & operator>>(istream & is, complex & b);
};
#endif
complex0.cpp
// complex0.cpp
#include <iostream>
using namespace std;
#include "complex0.h"
complex::complex()
{
v = 0.0;
m = 0.0;
}
complex::complex(double a, double b)
{
v = a;
m = b;
}
complex::~complex()
{
}
complex complex::operator+(const complex & b) const
{
return complex(v + b.v, m + b.m);
}
complex complex::operator-(const complex & b) const
{
return complex(v - b.v, m - b.m);
}
complex complex::operator~() const
{
return complex(v, -m);
}
complex complex::operator*(double n)const
{
return complex(n * v, n * m);
}
complex complex::operator*(const complex & b)const
{
return complex((v * b.v - m * b.m), (v * b.m + m * b.v));
}
complex operator*(double n, const complex & b)
{
return (b * n);
}
ostream & operator<<(ostream & os, const complex & b)
{
os << "(" << b.v << ", " << b.m << "i)";
return os;
}
istream & operator>>(istream & is, complex & b)
{
cout << "real: ";
is >> b.v;
cout << "imaginary: ";
is >> b.m;
return is;
}
main.cpp
#include <iostream>
#include <cstdlib>
using namespace std;
#include "complex0.h" // чтобы избежать путаницы с файлом complex.h
int main ()
{
complex a(3.0, 4.0); // инициализация посредством (3,4i)
complex c;
cout << "Enter a complex number (q to quit):\n";
while (cin >> c)
{
cout << "c is " << c << "\n" ;
cout << "complex conjugate is " << ~c << "\n";
cout << "a + c is " << a + c << "\n";
cout << "a - c is " << a - c << "\n";
cout << "a * c is " << a * c << "\n";
cout << "2 * c is " << 2 * c << "\n";
cout << "Enter a complex number (q to quit):\n";
}
cout << "Done!\n";
system("PAUSE");
return 0;
}
Комментариев нет:
Отправить комментарий