Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Plural {
- private:
- double real = 0;
- double imaginary = 0;
- public:
- plural(double a, double b) {
- real = a;
- imaginary = b;
- }
- double get_real() {
- return real;
- }
- double get_imaginary() {
- return imaginary;
- }
- double modulo() {
- return real * real + imaginary * imaginary;
- }
- friend plural operator+(plural first, plural second);
- friend plural operator-(plural first, plural second);
- friend plural operator*(plural first, plural second);
- friend plural operator/(plural first, plural second);
- };
- plural operator+(plural first, plural second) {
- double r = first.real + second.real;
- double i = first.imaginary + second.imaginary;
- return plural(r, i);
- }
- plural operator-(plural first, plural second) {
- double r = first.real - second.real;
- double i = first.imaginary - second.imaginary;
- return plural(r, i);
- }
- plural operator*(plural first, plural second) {
- //r=ac-bd
- double r = first.real * second.real - first.imaginary * second.imaginary;
- //i=bc+ad
- double i = first.real * second.imaginary + first.imaginary * second.real;
- return plural(r, i);
- }
- plural operator/(plural first, plural second) {
- //r=(ac+bd)/(c*c+d*d)
- double r = (first.real * second.real + first.imaginary * second.imaginary) /
- (second.real * second.real + second.imaginary * second.imaginary);
- //i=(bc-ad)/(c*c+d*d)
- double i = (first.imaginary * second.real - first.real * second.imaginary) /
- (second.real * second.real + second.imaginary * second.imaginary);
- return plural(r, i);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement