Binod's Blog

Tag: Complex number

Prefix And Postfix addition in Complex Number

by on Apr.03, 2011, under C & C++

#include<iostream.h>
#include<conio.h>
class complexNumber{
int real,imaginary;
public:
complexNumber(){}
complexNumber(int x, int y){real=x;imaginary=y;}
void getdata(){
cout<<”Enter a real part: “;
cin>>real;//takes real numbr from user
cout<<”Enter a Imaginary part: “;
cin>>imaginary;//takes imaginary numbr from user
}
void showdata(){
if(imaginary<0) //imaginary negative
cout<<real<<”"<<imaginary<<”i”<<endl;
else if(imaginary==0) //no imaginary
cout<<real<<endl;
else if(real==0)    //no real part
cout<<imaginary<<”i”<<endl;
else
cout<<real<<”+”<<imaginary<<”i”<<endl;
}
complexNumber operator ++(){   //prefix
++real;
++imaginary;
return *this;
}
complexNumber operator ++(int x){   //postfix
complexNumber temp;
temp.real=real++;
temp.imaginary=imaginary++;
return temp;
}

};

main(){
clrscr();
complexNumber a,b,c,d,e,f;
a.getdata();
cout<<”The Imaginary Number A is:”;a.showdata();
b.getdata();
cout<<”The Imaginary Number B is:”;b.showdata();
c=++b;
cout<<”Object C <- B by Prefix Operator:”;c.showdata();
cout<<”Increment of object B:”;b.showdata();
d=a++;
cout<<”Assigned value of Postfix in D <- A:”;d.showdata();
cout<<”Value at object A:”;a.showdata();

getch();
return 0;
}

Leave a Comment :, more...

Complex Number Addition (a+ib)+(x-iy)

by on Apr.03, 2011, under C & C++

#include<iostream.h>
#include<conio.h>
class complexNumber{
int real,imaginary;
public:
void getdata(){
cout<<”Enter a real part: “;
cin>>real;//takes real numbr from user
cout<<”Enter a Imaginary part: “;
cin>>imaginary;//takes imaginary numbr from user
}
void showdata(){
if(imaginary<0) //imaginary negative
cout<<real<<”"<<imaginary<<”i”<<endl;
else if(imaginary==0) //no imaginary
cout<<real<<endl;
else if(real==0)    //no real part
cout<<imaginary<<”i”<<endl;
else
cout<<real<<”+”<<imaginary<<”i”<<endl;
}
complexNumber add(complexNumber b){ //adding by passing object
real+=b.real;
imaginary+=b.imaginary;
return *this;
}
};

main(){
clrscr();
complexNumber a,b,c;
a.getdata();
cout<<”The Imaginary Number is:”;
a.showdata();
b.getdata();
cout<<”The Imaginary Number is :”;b.showdata();
c=a.add(b);
cout<<”The sum of two imaginary number is:”;c.showdata();
getch();
return 0;
}

Leave a Comment :, more...

Complex Number (a+ib) in C++

by on Apr.03, 2011, under C & C++

To create a class name complexNumber with two data member real and imaginary, include two member functions to take I/P and give O/P.

Source Code:
#include<iostream.h>
#include<conio.h>
class complexNumber{
int real,imaginary;
public:
void getdata(){
cout<<”Enter a real part: “;
cin>>real;//takes real numbr from user
cout<<”Enter a Imaginary part: “;
cin>>imaginary;//takes imaginary numbr from user
}
void showdata(){
if(imaginary<0) //imaginary negative
cout<<real<<”"<<imaginary<<”i”<<endl;
else if(imaginary==0) //no imaginary
cout<<real<<endl;
else if(real==0)    //no real part
cout<<imaginary<<”i”<<endl;
else               //imaginary positive
cout<<real<<”+”<<imaginary<<”i”<<endl;
}
};

main(){
clrscr();
complexNumber a,b,c,d;
a.getdata();
cout<<”The Imaginary Number is:”;a.showdata();
b.getdata();
cout<<”The Imaginary Number is :”;b.showdata();
c.getdata();
cout<<”The Imaginary Number is :”;c.showdata();
d.getdata();
cout<<”The Imaginary Number is :”;d.showdata();

getch();
return 0;
}

Leave a Comment :, more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...