-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.c
145 lines (125 loc) · 3.32 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "complex.h"
#include <math.h>
#include <stdio.h>
#define OK 1
#define FALSE 0
#define TRUE 1
/* Function declarations */
int is_over(void);
void prn_comp(complex* comp);
int get_num(int needs_comma, double* param);
int is_comma(char c);
complex* get_variable(void);
char get_nonspace_char(void);
extern int is_running;
/* Function to update the complex variable members */
void read_comp(void)
{
complex* comp = get_variable();
double real, img;
if (!comp || !is_comma(get_nonspace_char())) {
return;
}
if (get_num(TRUE, &real) != OK || get_num(FALSE, &img) != OK) {
return;
}
comp->real = real;
comp->img = img;
}
/* Function to add two complex variables and print the result */
void add_comp(void)
{
complex* comp1 = get_variable();
if (!comp1 || !is_comma(get_nonspace_char())) {
return;
}
complex* comp2 = get_variable();
if (comp2 && is_over() == OK) {
complex result = {comp1->real + comp2->real, comp1->img + comp2->img};
prn_comp(&result);
}
}
/* Function to subtract two complex variables and print the result */
void sub_comp(void)
{
complex* comp1 = get_variable();
if (!comp1 || !is_comma(get_nonspace_char())) {
return;
}
complex* comp2 = get_variable();
if (comp2 && is_over() == OK) {
complex result = {comp1->real - comp2->real, comp1->img - comp2->img};
prn_comp(&result);
}
}
/* Function to print a complex variable */
void print_comp(void)
{
complex* comp = get_variable();
if (comp && is_over() == OK) {
prn_comp(comp);
}
}
/* Function to calculate and print the absolute value of a complex variable */
void abs_comp(void)
{
complex* comp = get_variable();
if (comp && is_over() == OK) {
double abs_value =
sqrt(comp->real * comp->real + comp->img * comp->img);
printf("Absolute value: %.2f\n", abs_value);
}
}
/* Function to multiply a complex variable with a real number and print the
* result */
void mult_comp_real(void)
{
complex* comp = get_variable();
if (!comp) {
return;
}
if (!is_comma(get_nonspace_char())) {
return;
}
double real_num;
if (get_num(FALSE, &real_num) == OK) {
complex result = {comp->real * real_num, comp->img * real_num};
prn_comp(&result);
}
}
/* Function to multiply a complex variable with an imaginary number and print
* the result */
void mult_comp_img(void)
{
complex* comp = get_variable();
if (!comp || !is_comma(get_nonspace_char())) {
return;
}
double img_num;
if (get_num(FALSE, &img_num) == OK) {
complex result = {-comp->img * img_num, comp->real * img_num};
prn_comp(&result);
}
}
/* Function to multiply two complex variables and print the result */
void mult_comp_comp(void)
{
complex* comp1 = get_variable();
if (!comp1 || !is_comma(get_nonspace_char())) {
return;
}
complex* comp2 = get_variable();
if (comp2 && is_over() == OK) {
complex result = {comp1->real * comp2->real - comp1->img * comp2->img,
comp1->real * comp2->img + comp1->img * comp2->real};
prn_comp(&result);
}
}
/* Function to stop the program */
void stop(void)
{
if (is_over()) {
printf("Program stopped\n");
is_running = FALSE;
}
}