Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Make it 3 file - main.cpp, complex.cpp and complex.h - run in main.cpp - add and

ID: 3861989 • Letter: M

Question

Make it 3 file - main.cpp, complex.cpp and complex.h

- run in main.cpp

- add and subract shoud be in complex file

-I like to see the ouput like his -----2+3i or 5-i or -7+9i.

--------------------------------------------------------------------

-overload math operators for your complex number.

-override istream and ostream operators for your complex number.

-override == for your complex number.

-override combined assignment operator for your complex number.

-Using namespace and guard and putting comments is necessary.

Explanation / Answer

//complex.h

class complex1
{
int i,r;
public:
void read();
void display();
complex1 operator+(complex1 a2);
complex1 operator-(complex1 a2);
complex1 operator*(complex1 a2);
complex1 operator/(complex1 a2);
int operator==(complex1 a2);
};

//complex.cpp

#include "complex.h"
#include <stdio.h>
#include <iostream>
using namespace std;

void complex1::read()
{
cout<<" Enter Real Part:";
cin>>r;
cout<<"Enter Imaginary Part:";
cin>>i;
}
void complex1::display()
{
cout<<" = "<<r<<"+"<<i<<"i";
}
complex1 complex1::operator+(complex1 a2)
{
complex1 a;
a.r=r+a2.r;
a.i=i+a2.i;
return a;
}
complex1 complex1::operator-(complex1 a2)
{
complex1 a;
a.r=r-a2.r;
a.i=i-a2.i;
return a;
}
complex1 complex1::operator*(complex1 a2)
{
complex1 a;
a.r=(r*a2.r)-(i*a2.i);
a.i=(r*a2.i)+(i*a2.r);
return a;
}
complex1 complex1::operator/(complex1 a2)
{
complex1 a;
a.r=((r*a2.r)+(i*a2.i))/((a2.r*a2.r)+(a2.i*a2.i));
a.i=((i*a2.r)-(r*a2.i))/((a2.r*a2.r)+(a2.i*a2.i));
return a;
}
int complex1::operator==(complex1 a2)
{
if(r==a2.r && i==a2.i)
return 1;
return 0;
}

//main.cpp

#include <iostream>
#include <string.h>
#include <stdio.h>
#include "complex.h"
using namespace std;

int main()
{
int ch;

complex1 a,b,c;
do
{
cout<<" 1.Addition 2.Substraction";
cout<<" 3.Mulitplication 4.Division 5.Comparison 6.Exit ";
cout<<" Enter the choice :";
cin>>ch;
switch(ch)
{
case 1:
cout<<" Enter The First Complex Number:";
a.read();
a.display();
cout<<" Enter The Second Complex Number:";
b.read();
b.display();
c=a+b;
cout<<"Addition of given numbers is:";
c.display();
break;
case 2:
cout<<" Enter The First Complex Number:";
a.read();
a.display();
cout<<" Enter The Second Complex Number:";
b.read();
b.display();
c=b-a;
cout<<"Substraction of two number is :";
c.display();
break;
case 3:
cout<<" Enter The First Complex Number:";
a.read();
a.display();
cout<<" Enter The Second Complex Number:";
b.read();
b.display();
c=a*b;
cout<<"Multiplication of two number is :";
c.display();
break;
case 4:
cout<<" Enter The First Complex Number:";
a.read();
a.display();
cout<<" Enter The Second Complex Number:";
b.read();
b.display();
cout<<"Division of two number is :";
c=a/b;
c.display();
break;
case 5:
cout<<" Enter The First Complex Number:";
a.read();
a.display();
cout<<" Enter The Second Complex Number:";
b.read();
b.display();
int res=a==b;
if(res==0)
cout<<" Given numbers are not equal ";
else
cout<<" Given numbers are equal ";
break;

}
}while(ch!=6);
return 0;
}

//Output ::

:~/codes/schegg$ g++ complex.h complex.cpp complex_main.cpp

:~/codes/schegg$ ./a.out

1.Addition 2.Substraction 3.Mulitplication 4.Division 5.Comparison 6.Exit

Enter the choice :1

Enter The First Complex Number:
Enter Real Part:32
Enter Imaginary Part:15

= 32+15i
Enter The Second Complex Number:
Enter Real Part:43
Enter Imaginary Part:23

= 43+23iAddition of given numbers is:
= 75+38i
1.Addition 2.Substraction 3.Mulitplication 4.Division 5.Comparison 6.Exit

Enter the choice :3

Enter The First Complex Number:
Enter Real Part:321
Enter Imaginary Part:53

= 321+53i
Enter The Second Complex Number:
Enter Real Part:23
Enter Imaginary Part:56

= 23+56iMultiplication of two number is :
= 4415+19195i
1.Addition 2.Substraction 3.Mulitplication 4.Division 5.Comparison 6.Exit

Enter the choice :5

Enter The First Complex Number:
Enter Real Part:321
Enter Imaginary Part:43

= 321+43i
Enter The Second Complex Number:
Enter Real Part:543
Enter Imaginary Part:23

= 543+23i
Given numbers are not equal

1.Addition 2.Substraction 3.Mulitplication 4.Division 5.Comparison 6.Exit

Enter the choice :6
:~/codes/schegg$ g++ complex

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote