Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help in C++ I need to modify the following code to work with complex numbers op: Complex Complex Complex op: Complex double Complex

I need help in C++ I need to modify the following code to work with complex numbers 

op: Complex Complex Complex

op: Complex double Complex

op: double Complex Complex

the output should be like :

(1 + 2i) + (1 + 3i) = (2 + 5i) (1 + 2i) - (1 + 3i) = (0 + -1i) (1 + 2i) * (1 + 3i) = (-5 + 5i) (1 + 2i) / (1 + 3i) = (0.7 + -0.1i) (1 + 2i) + 5 = (6 + 2i) (1 + 2i) - 5 = (-4 + 2i) (1 + 2i) * 5 = (5 + 10i) (1 + 2i) / 5 = (0.2 + 0.4i) 5 + (1 + 2i) = (6 + 2i) 5 - (1 + 2i) = (4 + -2i) 5 * (1 + 2i) = (5 + 10i) 5 / (1 + 2i) = (1.0 + -2.0i)

*** need imaginary number i ***

/* rational.h */ #ifndef RATIONAL_H #define RATIONAL_H #include  using std::ostream; struct rational { rational(int = 0, int = 1); rational operator+(const rational &) const; rational operator-(const rational &) const; rational operator*(const rational &) const; rational operator/(const rational &) const; rational operator+(int) const; rational operator-(int) const; rational operator*(int) const; rational operator/(int) const; friend rational operator+(int, const rational &); friend rational operator-(int, const rational &); friend rational operator*(int, const rational &); friend rational operator/(int, const rational &); friend ostream &operator<<(ostream &, const rational &); private: int den; int num; }; #endif /* RATIONAL_H */ /* rational.cc */ #include  #include "rational.h" rational::rational(int num, int den) : num(num), den(den) {} rational rational::operator+(const rational &o) const { return rational(num * o.den + o.num * den, den * o.den); } rational rational::operator+(int n) const { return rational(num + n * den, den); } rational rational::operator-(const rational &o) const { return rational(num * o.den - o.num * den, den * o.den); } rational rational::operator-(int n) const { return rational(num - n * den, den); } rational rational::operator*(const rational &o) const { return rational(num * o.num, den * o.den); } rational rational::operator*(int n) const { return rational(num * n, den); } rational rational::operator/(const rational &o) const { return rational(num * o.den, den * o.num); } rational rational::operator/(int n) const { return rational(num, den * n); } rational operator+(int n, const rational &o) { return o + n; } rational operator-(int n, const rational &o) { return rational(n) - o; } rational operator*(int n, const rational &o) { return o * n; } rational operator/(int n, const rational &o) { return rational(n) / o; } ostream &operator<<(ostream &out, const rational &o) { out << '(' << o.num << " / " << o.den << ')'; return out; } /* main.cc */ #include  #include "rational.h" using std::cout; using std::endl; int main(void) { rational a(1, 2); rational b(1, 3); int i = 5; cout << a << " + " << b << " = " << a + b << endl; cout << a << " - " << b << " = " << a - b << endl; cout << a << " * " << b << " = " << a * b << endl; cout << a << " / " << b << " = " << a / b << endl; cout << a << " + " << i << " = " << a + i << endl; cout << a << " - " << i << " = " << a - i << endl; cout << a << " * " << i << " = " << a * i << endl; cout << a << " / " << i << " = " << a / i << endl; cout << i << " + " << a << " = " << i + a << endl; cout << i << " - " << a << " = " << i - a << endl; cout << i << " * " << a << " = " << i * a << endl; cout << i << " / " << a << " = " << i / a << endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven Databases In Seven Weeks A Guide To Modern Databases And The NoSQL Movement

Authors: Luc Perkins, Eric Redmond, Jim Wilson

2nd Edition

1680502530, 978-1680502534

More Books

Students also viewed these Databases questions

Question

4.6 Summarize job design concepts.

Answered: 1 week ago

Question

4.5 Explain what competencies and competency modeling are.

Answered: 1 week ago