-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment 3.cpp
More file actions
112 lines (109 loc) · 2.18 KB
/
Assignment 3.cpp
File metadata and controls
112 lines (109 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//============================================================================
// Name : Complex.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
using namespace std;
class Complex{
private : double real,imag;
public: Complex(double r=0,double i=0)
{
real=r;
imag=i;
}
Complex operator + (Complex const &obj)
{
Complex res;
res.real=real+obj.real;
res.imag=imag+obj.imag;
return res;
}
Complex operator - (Complex const &obj)
{
Complex res;
res.real=real-obj.real;
res.imag=imag-obj.imag;
return res;
}
Complex operator * (Complex const &obj)
{
Complex res;
res.real=real*obj.real-imag*obj.imag;
res.imag=real*obj.imag+obj.real*imag;
return res;
}
Complex operator / (Complex const &obj)
{
Complex res;
res.real=(real*obj.real+imag*obj.imag)/(obj.real*obj.real+obj.imag*obj.imag);
res.imag=(obj.real*imag-real*obj.imag)/(obj.real*obj.real+obj.imag*obj.imag);
return res;
}
friend ostream &operator << (ostream &o, Complex &c);
friend istream &operator >> (istream &o, Complex &c);
};
ostream &operator << (ostream &o,Complex &c)
{
o<<c.real;
cout<<"+ i";
o<<c.imag;
return o;
}
istream &operator >> (istream &o,Complex &c)
{
cout<<"Enter real part : ";
o>>c.real;
cout<<"Enter imaginary part : ";
o>>c.imag;
return o;
}
int main() {
double a,b,c,d;
Complex c1,c2,c3;
char o;
string e;
cout<<"Enter the first number : "<<endl;
cin>>c1;
cout<<c1<<endl;
cout<<"Enter the second number : "<<endl;
cin>>c2;
cout<<c2<<endl;
do
{
cout<<"Enter operator : ";
cin>>o;
switch(o)
{
case '+' : c3=c1+c2;
cout<<"Addition : ";
cout<<c3;
cout<<endl;
break;
case '-' : c3=c1-c2;
cout<<"Subtraction : ";
cout<<c3;
cout<<endl;
break;
case '*' : c3=c1*c2;
cout<<"Multiplication : ";
cout<<c3;
cout<<endl;
break;
case '/' : c3=c1/c2;
cout<<"Division : ";
cout<<c3;
cout<<endl;
break;
default : cout<<"Please enter a valid option"<<endl;
}
cout<<"Would you like to perform another operation? (Y/N)"<<endl;
cin>>e;
}
while(e=="Y");
if(e=="N")
cout<<"Thank you for using the calculator!";
return 0;
}