-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpws24-hw3.c
157 lines (142 loc) · 4.25 KB
/
pws24-hw3.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
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
147
148
149
150
151
152
153
154
155
156
157
/*
* pws24-hw3.c
*
* Created on: Apr 17, 2016
* Author: Patrick Stevens
*/
#include<stdio.h>
#include<string.h>
#define ORG_RANK 0
#define TOP_NUM 100
#define YEAR_INC 10
#define MAX_NAME_LEN 20
void babyName(const char *temp, char *idvName) {
while (*temp != ',') {
*idvName++ = *temp++;
}
*idvName = 0;
}
int searchRank(char(*bbyNames)[MAX_NAME_LEN], const char *idvName) {
int index = 0;
while (strcmp(idvName, bbyNames[index]) != 0 && strlen(bbyNames[index]) > 0) {
index++;
}
return index;
}
int processName(char (*bbyNames)[MAX_NAME_LEN], int (*bbyRank)[YEAR_INC], const char *idvName, int yearIndex, int rankInYear) {
int index, x = 0;
index = searchRank(bbyNames, idvName);
if (strlen(bbyNames[index]) == 0) {
strcpy(bbyNames[index], idvName);
while (x < YEAR_INC){
bbyRank[index][x] = ORG_RANK;
x++;
}
bbyRank[index][yearIndex] = rankInYear;
bbyNames[index + 1][0] = 0;
return 1;
} else {
bbyRank[index][yearIndex] = rankInYear;
return 0;
}
}
int inputFile(char (*bbyNames)[MAX_NAME_LEN], int (*bbyRank)[YEAR_INC], int totalNum, const char *file, int yearIndex) {
FILE *outfile;
int x = 0;
char idvName[MAX_NAME_LEN], temp[128];
if (NULL == (outfile = fopen(file, "r"))) {
return 0;
}
while(x < TOP_NUM) {
fscanf(outfile, "%s", temp);
babyName(temp, idvName);
totalNum += processName(bbyNames, bbyRank, idvName, yearIndex, 1 + x);
x++;
}
fclose(outfile);
return totalNum;
}
int allFiles(char (*bbyNames)[MAX_NAME_LEN], int (*bbyRank)[YEAR_INC], char **totalFiles, unsigned fileNum) {
int yearNum = 0, totalNum = 0;
while (fileNum--) {
totalNum = inputFile(bbyNames, bbyRank, totalNum, *totalFiles, yearNum);
yearNum++;
totalFiles++;
}
return totalNum;
}
void switchRank(char (*bbyNames)[MAX_NAME_LEN], int (*bbyRank)[YEAR_INC], int low, int idx) {
char temp[MAX_NAME_LEN];
int x = 0, pws;
//same
if (low == idx) {
return;
}
//switch
strcpy(temp, bbyNames[low]);
strcpy(bbyNames[low], bbyNames[idx]);
strcpy(bbyNames[idx], temp);
while(x < YEAR_INC) {
pws = bbyRank[low][x];
bbyRank[low][x] = bbyRank[idx][x];
bbyRank[idx][x] = pws;
x++;
}
}
void sortNames(char (*bbyNames)[MAX_NAME_LEN], int (*bbyRank)[YEAR_INC], int totalNum) {
int x = 0, j, low;
//sort
while(x < totalNum) {
low = x;
for (j = x + 1; j < totalNum; ++j) {
if (strcmp(bbyNames[j], bbyNames[low]) < 0)
low = j;
}
if (low != x) {
switchRank(bbyNames, bbyRank, low, x);
}
x++;
}
}
void writeToFile(char (*bbyNames)[MAX_NAME_LEN], int (*bbyRank)[YEAR_INC], int totalNum, char *csvfile) {
int x = 0, n;
//summary.csv
FILE *outfile;
outfile = fopen(csvfile, "w");
if (!outfile) {
printf("Problem opening te file!\n");
return 1;
}
fprintf(outfile, "%s", "Name");
while(x < YEAR_INC) {
fprintf(outfile, ",%d", 1920 + (10 * x));
x++;
}
n = 0;
while (totalNum--) {
fprintf(outfile, "\n");
fprintf(outfile, "%s", bbyNames[n]);
int x = 0;
while(x < YEAR_INC) {
if (ORG_RANK == bbyRank[n][x]) {
fprintf(outfile, ",");
} else {
fprintf(outfile, ",%d", bbyRank[n][x]);
}
x++;
}
n++;
}
fclose(outfile);
}
int main(void) {
int totalNum, bbyRank[TOP_NUM * YEAR_INC][YEAR_INC] = {0};
char bbyNames[TOP_NUM * YEAR_INC][MAX_NAME_LEN] = {0};
char *totalFiles[] = {"names/yob1920.txt", "yob1930.txt", "yob1940.txt", "yob1950.txt",
"yob1960.txt", "yob1970.txt", "yob1980.txt", "yob1990.txt",
"yob2000.txt", "yob2010.txt"};
totalNum = allFiles(bbyNames, bbyRank, totalFiles, sizeof (totalFiles) / sizeof (totalFiles[0]));
sortNames(bbyNames, bbyRank, totalNum);
writeToFile(bbyNames, bbyRank, totalNum, "summary.csv");
return 1;
}