Write a C++ program to
create class named Complex to perform addition and subtraction on two complex
numbers.
Ans
#include <iostream.h>
class Complex {
public:
double r;
//real part
double i;
//imaginary part
public:
void
add(Complex, Complex);
void
subtract(Complex, Complex);
void print();
};
void Complex::add
(Complex op1, Complex op2) {
r = op1.r +
op2.r;
i = op1.i +
op2.i;
}
void
Complex::subtract (Complex op1, Complex op2) {
r = op1.r -
op2.r;
i = op1.i -
op2.i;
}
void Complex::print
() {
cout <<
"("<<r<<", " << i <<")";
}
void main () {
Complex operand1, operand2, result;
cout <<
"\nInput real part for operand one: " << endl;
cin >>
operand1.r;
cout <<
"Input imaginary part for operand one: " << endl;
cin >>
operand1.i;
cout <<
"Input real part for operand two: " << endl;
cin >>
operand2.r;
cout <<
"Input imaginary part for operand two: " << endl;
cin >>
operand2.i;
cout <<
"\nThe sum is ";
result.add(operand1, operand2);
result.print();
cout <<
"\nThe difference is ";
result.subtract(operand1, operand2);
result.print();
}
Other way is
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class complex
{
int real;
float image;
public:
void getdata()
{
cout<<"\n
enter the real part of the complex";
cin>>real;
cout<<"\n
enter the imaginary part of the complex";
cin>>image;
}
void
operator + (complex);
void
operator - (complex);
};
void complex :: operator +
(complex c1)
{
complex
temp;
temp.real=real+c1.real;
temp.image=image+c1.image;
if
(temp.image>=0);
{
cout<<"\n
complex no. after addition:";
cout<<temp.real<<"+"<<temp.image<<"i";
}
else
{
cout<<"\n
complex no. after addition ";
cout<<temp.real<<temp.image<<"i";
}
}
void complex ::operator-(complex c1)
{
complex
temp;
temp.real =
real-c1.image;
temp.image=
image-c1.image;
if
(temp.image>=0)
{
cout<<"\n
complex no. after subtraction";
cout<<"\n
temp.real<<"+"<<temp.image<<"i";
}
else
{
cout<<"\n
complex no. after subtraction";
cout<<temp.real<<temp.image<<"i"
}
}
void main()
{
clrscr();
comp.ex c1,
c2;
int n;
do
{
cout<<"\n
1. Input data for complex no. ";
cout<<"\n
2. Addition of complex no. ";
cout<<"\n
3. Subtraction of complex no. ";
cout<<"\n
4. Quit";
cout<<"\n
Enter your choice";
cin>>n;
switch(n)
{
case1:
cout<<endl<<"\n Enter the
data for First Complex No......";
cl.getdata();
cout<<endl<<"\n Enter the
data for seconds Complex No.....";
c2.getdata();
clrscr();
break;
case 2;
cl+c2;
getch();
clrscr();
break;
case 3:
cl-c2;
getch();
clrscr();
brak;
case 4:
exit91);
break;
}
}
while (n!=4);
getch();
}
Again other way is
#include<iostream.h>
#include<conio.h>
class complex
{
int r;
int i;
public:
complex()
{ }
complex(int a,int b)
{
r=a;i=b;
}
friend complex operator+(complex,complex);
friend show(complex);
complex operator+(complex c1,complex c2)
{
complex c3;
c3.r=c1.r+c2.r;
c3.i=c1.i+c2.i;
return(c3);
}
show(complex c)
{
cout<<c.r<<"i+"<<c.i<<endl;
}
void main()
{
complex a,b,c;
clrscr();
a.complex(3,6);
b.complex(4,7);
c=a+b;
show(a);
show(b);
show(c);
getch()
}
No comments:
Post a Comment