2008. 5. 27. 10:44ㆍETC Programmings
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student {
int id;
char name[40];
int age;
} Student;
struct ArrayList {
Student *dataSet;
int length;
};
void initList(struct ArrayList *list){
list->dataSet = calloc(100, sizeof(Student));
list->length = 0;
}
void add(struct ArrayList *list, struct Student *student){
list->dataSet[list->length] = *student;
list->length = list->length + 1;
}
void display(struct ArrayList *list, int start, int end){
int i;
for(i = start; i < end ; i++){
printf("ID : %d\n", list->dataSet[i].id);
printf("이름 : %s\n", list->dataSet[i].name);
printf("나이 : %d\n", list->dataSet[i].age);
}
}
void insertStudent(struct ArrayList *list){
Student* student = malloc(sizeof(Student));
printf("이름을 입력해주세요 : ");
fflush(stdin);
gets(student->name);
printf("나이를 입력해주세요 : ");
scanf("%d", &student->age);
student->id = list->length;
add(list, student);
}
void saveToFiles(struct ArrayList *list){
FILE *fp;
int i;
char fileName[80];
fflush(stdin);
printf("저장하실 파일 이름을 입력 : ");
gets(fileName);
fp = fopen(fileName, "w+");
for(i = 0; i < list->length ; i++){
fprintf(fp, "ID : %d\n", list->dataSet[i].id);
fprintf(fp, "이름 : %s\n", list->dataSet[i].name);
fprintf(fp, "나이 : %d\n", list->dataSet[i].age);
fprintf(fp, "----------------------------------------------------\n");
}
fclose(fp);
}
void run(){
int isMore = 1;
struct ArrayList *list = malloc(sizeof(struct ArrayList));
initList(list);
while(isMore){
insertStudent(list);
printf("더 입력하실 자료가 있으십니까? 예(1), 아니오(0) ");
scanf("%d", &isMore);
}
display(list, 0, list->length);
saveToFiles(list);
}
void main(){
run();
}