-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
330 lines (313 loc) · 7.16 KB
/
Copy pathmain.c
File metadata and controls
330 lines (313 loc) · 7.16 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<getopt.h>
#define EPS1 0.00033
#define EPS2 0.00033
#ifndef START_SEG
#define START_SEG 2.1
#endif
#ifndef END_SEG
#define END_SEG 6.5
#endif
extern double f1(double x);
extern double f2(double x);
extern double f3(double x);
static int steps = 0;
static int intersection_print = 0;
static int steps_print = 0;
static int help_print = 0;
static char *help_text = "Options:\n"
"-h / --help \tshow this help\n"
"-i / --intersect print abscisses of intersections\n"
"-s / --steps \tprint amount of steps of equation solver algorithm\n"
"-r / --root \tfind intersection of 2 functions on given interval\n"
"example FUNC_NUM1 FUNC_NUM2 SEG_START SEG_END\n"
"-a / --integral \tcalculate integral of a function on given interval\n"
"example FUNC_NUM1 SEG_START SEG_END\n"
"-t / --test \tcalculate function value at given interval\n"
"example FUNC_NUM POINT\n"
"If -r and/or -a are specified -i and -s are ignored\n";
static char *root_checker;
static char *integral_checker;
static char *func_checker;
static struct option long_options[] =
{
{"help", no_argument, &help_print, 1},
{"intersections", no_argument, &intersection_print, 1},
{"steps", no_argument, &steps_print, 1},
{"test", required_argument, 0, 't'},
{"root", required_argument, 0, 'r'},
{"integral", required_argument, 0, 'a'},
{0, 0, 0, 0}
};
double inline fabs(double x){
return x > 0 ? x : -x;
}
#if METHOD == 1
//binary search
double root(double (*f)(double x), double (*g)(double x), double a, double b, double eps1){
if (a > b){
double tmp = a;
a = b;
b = tmp;
}
int mod = f(a) > g(a) ? 1 : -1;
double middle = (a+b)/2;
while ((b-a)>eps1){
steps++;
if (mod * f(middle) > mod * g(middle)){
a = middle;
} else {
b = middle;
}
middle = (a+b)/2;
}
return middle;
}
#else
//sec
double root(double (*f)(double x), double (*g)(double x), double a, double b, double eps1){
if (a > b){
double tmp = a;
a = b;
b = tmp;
}
while (fabs(f(b) - g(b)) > eps1){
steps++;
b -= (b-a) * (f(b)-g(b)) / (f(b)-g(b)-f(a)+g(a));
}
return b;
}
#endif
//trapezium method
double integral(double (*f)(double x), double a, double b, double eps2){
if (a > b){
double tmp = a;
a = b;
b = tmp;
}
int n = 1;
double prev_i = 0;
double p = (double)1/3;
double h = (b - a);
double f_summ = (f(a) + f(b)) / 2;
do {
prev_i = f_summ * h;
f_summ = (f(a) + f(b)) / 2;
n *= 2;
h = (b - a) / n;
int i = 0;
for (i = 1; i < n; i++){
f_summ += f(a + i*h);
}
} while (p*fabs(f_summ * h - prev_i) > eps2);
return f_summ * h;
}
int process_func(char * input_line){
if (!input_line){
return -1;
}
double x;
double (*f)(double x);
int num1;
sscanf(input_line, "%d%lf", &num1, &x);
// this is terrible. fix this please
switch (num1){
case 1:
f = f1;
break;
case 2:
f = f2;
break;
case 3:
f = f3;
break;
default:
return 1;
}
double res = f(x);
printf("-t execution:\n");
printf("F%d value at %lf is %lf\n", num1, x, res);
printf("---\n");
return 0;
}
int process_root(char * input_line){
if (!input_line){
return -1;
}
double a, b;
double (*f)(double x), (*g)(double x);
int num1, num2;
sscanf(input_line, "%d%d%lf%lf", &num1, &num2, &a, &b);
// this is terrible. fix this please
switch (num1){
case 1:
f = f1;
break;
case 2:
f = f2;
break;
case 3:
f = f3;
break;
default:
return 1;
}
switch (num2){
case 1:
g = f1;
break;
case 2:
g = f2;
break;
case 3:
g = f3;
break;
default:
return 1;
}
double x = root(f, g, a, b, EPS1);
printf("-r execution:\n");
printf("F%d intersects with F%d: %lf\n", num1, num2, x);
printf("---\n");
return 0;
}
int process_integral(char * input_line){
if (!input_line){
return -1;
}
double a, b;
double (*f)(double x);
int num1;
sscanf(input_line, "%d%lf%lf", &num1, &a, &b);
// this is terrible. fix this please
switch (num1){
case 1:
f = f1;
break;
case 2:
f = f2;
break;
case 3:
f = f3;
break;
default:
return 1;
}
double x = integral(f, a, b, EPS2);
printf("-a execution:\n");
printf("F%d area: %lf\n", num1, x);
printf("---\n");
return 0;
}
int main(int argc, char **argv){
#if METHOD == 1
printf("Compiled with Binary Search\n");
#else
printf("Compiled with Chords Method\n");
#endif
int option_index = 0;
int c = 0, count;
while((c = getopt_long_only(argc, argv, "hisr:a:t:", long_options, &option_index)) != -1){
option_index = 0;
switch (c)
{
case 0:
break;
case 'h':
help_print = 1;
break;
case 'i':
intersection_print = 1;
break;
case 's':
steps_print = 1;
break;
case 't':
if (func_checker){
free(func_checker);
}
func_checker = malloc(sizeof(char) * (strlen(optarg) + 1));
strcpy(func_checker, optarg);
for (count = 1; optind < argc && count < 2; optind++, count++){
strcat(func_checker, " "); // this is terrible
strcat(func_checker, argv[optind]);
}
if (count < 2){
printf("Not enough arguments for -t!\n");
abort();
}
break;
case 'r':
if (root_checker){
free(root_checker);
}
root_checker = malloc(sizeof(char) * (strlen(optarg) + 1));
strcpy(root_checker, optarg);
for (count = 1; optind < argc && count < 4; optind++, count++){
strcat(root_checker, " "); // this is terrible
strcat(root_checker, argv[optind]);
}
if (count < 4){
printf("Not enough arguments for -r!\n");
abort();
}
break;
case 'a':
if (integral_checker){
free(integral_checker);
}
integral_checker = malloc(sizeof(char) * (strlen(optarg) + 1));
strcpy(integral_checker, optarg);
for (count = 1; optind < argc && count < 3; optind++, count++){
strcat(integral_checker, " "); // this is terrible
strcat(integral_checker, argv[optind]);
}
if (count < 3){
printf("Not enough arguments for -a!\n");
abort();
}
break;
default:
abort();
}
}
if (help_print){
printf(help_text);
return 0;
}
if (root_checker || integral_checker || func_checker){
if (process_root(root_checker) == 1){
printf("Functions numbers are invalid for -r\n");
}
if (process_integral(integral_checker) == 1){
printf("Function number is invalid for -a\n");
}
if (process_func(func_checker) == 1){
printf("Function number is invalid for -a\n");
}
return 0;
}
double x1, x2, x3;
x1 = root(f1, f2, START_SEG, END_SEG, EPS1); //
x2 = root(f2, f3, START_SEG, END_SEG, EPS1); // all limits are precalculated
x3 = root(f3, f1, START_SEG, END_SEG, EPS1);//
double answ = 0;
answ += integral(f3, x3, x2, EPS2);
answ += integral(f2, x2, x1, EPS2);
answ -= integral(f1, x3, x1, EPS2);
if (intersection_print){
printf("F1 intersects with F2: %lf\n", x1);
printf("F2 intersects with F3: %lf\n", x2);
printf("F3 intersects with F1: %lf\n", x3);
printf("---\n");
}
if (steps_print){
printf("Total iterations for all equations: %d\n", steps);
printf("---\n");
}
printf("Area between thesex curves: %lf\n", answ);
return 0;
}