forked from Dev-XYS/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Complex-Number.cpp
58 lines (51 loc) · 1.11 KB
/
Complex-Number.cpp
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
#include <cstdio>
using namespace std;
struct complex
{
double re, im;
complex(double _re = 0, double _im = 0) : re(_re), im(_im) { }
};
complex operator + (const complex &x, const complex &y)
{
return complex(x.re + y.re, x.im + y.im);
}
complex operator - (const complex &x, const complex &y)
{
return complex(x.re - y.re, x.im - y.im);
}
complex operator * (const complex &x, const complex &y)
{
return complex(x.re * y.re - x.im * y.im, x.re * y.im + x.im * y.re);
}
complex operator / (const complex &x, const complex &y)
{
return complex((x.re * y.re + x.im * y.im) / (y.re * y.re + y.im * y.im), (x.im * y.re - x.re * y.im) / (y.re * y.re + y.im * y.im));
}
int main()
{
complex x, y, res;
char op;
while (true)
{
scanf("%lf%lf %c %lf%lf", &x.re, &x.im, &op, &y.re, &y.im);
switch (op)
{
case '+':
res = x + y;
break;
case '-':
res = x - y;
break;
case '*':
res = x * y;
break;
case '/':
res = x / y;
break;
default:
continue;
}
printf("(%.2lf + (%.2lf)i) %c (%.2lf + (%.2lf)i) = %.2lf + (%.2lf)i\n", x.re, x.im, op, y.re, y.im, res.re, res.im);
}
return 0;
}