forked from embedded2016/phonebook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate.c
74 lines (68 loc) · 2.24 KB
/
calculate.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
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp = fopen("orig.txt", "r");
FILE *output = fopen("output.txt", "w");
if (!fp) {
printf("ERROR opening input file orig.txt\n");
exit(0);
}
int i = 0;
char append[50], find[50];
double orig_sum_a = 0.0, orig_sum_f = 0.0, orig_a, orig_f;
for (i = 0; i < 100; i++) {
if (feof(fp)) {
printf("ERROR: You need 100 datum instead of %d\n", i);
printf("run 'make run' longer to get enough information\n\n");
exit(0);
}
fscanf(fp, "%s %s %lf %lf\n", append, find,&orig_a, &orig_f);
orig_sum_a += orig_a;
orig_sum_f += orig_f;
}
fclose(fp);
fp = fopen("opt.txt", "r");
if (!fp) {
fp = fopen("orig.txt", "r");
if (!fp) {
printf("ERROR opening input file opt.txt\n");
exit(0);
}
}
double opt_sum_a = 0.0, opt_sum_f = 0.0, opt_a, opt_f;
for (i = 0; i < 100; i++) {
if (feof(fp)) {
printf("ERROR: You need 100 datum instead of %d\n", i);
printf("run 'make run' longer to get enough information\n\n");
exit(0);
}
fscanf(fp, "%s %s %lf %lf\n", append, find,&opt_a, &opt_f);
opt_sum_a += opt_a;
opt_sum_f += opt_f;
}
fp = fopen("opt_hash.txt", "r");
if (!fp) {
fp = fopen("orig.txt", "r");
if (!fp) {
printf("ERROR opening input file opt.txt\n");
exit(0);
}
}
double opt_hash_sum_a = 0.0, opt_hash_sum_f = 0.0, opt_hash_a, opt_hash_f;
for (i = 0; i < 100; i++) {
if (feof(fp)) {
printf("ERROR: You need 100 datum instead of %d\n", i);
printf("run 'make run' longer to get enough information\n\n");
exit(0);
}
fscanf(fp, "%s %s %lf %lf\n", append, find,&opt_hash_a, &opt_hash_f);
opt_hash_sum_a += opt_hash_a;
opt_hash_sum_f += opt_hash_f;
}
fprintf(output, "append() %lf %lf %lf\n",orig_sum_a / 100.0, opt_sum_a / 100.0, opt_hash_sum_a / 100.0);
fprintf(output, "findName() %lf %lf %lf", orig_sum_f / 100.0, opt_sum_f / 100.0, opt_hash_sum_f / 100.0);
fclose(output);
fclose(fp);
return 0;
}