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
88 lines (77 loc) · 2.52 KB
/
complex.c
File metadata and controls
88 lines (77 loc) · 2.52 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
/*Ava N.*/
//This program implements complex numbers as a structure and allows a user to perform calculations with them. The structure contains real and imaginary portions of a complex number. The functions add, subtract, multiply, divide and display complex numbers. I have allowed the user to select what functions they want to use (much like my calculator.c programs from last week) and enter the complex numbers they want to perform calculations with.
#include <stdio.h>
#include <math.h>
typedef struct {
int real; //real
int imag; //imaginary
} complx;
int main(){
complx f; //first number
complx s; //second number
complx a; //answer
complx o; //operation
printf("Choose one of the following: +, -, *, /, or %%: \n");
scanf("%d\n", &o.real);
printf("Pick a real number: \n");
scanf("%d\n", &f.real);
printf("Pick another real number: \n");
scanf("%d\n", &s.real);
printf("Choose one of the following: +, -, *, /, or %%: \n");
scanf("%d\n", &o.imag);
printf("Pick an imaginary number: \n");
scanf("%d\n", &f.imag);
printf("Pick another imaginary number: \n");
scanf("%d\n", &s.imag);
switch(o.real){
case '+':
a.real=f.real+s.real;
printf("The addition is: %d\n", a.real);
break;
case '-':
a.real=f.real-s.real;
printf("The subtraction is: %d\n", a.real);
break;
case '/':
a.real=f.real/s.real;
printf("The division is: %d\n", a.real);
break;
case '*':
a.real=f.real*s.real;
printf("The multiplication is: %d\n", a.real);
break;
case '%':
a.real=(int)f.real%(int)s.real;
printf("The percentage is: %d\n", a.real);
break;
default:
printf("Your response was invalid. Please try again.");
}
switch(o.imag){
case '+':
a.imag=f.imag+s.imag;
printf("The addition is: %d\n", a.imag);
break;
case '-':
a.imag=f.imag-s.imag;
printf("The subtraction is: %d\n", a.imag);
break;
case '/':
a.imag=f.imag/s.imag;
printf("The division is:%d \n", a.imag);
break;
case '*':
a.imag=f.imag*s.imag;
printf("The multiplication is: %d\n", a.imag);
break;
case '%':
a.imag=(int)f.imag%(int)s.imag;
printf("The percentage is: %d\n", a.imag);
break;
default:
printf("Your response was invalid. Please try again.");
}
printf("%d \n", a.imag);
printf("%d \n", a.real);
return 0;
}