-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.c
74 lines (74 loc) · 1.61 KB
/
complex.c
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
#include<stdio.h>
typedef struct complex{
int r;
int i;
}complex;
complex add(complex,complex);
complex sub(complex,complex);
complex multi(complex,complex);
complex divd(complex,complex);
void main()
{
struct complex x,y,r;
int n;
printf("Enter the 1st real part: ");
scanf("%d",&x.r);
printf("Enter the 1st imaginary part: ");
scanf("%d",&x.i);
printf("Enter the 2nd real part: ");
scanf("%d",&y.r);
printf("Enter the 2nd imaginary part: ");
scanf("%d",&y.i);
printf("OPTIONS:\n1.Addition\n2.Substraction\n3.Multiplication\n4.Division");
printf("\nEnter choice: ");
scanf("%d",&n);
switch(n)
{
case 1:
r=add(x,y);
printf("The result is: %d+(%di)",r.r,r.i);
break;
case 2:
r=sub(x,y);
printf("The result is: %d+(%di)",r.r,r.i);
break;
case 3:
r=multi(x,y);
printf("The result is: %d+(%di)",r.r,r.i);
break;
case 4:
r=divd(x,y);
printf("The result is: %d+(%di)",r.r,r.i);
break;
default:
printf("wrong input");
}
}
complex add(complex x, complex y)
{
struct complex z;
z.r=x.r+y.r;
z.i=x.i+y.i;
return z;
}
complex sub(complex x, complex y)
{
struct complex z;
z.r=x.r-y.r;
z.i=x.i-y.i;
return z;
}
complex multi(complex x, complex y)
{
struct complex z;
z.r=(x.r*y.r)-(x.i*y.i);
z.i=(x.r*y.i)+(x.i*y.r);
return z;
}
complex divd(complex x, complex y)
{
struct complex z;
z.r=(x.r*y.r)+(x.i*y.i)/(y.r*y.r+y.i*y.i);
z.i=(x.i*y.r)-(x.r*y.i)/(y.r*y.r+y.i*y.i);
return z;
}