C++ Programs by Yamrajbhalla

yamrajbhalla

yamrajbhalla

@yamrajbhalla-ZPvBoL Oct 22, 2024
This Will Do the +,-,*and / of the complex numbers



/*Operator overloading for complex Numbers*/
#include<iostream.h>
#include<conio.h>

class complex
{
 float real;
 float img;

 public:
  complex()
  {
   real=img=0;
  }


 complex(float real,float img)
 {
  this->real=real;
  this->img=img;
 }

 void setcomplex(float r,float i)
 {
  real=r;
  img=i;
 }

 void print()
 {
  cout<<real<<" + "<<img<<"i";
 }

 complex operator +(complex c)
 {
   complex t;
   t.real=real+c.real;
   t.img=img+c.img;
   return t;
 }

 complex operator -(complex c)
 {
   complex t;
   t.real=real-c.real;
   t.img=img-c.img;
   return t;
 }

  complex operator *(complex c)
 {complex t;
   t.real=real*c.real;
   t.img=img*c.img;
   return t;
   }
  complex operator /(complex c)
  {complex t;
   t.real=real/c.real;
   t.img=img/c.img;
   return t;
  }

};

 void main()
 {complex c1,c2,c3,c4,c5,c6;
  clrscr();
   //add items;
   c1.setcomplex(3,3);
   c2.setcomplex(5,5);
   c3=c1+c2;
   cout<<endl<<"(";c1.print();cout<<")  +  (";c2.print();
   cout<<") = (";
   c3.print();cout<<")";
   c4=c1-c2;
   cout<<endl<<"(";c1.print();cout<<")  -  (";c2.print();
   cout<<")  =  (";
   c4.print();cout<<")";
   c5=c1*c2;
   cout<<endl<<"(";c1.print();cout<<")  *  (";c2.print();
   cout<<") = (";
   c5.print();cout<<")";
   c6=c1/c2;
   cout<<endl<<"(";c1.print();cout<<")  /  (";c2.print();
   cout<<") = (";
   c6.print();cout<<")";

   
   getch();
 }

out put



(3+3i) + (5+5i) =(8+8i)
(3+3i) - (5+5i) =(-2+-2i)
(3+3i) * (5+5i) =(15+15i)
(3+3i) / (5+5i) =(0.6+0.6i)

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Mar 31, 2009

    The program looks good except the logic of for * (multiplication) and / (division). The code for that was mentioned as

      complex operator *(complex c)
     {complex t;
       t.real=real*c.real;
       t.img=img*c.img;
       return t;
       }
      complex operator /(complex c)
      {complex t;
       t.real=real/c.real;
       t.img=img/c.img;
       return t;
      }
    
    with output
    (3+3i) * (5+5i) =(15+15i)
    (3+3i) / (5+5i) =(0.6+0.6i)

    The output should be
    (3+3i) * (5+5i) = 3*5 + 3i*5 + 3*5i + 3i*5i = 15 + 15i + 15i -15 = (0+30i)

    (3+3i) / (5+5i) = [(3+3i)/(5+5i)] * [(5-5i)/(5-5i)]
    = (15 + 15i -15i + 15) / (25 + 25i -25i + 25) = 30/50 = 0.6 = (0.6+0i)

    Coding for multiplication should be easy. Lets try coding for division.

    -Pradeep
  • shalini_goel14

    shalini_goel14

    @shalini-goel14-ASmC2J Mar 31, 2009

    pradeep_agrawal
    (3+3i) / (5+5i) = [(3+3i)/(5+5i)] * [(5+5i)/(5+5i)]
    = (15 + 15i -15i + 15) / (25 + 25i -25i + 25) = 30/50 = 0.6 = (0.6+0i)

    Coding for multiplication should be easy. Lets try coding for division.

    -Pradeep
    Hey pradeep, Don't you think for division it should be like following :
    (3+3i) / (5+5i) = [(3+3i)/(5+5i)] * [(5-5i)/(5-5i)]

    Please correct me if anything wrong. 😔

    Thanks !
  • pradeep_agrawal

    pradeep_agrawal

    @pradeep-agrawal-rhdX5z Mar 31, 2009

    Yes that should be [(3+3i)/(5+5i)] * [(5-5i)/(5-5i)].

    I did all calculation using [(3+3i)/(5+5i)] * [(5-5i)/(5-5i)] but mentioned [(3+3i)/(5+5i)] * [(5+5i)/(5+5i)] by mistake.

    Thanks for correcting me. I am modifying the above statement to reflect this.

    -Pradeep
  • yamrajbhalla

    yamrajbhalla

    @yamrajbhalla-ZPvBoL Apr 1, 2009

    bro after posting this i have chnged the code for multiplication and divison bt i stuck with fever and i am nt able to update it
    by the way thanks for view my post