-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutput.h
146 lines (120 loc) · 5.15 KB
/
output.h
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
#define OUTPUT_FILE_NAME "fundne_planter.txt"
/*
for printing results to .txt file
*/
/* Prototypes */
void create_output(struct matched_flora *matched_flora, struct area area);
int compFunc (const void * a, const void * b);
int mfo_types_sum(int mfoTypes[3]);
float vulnerability_average(struct matched_flora *matched_flora);
void print_mfo_types(int mfoTypes[3], FILE *output_file);
void mfo_area_percentage (struct area area, FILE *output_file);
void create_output(struct matched_flora *matched_flora, struct area area) {
int i, j;
FILE *output_file;
output_file = fopen (OUTPUT_FILE_NAME, "w+");
printf("Foelgende planter kan plantes i det specificerede areal. Planterne er sorteret \n"
"efter deres nytte for det truede insektliv paa den danske roedliste. Bogstaverne\n"
"i parantes er insektets roedliste kategori. RE - Regionalt Uddoed, CR - Kritisk \n"
"truet, EN - Truet, VU - Saarbar, NT - Naesten truet\n");
fprintf(output_file, "Foelgende planter kan plantes i det specificerede areal. Planterne er sorteret \n"
"efter deres nytte for det truede insektliv paa den danske roedliste. Bogstaverne\n"
"i parantes er insektets roedliste kategori. RE - Regionalt Uddoed, CR - Kritisk \n"
"truet, EN - Truet, VU - Saarbar, NT - Naesten truet\n");
mfo_area_percentage (area, output_file);
printf("%-40s | %-54s | %s\n", "Plantens navn", "MFO-omraader planten er godkendt til", "Insekter planten gavner");
fprintf(output_file, "%-40s | %-54s | %s\n", "Plantens navn", "MFO-omraader planten er godkendt til", "Insekter planten gavner");
qsort(matched_flora, MAX_NUMBER_OF_MATCHES, sizeof(struct matched_flora), *compFunc);
for (i = 0; i < MAX_NUMBER_OF_MATCHES; i++) {
if (strcmp(matched_flora[i].floraLatinName, "") != 0) {
int floraHash = hash(matched_flora[i].floraLatinName);
printf("%-40s |", flora[floraHash].danishName);
fprintf(output_file, "%-40s |", flora[floraHash].danishName);
print_mfo_types(flora[floraHash].mfoTypes, output_file);
j = 0;
while (strcmp(matched_flora[i].matchedFaunaLatinName[j], "") != 0 && j < MAX_FLORA_PER_FAUNA) {
int faunaHash = hash(matched_flora[i].matchedFaunaLatinName[j]);
if (j != 0) {
printf(",");
fprintf(output_file, ",");
}
printf(" %s (%s)", fauna[faunaHash].danishName, endanger_name(fauna[faunaHash].endangerlvl));
fprintf(output_file, " %s (%s)", fauna[faunaHash].danishName, endanger_name(fauna[faunaHash].endangerlvl));
j++;
}
printf("\n");
fprintf(output_file, "\n");
}
}
fclose(output_file);
}
/* Comparison function for qsort */
int compFunc (const void *a, const void *b) {
struct matched_flora *matched_flora1 = (struct matched_flora *)a;
struct matched_flora *matched_flora2 = (struct matched_flora *)b;
int matched_flora1_hash, matched_flora2_hash;
/* Empty ones go last */
if (strcmp(matched_flora1->floraLatinName, "") == 0) {
return 1;
}
if (strcmp(matched_flora2->floraLatinName, "") == 0) {
return -1;
}
matched_flora1_hash = hash(matched_flora1->floraLatinName);
matched_flora2_hash = hash(matched_flora2->floraLatinName);
/* Supporting the most vulnerable fauna goes first */
if (vulnerability_average(matched_flora1) < vulnerability_average(matched_flora2)) {
return -1;
}
if (vulnerability_average(matched_flora2) < vulnerability_average(matched_flora1)) {
return 1;
}
/* Most approved MFO types go first */
if (mfo_types_sum(flora[matched_flora1_hash].mfoTypes) > mfo_types_sum(flora[matched_flora2_hash].mfoTypes)) {
return -1;
}
if (mfo_types_sum(flora[matched_flora2_hash].mfoTypes) > mfo_types_sum(flora[matched_flora1_hash].mfoTypes)) {
return 1;
}
/* All else equal */
return 0;
}
int mfo_types_sum(int mfoTypes[3]) {
int i, sum = 0;
for (i = 0; i < 3; i++) {
sum += mfoTypes[i];
}
return sum;
}
float vulnerability_average(struct matched_flora *matched_flora) {
int i = 0, sum = 0;
while (strcmp(matched_flora->matchedFaunaLatinName[i], "") != 0 && i < MAX_FLORA_PER_FAUNA) {
int faunaHash = hash(matched_flora->matchedFaunaLatinName[i]);
sum += fauna[faunaHash].endangerlvl;
i++;
}
/* Not helping any fauna */
if (i == 0) {
return 1000;
}
/* Returning average, with bias for more fauna */
return (float) sum/ (float) (i+1) - (i*0.5);
}
void print_mfo_types(int mfoTypes[3], FILE *output_file) {
char* mfoPrint;
if (mfoTypes[2]) {
mfoPrint = "MFO-braemmer, MFO-brak, MFO-blomster- og bestoeverbrak";
} else if (mfoTypes[1]) {
mfoPrint = "MFO-braemmer, MFO-brak";
} else {
mfoPrint = "MFO-braemmer, MFO-brak, bemaerk fleraarig plante";
}
printf(" %-54s |", mfoPrint);
fprintf(output_file, " %-54s |", mfoPrint);
}
void mfo_area_percentage (struct area area, FILE *output_file){
float procent_of_mfo = 0;
procent_of_mfo = (float) area.mfoArea / (float) area.totalArea * 100;
printf("Omraadet omlagt til MFO er %.2f procent af totale det areal.\n", procent_of_mfo);
fprintf(output_file, "Omraadet omlagt til MFO er %.2f procent af totale det areal.\n", procent_of_mfo);
}