Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to format complex number output with negative or zero values
    primarykey
    data
    text
    <p>Please I need to write a program that takes two complex numbers from the user and output their difference. Complex numbers are those numbers in the form of a+bi, where a and b are real numbers known as the real and imaginary parts respectively. Thus, the difference between two complex numbers a1+b1i and a2+b2i is (a1-a2)+(b1-b2)i. I wrote the codes as</p> <pre><code>#include&lt;iostream&gt; using namespace std; class cmp { private: double re, im; public: cmp(double a, double b) {re=a; im=b;} friend cmp operator+(cmp&amp;, cmp&amp;); void show() {cout&lt;&lt;re&lt;&lt;" + "&lt;&lt;im&lt;&lt;"i"&lt;&lt;endl;} }; cmp operator+(cmp&amp; p, cmp&amp; q) {return cmp (p.re-q.re, p.im-q.im);} main() { double m1, n1, m2, n2; cout&lt;&lt;"Give two complex numbers: "; cin&gt;&gt;m1&gt;&gt;n1; cout&lt;&lt;endl; cout&lt;&lt;"Give another two complex numbers: "; cin&gt;&gt;m2&gt;&gt;n2; cout&lt;&lt;endl; cmp c1(m1,n1); cmp c2(m2,n2); cmp c3=c1+c2; c3.show(); system("PAUSE"); } </code></pre> <p>However, when you look at the difference (a1-a2)+(b1-b2)i, if</p> <ol> <li><p>b2>b1, then the imaginary part in the difference will be negative. For example (10.5+2i)-(5+4i)=5.5-2i. But, the program can't display that and display 5+-2i instead. How can my program be in usual maths format?</p></li> <li><p>a1=a2 and/or b1=b2, 0 is/are displayed in the program unlike the usual format in maths. For example, mathematically, (4+7i)-(4+5i)=(4-4)+(7-5)i=2i. But this program will display 0+2i instead. How can I solve this problem too?</p></li> </ol> <p>Please I need a clue on how to overcome these. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload