반응형
자료 구조를 들어가기 전에
과일 평가 시스템을 한번 만들어 보도록 하자!
배열로 데이터를 어떻게 구성하고 늘려갈지에 대해서 생각을 해보는 좋은 예제 인것 같다.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define TSIZE 45
#define DMAX 10
typedef struct _fruit
{
char name[TSIZE];
float score;
}fruit;
void read_file(fruit fruit_list[], int* ptr_data_size)
{
char filename[TSIZE] = {0,};
FILE* file = NULL;
int num = 0;
int i = 0;
printf("input filename to get data\n");
printf(">> ");
if(scanf("%s",filename)!=1)
{
printf("Wrong Name");
exit(1);
}
file = fopen(filename,"r");
if(file == NULL)
{
printf("ERROR : Cannot open file\n");
exit(1);
}
if(fscanf(file,"%d",&num) != 1)
{
printf("Wrong data format\n");
exit(1);
}
for(i=0;i<num;i++)
{
if(fscanf(file,"%s",fruit_list[i].name) != 1 ||
fscanf(file,"%f",&fruit_list[i].score) !=1){
printf("Wrong data format\n");
exit(1);
}
*ptr_data_size += 1;
}
assert(*ptr_data_size==num);
printf("%d data can be read for file\n", *ptr_data_size);
fclose(file);
#if 0
printf("num = %d\n",num);
for(i=0;i<num;i++)
{
printf("%d : %s\t%.1f\n", i,fruit_list[i].name,fruit_list[i].score);
}
#endif
}
int input_int(void)
{
int input = 0;
printf("\npress input value.\n");
while(1)
{
printf(">> ");
if(scanf("%d",&input)==1)
{
return input;
}
else
{
printf("Wrong input! press input value again.\n");
while(getchar()!='\n')
{
continue;
}
}
}
}
int input_menu(void)
{
int input = 0;
while(1)
{
printf("\nPlease select an option and press enter.\n");
printf("1. Print all items\t2. Print an item\n");
printf("3. Edit an item\t\t4. Add an item\n");
printf("5. Insert all items\t6. Delete an item\n");
printf("7. Delete all items\t8. Save file\n");
printf("9. Serach by name\t10. Quit\n");
input = input_int();
if(input>=1 && input<=10)
{
return input;
}
else
{
printf("\nWorng value! please input between 1~10!\n");
}
}
}
void print_all(fruit fruit_list[], int data_size)
{
int i;
for(i=0;i<data_size;i++)
{
printf("%d\t%s\t%.1f\n",i, fruit_list[i].name,fruit_list[i].score);
}
}
void print_an_item(fruit fruit_list[], int data_size)
{
int index = 0;
printf("please index number for item.");
index = input_int();
if(index < data_size)
{
printf("%d\t%s\t%.1f\n",index, fruit_list[index].name,fruit_list[index].score);
}
else
{
printf("index is larger than data size\n");
}
}
void edit_an_item(fruit fruit_list[], int data_size)
{
int index = 0;
printf("please index number for item.");
index = input_int();
if(index < data_size)
{
printf("please edit fruit name.\n");
printf(">> ");
scanf("%s",fruit_list[index].name);
printf("please edit fruit score.\n");
printf(">> ");
scanf("%f",&fruit_list[index].score);
printf("%d\t%s\t%.1f\n",index, fruit_list[index].name,fruit_list[index].score);
}
else
{
printf("index is larger than data size\n");
}
}
void add_an_item(fruit fruit_list[], int* ptr_data_size)
{
if(*ptr_data_size >= DMAX)
{
printf("Data Slot is full!\n");
}
else
{
printf("please add fruit name.\n");
printf(">> ");
scanf("%s",fruit_list[*ptr_data_size].name);
printf("please add fruit score.\n");
printf(">> ");
scanf("%f",&fruit_list[*ptr_data_size].score);
printf("%d\t%s\t%.1f\n",*ptr_data_size, fruit_list[*ptr_data_size].name,fruit_list[*ptr_data_size].score);
*ptr_data_size += 1;
}
}
void insert_an_item(fruit fruit_list[], int* ptr_data_size)
{
int index = 0;
int i;
if(*ptr_data_size >= DMAX)
{
printf("Data Slot is full!\n");
}
else
{
printf("please input for inserting index!");
index = input_int();
for(i=(*ptr_data_size)-1; i>=index;i--)
{
strcpy(fruit_list[i+1].name, fruit_list[i].name);
fruit_list[i+1].score = fruit_list[i].score;
}
printf("please insert fruit name.\n");
printf(">> ");
scanf("%s",fruit_list[index].name);
printf("please insert fruit score.\n");
printf(">> ");
scanf("%f",&fruit_list[index].score);
*ptr_data_size += 1;
}
}
void delete_an_item(fruit fruit_list[], int* ptr_data_size)
{
int index = 0;
int i;
printf("please input for deleting index!");
index = input_int();
for(i=index; i<(*ptr_data_size)-1;i++)
{
strcpy(fruit_list[i].name, fruit_list[i+1].name);
fruit_list[i].score = fruit_list[i+1].score;
}
*ptr_data_size -= 1;
}
void write_file(fruit fruit_list[], int data_size)
{
char filename[TSIZE] = {0,};
FILE* file = NULL;
int num = data_size;
int i = 0;
printf("input filename to get data\n");
printf(">> ");
if(scanf("%s",filename)!=1)
{
printf("Wrong Name");
exit(1);
}
file = fopen(filename,"w");
if(file == NULL)
{
printf("ERROR : Cannot open file\n");
exit(1);
}
fprintf(file,"%d\n",num);
for(i=0;i<num;i++)
{
fprintf(file,"%s\n",fruit_list[i].name);
fprintf(file,"%.1f\n",fruit_list[i].score);
}
fclose(file);
printf("%d data can be write for file\n", num);
}
void search_by_name(fruit fruit_list[], int data_size)
{
char search_name[TSIZE] = {0,};
int index = 0;
printf("input fruit name to search.\n");
printf(">> ");
if(scanf("%s",search_name)!=1)
{
printf("Wrong Name");
exit(1);
}
for(index=0;index<data_size;index++)
{
if(strcmp(fruit_list[index].name, search_name)==0)
{
break;
}
}
if(index >= data_size)
{
printf("Can not find the fruit name!\n");
}
else
{
printf("%d : %s\t%.1f\n", index,fruit_list[index].name,fruit_list[index].score);
}
}
int main(void)
{
fruit fruit_list[DMAX];
int data_size = 0;
int s = 0;
read_file(fruit_list, &data_size);
while(1)
{
s = input_menu();
printf("\nStart Menu : %d\n\n",s);
switch(s)
{
case 1:
print_all(fruit_list,data_size);
break;
case 2:
print_an_item(fruit_list,data_size);
break;
case 3:
edit_an_item(fruit_list,data_size);
break;
case 4:
add_an_item(fruit_list,&data_size);
break;
case 5:
insert_an_item(fruit_list,&data_size);
break;
case 6:
delete_an_item(fruit_list,&data_size);
break;
case 7:
data_size = 0;
break;
case 8:
write_file(fruit_list,data_size);
break;
case 9:
search_by_name(fruit_list,data_size);
break;
case 10:
printf("\nGood Bye!\n");
exit(1);
break;
default:
/*No Code*/
break;
}
}
return 0;
}
해당 결과는 아래와 같다.
반응형
'Computer Language > Data Structure' 카테고리의 다른 글
[자료구조] 05. Queue List 구조 예제 (C언어) (0) | 2021.07.10 |
---|---|
[자료구조] 04. Queue 배열 구조 예제 (C언어) (0) | 2021.07.10 |
[자료구조] 03. Stack Flood Fill 예제 (C언어) (0) | 2021.07.10 |
[자료구조] 02. Stack 구조 Basic 예제 (C언어) (0) | 2021.07.09 |
[자료구조] 01. List 구조 Baisc 예제 (C언어) (0) | 2021.07.09 |
댓글