Posted by MindBreaker at 9:38 AM
Read our previous post
Object:
Write a class description for complex numbers. A complex number in mathematics is defined
as
x+y, where x defines the real part and y defines the imaginary part. Provide
the getData
and
display Data functionalities to input and display a complex number. Also write
methods
for
addition and subtraction of two numbers. Write a program that creates 3 complex
members and initialize them to zero. Provide options to either input, display,
add,
subtract,
or multiply the 2 members.
Add: x3= x1 + x2 , y3= y1 + y2
Source code:
#include <iostream>
using namespace std;
class
calculate
{
public:
void
add(int c,int
d,int a,int b)
{
int
number1=a,imaginary1=b,number2=c,imaginary2=d,number,imaginary;
number=number1+number2;
imaginary=imaginary1+imaginary2;
cout<<"Answer is : "<<number<<"+"<<imaginary<<"i"<<endl;
};
void
sub(int c,int
d,int a,int b)
{
int
number1=a,imaginary1=b,number2=c,imaginary2=d,number,imaginary;
number=number1-number2;
imaginary=imaginary2-imaginary1;
cout<<"Answer is : "<<number<<"-"<<imaginary<<"i"<<endl;
};
};
void
main()
{
calculate calcu;
int
input,input2,input3,input4;
char
press;
cout<<"Press 1 for addition and 2 for subtraction complex
numbers"<<endl;
cin>>press;
cout<<endl;
switch
(press)
{ case '1':
{cout<<"Use i with imaginary numbers"<<endl;
cout<<"Enter first real number"<<endl;
cin>>input;
cout<<"Enter first imaginary number"<<endl;
cin>>input2;
cout<<"Enter second real number"<<endl;
cin>>input3;
cout<<"Enter second imaginary number"<<endl;
cin>>input4;
calcu.add(input,input2,input3,input4);
}
case
'2':
{ cout<<"Enter
first real number"<<endl;
cin>>input;
cout<<"Enter first imaginary number"<<endl;
cin>>input2;
cout<<"Enter second real number"<<endl;
cin>>input3;
cout<<"Enter second imaginary number"<<endl;
cin>>input4;
calcu.sub(input,input2,input3,input4);}
}
}
Output
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.