[ create a new paste ] login | about

Link: http://codepad.org/txVGoUmT    [ raw code | output | fork ]

C, pasted on Sep 25:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct bkcmpy {
	char name[100];
	char book[420];
	int year;
	int price;
};

void select_sort2(struct bkcmpy *[]);
void touroku(struct bkcmpy *[], char[], char[], int, int);
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, bname, year, price);
		}
	}
	select_sort2(bkcmpydb);
	for (i = 0; bkcmpydb[i] != NULL ; i++)
		printf("%s %s %d %d\n", bkcmpydb[i]->name, bkcmpydb[i]->book,
				bkcmpydb[i]->year, bkcmpydb[i]->price);
	fclose(f1);
}

void select_sort2(struct bkcmpy *bkcmpydb[]) {
	int i;
	int j;
	
	int max;
	struct bkcmpy *t;
	
	for (i = 0; i < 149; i++) {
		if (bkcmpydb[i] == NULL) {
			break;
		}
		max = i;
		for (j = i + 1; j < 150; j++) {
			if (bkcmpydb[j] == NULL) {
				break;
			}
			if (bkcmpydb[max]->price < bkcmpydb[j]->price) {
				max = j;
			}
		}
		if (i < max) {
			t = bkcmpydb[i];
			bkcmpydb[i] = bkcmpydb[max];
			bkcmpydb[max] = t;
		}
	}
}

void touroku(struct bkcmpy *bkcmpydb[], char cname[], char bname[], int year, int price) {
	int i;
	struct bkcmpy *b;
	
	b = malloc(sizeof(struct bkcmpy));
	for (i = 0; i < 100; i++) {
		b->name[i] = cname[i];
	}
	for (i = 0; i < 400; i++) {
		b->book[i] = bname[i];
	}
	b->year = year;
	b->price = price;
	
	for (i = 0; i < 150; i++) {
		if (bkcmpydb[i] == NULL) {
			bkcmpydb[i] = b;
			break;
		}
	}
}

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 l;
	
	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 (l = 0; l < bufferCount; l++) {
						cname[l] = buffer[l];
					}
					cname[l] = '\0';
					target = targetBname;
				} else if (target == targetBname) {
					for (l = 0; l < bufferCount; l++) {
						bname[l] = buffer[l];
					}
					bname[l] = '\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 (l = 0; l < bufferCount; l++) {
			cname[l] = buffer[l];
		}
		cname[l] = '\0';
		target = targetBname;
	} else if (target == targetBname) {
		for (l = 0; l < bufferCount; l++) {
			bname[l] = buffer[l];
		}
		bname[l] = '\0';
		target = targetY;
	} else if (target == targetY) {
		*y = atoi(buffer);
		target = targetP;
	} else if (target == targetP) {
		*p = atoi(buffer);
		target = targetNone;
	}
}


Output:
1
Segmentation fault


Create a new paste based on this one


Comments: