/home/toolbox/public_html/solutions/102/10226/a.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <stdlib.h>
7 #include <math.h>
8 #include <stdint.h>
9
10 #define TRUE (1 == 1)
11 #define FALSE (1 != 1)
12
13 #define DEBUG if (FALSE)
14
15 /* fprintf(stderr, "functionName: message", varslist); */
16
17 /*
18 * Author: Isaac Traxler
19 * Date: 2018-11-05
20 * Purpose: fun
21 * Problem: 10226
22 */
23
24 /*
25 * This template reads data a specified number of times.
26 */
27
28 #define MAX_LENGTH 36
29 #define MAX_LENGTH_MINUS_1 35
30 #define TREE_MAX 10002
31
32 typedef struct TREE_STRUCT
33 {
34 /* struct TREE_STRUCT */
35 char name[MAX_LENGTH];
36 int count;
37 int length;
38 } /* struct TREE_STRUCT */
39 TREE_STRUCT;
40
41
42 int numberOfTimes;
43 char buf[MAX_LENGTH];
44 TREE_STRUCT trees[TREE_MAX];
45 int treeCnt;
46
47 void init()
48 {
49 /* FUNCTION init */
50 scanf("%d ", &numberOfTimes);
51 /* skip over initial blank line */
52 fgets(buf, MAX_LENGTH_MINUS_1, stdin);
53 } /* FUNCTION init */
54
55 void dump()
56 {
57 /* FUNCTION dump */
58 int i;
59
60 for (i=0; treeCnt>i; i++)
61 {
62 printf("%3d: [%s] %d\n", i, trees[i].name, trees[i].count);
63 }
64 } /* FUNCTION dump */
65
66 int compare(const void *ap, const void *bp)
67 {
68 /* FUNCTION compare function for qsort */
69 int toReturn = 0;
70 struct TREE_STRUCT *a = ap;
71 struct TREE_STRUCT *b = bp;
72
73 toReturn = strcmp(a->name, b->name);
74 return toReturn;
75 } /* FUNCTION compare function for qsort */
76
77
78
79 void getInput()
80 {
81 /* FUNCTION getInput */
82 char * tmp;
83
84 treeCnt = 0;
85 buf[0] = 'a';
86 tmp = fgets(trees[treeCnt].name, MAX_LENGTH_MINUS_1, stdin);
87 if (NULL == tmp)
88 {
89 buf[0] = 0;
90 }
91 while (33 < buf[0])
92 {
93 /* read another line */
94 trees[treeCnt].length = strlen(buf) - 1;
95 buf[trees[treeCnt].length] = 0;
96 trees[treeCnt].count = 1;
97 strcpy(trees[treeCnt].name, buf);
98 treeCnt++;
99 tmp = fgets(buf, MAX_LENGTH_MINUS_1, stdin);
100 if (NULL == tmp)
101 {
102 buf[0] = 0;
103 }
104 } /* read another line */
105 qsort(trees, treeCnt, sizeof(TREE_STRUCT), compare);
106 printf("Lines read: %d\n", treeCnt);
107 } /* FUNCTION getInput */
108
109 void process()
110 {
111 /* FUNCTION process */
112 dump();
113 } /* FUNCTION process */
114
115 int main()
116 {
117 /* main */
118 int i;
119
120 init();
121 for (i=0; i<numberOfTimes; i++)
122 {
123 /* while */
124 getInput();
125 process();
126 } /* while */
127
128 return EXIT_SUCCESS;
129 } /* main */
130
131