/home/toolbox/public_html/solutions/104/10420/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: 2015-03-13
20 * Purpose:
21 * Problem: 10420
22 */
23
24 /*
25 * This template reads data a specified number of times.
26 */
27
28 #define MAX_LINE 80
29 #define MAX_COUNTRIES 2000
30
31
32 struct countryType
33 {
34 /* struct countryType */
35 char name[MAX_LINE];
36 int count;
37 };/* struct countryType */
38
39 int numberOfTimes;
40 struct countryType countries[MAX_COUNTRIES];
41 int countryCount = 0;
42 char line[MAX_LINE];
43
44 void init()
45 {
46 /* FUNCTION init */
47 scanf("%d ", &numberOfTimes);
48 } /* FUNCTION init */
49
50 void dump()
51 {
52 /* FUNCTION dump */
53 } /* FUNCTION dump */
54
55 int countryFound(char name[])
56 {
57 /* FUNCTION countryFound */
58 int i;
59 int found = FALSE;
60
61 for (i=0; (! found) && (i<countryCount); i++)
62 {
63 /* for */
64 found = (0 == strcmp(name, countries[i].name));
65 } /* for */
66 i = i - 1; /* subtract one because I increment past the match */
67
68 if (! found)
69 {
70 i = -1;
71 }
72 return i;
73 } /* FUNCTION countryFound */
74
75 void getInput()
76 {
77 /* FUNCTION getInput */
78 int i;
79 int tmp;
80 char *ptr;
81
82 fgets(line, MAX_LINE, stdin);
83 for (i=0; ' ' == line[i]; i++); /* skip over initial spaces */
84 ptr = &line[i]; /* set ptr to start at first non-blank */
85 for (i=0; ' ' != ptr[i]; i++); /* find first blank (end of country name) */
86 ptr[i] = 0; /* set blank location to 0, effectively ending string */
87 tmp = countryFound(ptr);
88 if (0 > tmp)
89 {
90 /* not found */
91 strcpy(countries[countryCount].name, ptr);
92 countries[countryCount].count = 1;
93 countryCount++;
94 } /* not found */
95 else
96 {
97 /* found */
98 countries[tmp].count = countries[tmp].count + 1;
99 } /* found */
100 } /* FUNCTION getInput */
101
102 int sortCountries(const void *ap, const void *bp)
103 {
104 /* FUNCTION compare function for qsort */
105 int toReturn = 0;
106 struct countryType *a = ap;
107 struct countryType *b = bp;
108
109 toReturn = strcmp(a->name, b->name);
110 return toReturn;
111 } /* FUNCTION compare function for qsort */
112
113 void process()
114 {
115 /* FUNCTION process */
116 int i;
117
118 qsort(countries, countryCount, sizeof(struct countryType), sortCountries);
119 for (i=0; i<countryCount; i++)
120 {
121 /* for */
122 printf("%s %d\n",countries[i].name, countries[i].count);
123 } /* for */
124 } /* FUNCTION process */
125
126 int main()
127 {
128 /* main */
129 int i;
130
131 init();
132 for (i=0; i<numberOfTimes; i++)
133 {
134 /* while */
135 getInput();
136 } /* while */
137 process();
138
139 return EXIT_SUCCESS;
140 } /* main */
141
142