forked from CPRO-Session1/Assignment6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplexcal.c
More file actions
98 lines (97 loc) · 1.76 KB
/
complexcal.c
File metadata and controls
98 lines (97 loc) · 1.76 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
/*Lloyd Page*/
/*Calculator for complex numbers*/
#include<stdio.h>
typedef struct
{
int real;
int fake;
}c;
c add(c,c);
c sub(c,c);
c div(c,c);
c mul(c,c);
int main()
{
char operation;
char handler[100];
while(1)
{
printf("Select your operation: +,-,/,*\n");
fgets(handler,sizeof(handler),stdin);
if(sscanf(handler,"%c",&operation)&&(operation=='+'||operation=='-'||operation=='/'||operation=='*'))
break;
printf("invalid option\n");
}
c a;
c b;
c c;
while(1)
{
printf("Enter the real part of your first number\n");
fgets(handler,sizeof(handler),stdin);
if(sscanf(handler,"%d",&a.real))
break;
printf("invalid input\n");
}
while(1)
{
printf("Enter the imaginary part of your first number\n");
fgets(handler,sizeof(handler),stdin);
if(sscanf(handler,"%d",&a.fake))
break;
printf("invalid input\n");
}
while(1)
{
printf("Enter the real part of your second number\n");
fgets(handler,sizeof(handler),stdin);
if(sscanf(handler,"%d",&b.real))
break;
printf("invalid input\n");
}
while(1)
{
printf("Enter the imaginary part of your second number\n");
fgets(handler,sizeof(handler),stdin);
if(sscanf(handler,"%d",&b.fake))
break;
printf("invalid input\n");
}
if(operation=='+')
c=add(a,b);
if(operation=='-')
c=sub(a,b);
if(operation=='/')
c=div(a,b);
if(operation=='*')
c=mul(a,b);
printf("%d%+di\n",c.real,c.fake);
return 0;
}
c add(c a, c b)
{
c c;
c.real=a.real+b.real;
c.fake=a.fake+b.fake;
return c;
}
c sub(c a, c b)
{
c c;
c.real=a.real-b.real;
c.fake=a.fake-b.fake;
return c;
}
c div(c a, c b)
{
c c;
c.real=(a.real*b.real+a.fake*b.fake)/(b.real*b.real+b.fake*b.fake);
return c;
}
c mul(c a, c b)
{
c c;
c.real=a.real*b.real-(a.fake*b.fake);
c.fake=a.fake*b.real+a.real*b.fake;
return c;
}