forked from CPRO-Session1/Assignment6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.c
More file actions
126 lines (80 loc) · 2.13 KB
/
complex.c
File metadata and controls
126 lines (80 loc) · 2.13 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
//Clarke Littlejohn
//Complex numbers
//could get the division to work i should split it up into multiple parts.
#include<stdio.h>
#include<string.h>
typedef struct{
float real;
float img;
}complx;
complx addition(complx,complx);
complx subtraction(complx,complx);
complx division(complx,complx);
complx multiplication(complx,complx);
void printcomplx(complx);
int main(){
char input[3];
complx nums[2];
float stor;
printf("Enter + for addition, - for subtraction, * for multiplication, and / for division: ");
fgets(input,sizeof(input),stdin);
input[1]='\0';
printf("Enter in the first real number: ");
scanf("%f",&stor);
nums[0].real=stor;
printf("Enter in the first imaginary number: ");
scanf("%f",&stor);
nums[0].img=stor;
printf("Enter in the second real number: ");
scanf("%f",&stor);
nums[1].real=stor;
printf("Enter in the second imaginary number: ");
scanf("%f",&stor);
nums[1].img=stor;
switch(input[0]){
case '+':
printcomplx(addition(nums[0],nums[1]));
break;
case '-':
printcomplx(subtraction(nums[0],nums[1]));
break;
case '*':
printcomplx(multiplication(nums[0],nums[1]));
break;
case '/':
printcomplx(division(nums[0],nums[1]));
break;
default:
printf("Restart the program");
break;
}
return 0;
}
complx addition(complx fZero,complx fOne){
complx fTwo;
fTwo.real=(fZero.real+fOne.real);
fTwo.img=(fZero.img+fOne.img);
return fTwo;
}
complx subtraction(complx fZero,complx fOne)
{
complx fTwo;
fTwo.real=(fZero.real-fOne.real);
fTwo.img=(fZero.img-fOne.img);
return fTwo;
}
complx multiplication(complx fZero,complx fOne){
complx fTwo;
fTwo.real=(fZero.real*fOne.real)+(fZero.img*fOne.img);
fTwo.img=(fZero.real*fOne.img)+(fZero.img*fOne.real);
return fTwo;
}
complx division(complx fZero,complx fOne){
complx fTwo;
fTwo.real=((fZero.real*fOne.real)+(fZero.img* (-1*fOne.img)))/((fOne.real*fOne.real)+(fOne.img*(-1*fOne.img)));
fTwo.img=(fZero.real*fOne.img)+(fZero.img*fOne.real)/((fOne.real*fOne.real)+(fOne.img*(-1*fOne.img)));
return fTwo;
}
void printcomplx(complx out){
printf("The answer is %.2f+%.2fi\n",out.real,out.img);
}