Implement a class Complex which represents the Complex Number data type. Implement the following operations:
1. Constructor (including a default constructor which creates the complex number 0+0i).
2. Overloaded operator+ to add two complex numbers.
3. Overloaded operator* to multiply two complex numbers.
4. Overloaded << and >> to print and read Complex Numbers.
We are using g++ compiler and terminal to compile the code and ubuntu OS.
PROGRAM:
#include<iostream>
using namespace std;
int main()
{
PROGRAM:
int a,b,c;
char op,d;
do
{
cout<<"Enter the first number:";
cin>>a;
cout<<"Enter the second number:";
cin>>b;
cout<<"Enter operator +,-,*,/:";
cin>>op;
switch(op)
{
case'+':
c=a+b;
cout<<"Addition:"<<a<<"+"<<b<<"="<<c;
break;
case'-':
c=a-b;
cout<<"Substraction:"<<a<<"-"<<b<<"="<<c;
break;
case'*':
c=a*b;
cout<<"Multiplication:"<<a<<"*"<<b<<"="<<c;
break;
case'/':
c=a/b;
cout<<"Divition:"<<a<<"/"<<b<<"="<<c;
break;
default:
cout<<"Enter Valid operator";
}
cout<<"\nDo you wan to continue(Y/N):";
cin>>d;
}while(d=='Y'||d=='Y');
return 0;
}
Output:
prakash@prakash-Vostro-3478:~$ g++ calculator.cpp
prakash@prakash-Vostro-3478:~$ ./a.out
Enter the first number:12
Enter the second number:13
Enter operator +,-,*,/:+
Addition:12+13=25
Do you wan to continue(Y/N):Y
Enter the first number:14
Enter the second number:15
Enter operator +,-,*,/:-
Substraction:14-15=-1
Do you wan to continue(Y/N):N
prakash@prakash-Vostro-3478:~$
Comments
Post a Comment