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
104 lines (84 loc) · 2.92 KB
/
Copy pathcomplex.c
File metadata and controls
104 lines (84 loc) · 2.92 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
/* Yael Kelmer.
This code uses a structure to create a complex number. The user is prompted to input two complex numbers and choose an operation. Using functions that I created the operations are executed.*/
#include <stdio.h>
typedef struct {
float realPart;
float imaginaryPart;
} complexNum;
complexNum add(complexNum c1, complexNum c2) {
float newRealPart = c1.realPart + c2.realPart;
float newImagPart = c1.imaginaryPart + c2.imaginaryPart;
complexNum result = {newRealPart, newImagPart};
return result;
}
complexNum subtract(complexNum c1, complexNum c2) {
float newRealPart = c1.realPart - c2.realPart;
float newImagPart = c1.imaginaryPart - c2.imaginaryPart;
complexNum result = {newRealPart, newImagPart};
return result;
}
complexNum multiply (complexNum c1, complexNum c2) {
float newRealPart = (c1.realPart*c2.realPart) - (c1.imaginaryPart*c2.imaginaryPart);
float newImagPart = (c1.realPart*c2.imaginaryPart) + (c1.imaginaryPart*c2.realPart);
complexNum result = {newRealPart, newImagPart};
return result;
}
complexNum divide (complexNum c1, complexNum c2) {
float newRealPart = ((c1.realPart*c2.realPart) + (c1.imaginaryPart*c2.imaginaryPart)) / ((c2.realPart*c2.realPart) + (c2.imaginaryPart*c2.imaginaryPart));
float newImagPart = ((-c1.realPart*c2.imaginaryPart) + (c1.imaginaryPart*c2.realPart)) / ((c2.realPart*c2.realPart) + (c2.imaginaryPart*c2.imaginaryPart));
complexNum result = {newRealPart, newImagPart};
return result;
}
void display (complexNum c) {
printf ("%.1f+%.1fi\n", c.realPart, c.imaginaryPart);
}
int main () {
float a;
float b;
float c;
float d;
printf ("Please print the real part of your first complex number.\n");
scanf ("%f", &a);
printf ("Please print the imaginary part of your first complex number (do not include the 'i').\n");
scanf ("%f", &b);
printf ("Please print the real part of your second complex number.\n");
scanf ("%f", &c);
printf ("Please print the imaginary part of your second complex number (do not include the 'i').\n");
scanf ("%f", &d);
complexNum c1 = {a, b};
complexNum c2 = {c, d};
printf ("This is your first complex number:");
display(c1);
printf ("This is your second complex number:");
display(c2);
printf ("Please choose and type one of the following operations to apply to the two complex numbers: + , - , * , /\n");
char operation;
scanf (" %c", &operation);
switch (operation)
{
case '+': ;
complexNum sum = add(c1, c2);
printf ("This is the sum: ");
display (sum);
printf ("\n");
break;
case '-': ;
complexNum difference = subtract(c1, c2);
printf ("This is the difference: ");
display (difference);
printf ("\n");
break;
case '*': ;
complexNum product = multiply(c1, c2);
printf ("This is the product: ");
display (product);
printf ("\n");
break;
case '/': ;
complexNum quotient = divide(c1, c2);
printf ("This is the quotient: ");
display (quotient);
printf ("\n");
break;
}
}