#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct bkcmpy {
char name[100];
int pub;
};
void select_sort(struct bkcmpy *[]);
void touroku(struct bkcmpy *[], char[]);
void bunkatu(char[], char[], char[], int *, int *);
int main(int argc, char *argv[]) {
FILE *f1;
struct bkcmpy *bkcmpydb[150];
char buf[4048], cname[100], bname[400];
int i, year, price;
for (i = 0; i < 150; i++)
bkcmpydb[i] = NULL;
f1 = fopen(argv[1], "r");
fgets(buf, 4048, f1);
while (fgets(buf, 4048, f1) != NULL ) {
bunkatu(buf, cname, bname, &year, &price);
if ((year >= 2000) && (year <= 2012)) {
touroku(bkcmpydb, cname);
}
}
select_sort(bkcmpydb);
for (i = 0; bkcmpydb[i] != NULL ; i++)
printf("%s %d\n", bkcmpydb[i]->name, bkcmpydb[i]->pub);
fclose(f1);
return EXIT_SUCCESS;
}
void select_sort(struct bkcmpy *bkcmpydb[]) {
int i;
int j;
int max;
struct bkcmpy *t;
for (i = 0; i < 150 - 1; i++) {
if (bkcmpydb[i] == NULL) {
break;
}
max = i;
for (j = i + 1; j < 150; j++) {
if (bkcmpydb[j] == NULL) {
break;
}
if (bkcmpydb[max]->pub < bkcmpydb[j]->pub) {
max = j;
}
}
if (i < max) {
t = bkcmpydb[i];
bkcmpydb[i] = bkcmpydb[max];
bkcmpydb[max] = t;
}
}
}
void touroku(struct bkcmpy *bkcmpydb[], char cname[]) {
struct bkcmpy *b;
int i;
int j;
for (i = 0; i < 150; i++) {
b = bkcmpydb[i];
if (b == NULL) {
break;
}
if (strcmp(b->name, cname) == 0) {
break;
}
}
if (150 <= i) {
return;
}
if (b == NULL) {
b = malloc(sizeof(struct bkcmpy));
for (j = 0; j < 100; j++) {
b->name[j] = cname[j];
}
b->pub = 1;
bkcmpydb[i] = b;
} else {
b->pub++;
}
}
void bunkatu(char buf[], char cname[], char bname[], int *y, int *p) {
/* state */
const int stateNormal = 0;
const int stateQuate = 1;
const int stateEnd = -1;
int state;
/* copy target */
const int targetCname = 0;
const int targetBname = 1;
const int targetY = 2;
const int targetP = 3;
const int targetNone = -1;
int target;
char buffer[1024] = {0};
int bufferCount;
char c;
int i;
int j;
state = stateNormal;
target = targetCname;
bufferCount = 0;
for (i = 0; i < 4048; i++) {
c = buf[i];
if (state == stateNormal) {
if (c == '\0') {
state = stateEnd;
break;
} else if (c == '\"') {
state = stateQuate;
} else if (c == ',') {
if (target == targetCname) {
for (j = 0; j < bufferCount; j++) {
cname[j] = buffer[j];
}
cname[j] = '\0';
target = targetBname;
} else if (target == targetBname) {
for (j = 0; j < bufferCount; j++) {
bname[j] = buffer[j];
}
bname[j] = '\0';
target = targetY;
} else if (target == targetY) {
*y = atoi(buffer);
target = targetP;
} else if (target == targetP) {
*p = atoi(buffer);
target = targetNone;
}
while (0 < bufferCount) {
--bufferCount;
buffer[bufferCount] = 0;
}
} else if (c == ' ') {
if (bufferCount == 0) {
/* none */
} else {
buffer[bufferCount] = c;
bufferCount++;
}
} else {
buffer[bufferCount] = c;
bufferCount++;
}
} else if (state == stateQuate) {
if (c == '\0') {
state = stateEnd;
break;
} else if (c == '\"') {
state = stateNormal;
} else if (c == ',') {
buffer[bufferCount] = c;
bufferCount++;
} else if (c == ' ') {
buffer[bufferCount] = c;
bufferCount++;
} else {
buffer[bufferCount] = c;
bufferCount++;
}
}
}
if (target == targetCname) {
for (j = 0; j < bufferCount; j++) {
cname[j] = buffer[j];
}
cname[j] = '\0';
target = targetBname;
} else if (target == targetBname) {
for (j = 0; j < bufferCount; j++) {
bname[j] = buffer[j];
}
bname[j] = '\0';
target = targetY;
} else if (target == targetY) {
*y = atoi(buffer);
target = targetP;
} else if (target == targetP) {
*p = atoi(buffer);
target = targetNone;
}
}