-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
61 lines (47 loc) · 1.68 KB
/
main.c
File metadata and controls
61 lines (47 loc) · 1.68 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
#include <stdio.h>
#include <limits.h>
#include "list.h"
#define FILENAME "saved_list"
T square(const T *a) { return *a * *a; }
T cube(const T *a) { return *a * *a * *a; }
void sum(const T *val, T *a) { *a = *val + *a; }
void min(const T *val, T *a) { *a = *a < *val ? *a : *val; }
void max(const T *val, T *a) { *a = *a > *val ? *a : *val; }
void print_space(T *a) { printf("%d ", *a); }
void print_new_line(T *a) { printf("%d\n", *a); }
T absolute(const T *val) { return *val < 0 ? -*val : *val; }
T double_value(const T *val) { return *val * 2; }
void list_print(const list *l, const char *message, void (*function)(T *)) {
printf("%s", message);
list_foreach(l, function);
printf("\n");
}
int main(void) {
list *l = list_read(stdin,1);
list_print(l, "list: ", print_space);
list_print(l, "list: ", print_new_line);
list *temp = list_map(l, square);
list_print(temp, "list^2: ", print_space);
list_free(temp);
temp = list_map(l, cube);
list_print(temp, "list^3: ", print_space);
list_free(temp);
printf("list_sum: %d\n", list_reduce(0, sum, l));
printf("list_min: %d\n", list_reduce(INT_MAX, min, l));
printf("list_max: %d\n", list_reduce(INT_MIN, max, l));
list_map_mut(l, absolute);
list_print(l, "list_abs: ", print_space);
temp = list_iterate(1, 10, double_value);
list_print(temp, "powers_of_2: ", print_space);
list_free(temp);
list_save(l,FILENAME,0);
list_free(l);
list_load(l,FILENAME,0);
list_print(l, "loaded_list: ", print_space);
list_save(l,FILENAME,1);
list_free(l);
list_load(l,FILENAME,1);
list_print(l, "loaded_binary_list: ", print_space);
list_free(l);
return 0;
}