시쓰는님 봐주세요.
민들레
질문 제목 : 시쓰는님 봐주세요.소스 코드 올립니다.질문 내용 :
/* main.c */
#include stdio.h
#include stdlib.h
#include menu.h
#include student.h
int main(void)
{
student *std = null;
student *temp = null;
student **arr = &std;
int total_std_num = 0;
int std_num = 0;
int menu = 0;
int i;
do{
printf(전체 학생수를 입력하세요 : );
if( scanf(%d, &total_std_num) == 0 )
{
printf(잘못 입력하셨습니다.\n);
fflush(stdin);
continue;
}
}while( total_std_num = 0 );
arr = (student**)malloc(sizeof(student*)); //궂이 이 문장이 필요한가 의문중입니다...이미 필드에서 하나 가르킬 변수를
std = (student*)malloc(sizeof(student) * total_std_num); 선언했다고 생각하기 때문입니다.
if( std == null )
{
printf(동적 메모리 할당 실패\n);
exit(1);
}
while(1)
{
menu = selectmenu();
if( menu == quit_program )
{
break;
}
switch(menu)
{
case add_student:
if(addstudentinfo(std, total_std_num, &std_num) == 0)
{
student *temp = expandstdarray(std, &total_std_num);
if( temp == null )
{
printf(더 이상 학생 정보를 추가할 수 없습니다.\n);
}
else
{
std = temp;
printf(전체 학생수를 %d로 늘립니다.\n, total_std_num);
}
}
else
{
printf(학생 정보가 추가되었습니다.\n);
}
break;
case list_by_num:
listbynumber(std, std_num);
break;
case list_by_name:
listbyname(std, std_num);
break;
case find_student:
findstudent(std, arr, &std_num, total_std_num);
break;
}
}
}
/* menu.c */
#include stdio.h
#include menu.h
#include student.h
int selectmenu(void)
{
int choice;
while(1)
{
printf(\n-------------------\n);
printf( 1. 학생 정보 추가\n);
printf( 2. 학번순 출력\n);
printf( 3. 이름순 출력\n);
printf( 4. 학생 정보 검색\n);
printf( 0. 종료\n);
printf(-------------------\n);
printf(메뉴 선택 : );
if( scanf(%d, &choice) == 0 )
{
printf(잘못 입력하셨습니다.\n);
fflush(stdin);
continue;
}
if( choice = 0 && choice = 4)
{
return choice;
}
else
{
printf(잘못 입력하셨습니다.\n);
continue;
}
}
}
int selectfindmenu(void)
{
int choice;
while(1)
{
printf(\n------------------\n);
printf(1. 학생 정보 수정\n);
printf(2. 학생 정보 삭제\n);
printf(0. 이전 메뉴\n);
printf(------------------\n);
printf(메뉴 선택 : );
if( scanf(%d, &choice) == 0 )
{
printf(잘못 입력하셨습니다.\n);
fflush(stdin);
continue;
}
if( choice =0 && choice =2 )
{
return choice;
}
else
{
printf(잘못 입력하셨습니다.\n);
continue;
}
}
}
/* student.c */
#include stdio.h
#include stdlib.h
#include string.h
#include student.h
#include menu.h
int addstudentinfo(student *std, int totalcnt, int *pcurcnt)
{
int i;
student *p = null;
if( totalcnt == *pcurcnt )
{
return 0;
}
p = &std[*pcurcnt];
printf(\n학번과 이름을 입력하세요 : \n);
scanf(%d %s, &p-number, &p-name);
for(i=0; i*pcurcnt; i++)
{
if(p-number == std[i].number)
{
printf(동일한 학번을 갖는 학생이 있습니다.\n);
printf(다시 입력하세요 : );
scanf(%d, &p-number);
}
}
(*pcurcnt)++;
return *pcurcnt;
}
void listbynumber(student *std, int cnt)
{
if( cnt == 0 )
{
printf(출력할 내용이 없습니다.\n);
return;
}
qsort(std, cnt, sizeof(student), comparebynumber);
printstdlist(std, cnt);
}
void listbyname(student *std, int cnt)
{
if( cnt == 0 )
{
printf(출력할 내용이 없습니다.\n);
return;
}
qsort(std, cnt, sizeof(student), comparebyname);
printstdlist(std, cnt);
}
void printstdlist(const student *std, int cnt)
{
int i;
printf(\n==========================\n);
printf( 학번&nbnbsp; 이름\n);
printf(==========================\n);
for( i=0; icnt; i++)
{
printf(%d %s \n, std[i].number, std[i].name);
}
printf(==========================\n);
}
int comparebynumber(const void* e1, const void* e2)
{
const student *p1 = (const student*) e1;
const student *p2 = (const student*) e2;
double diff = p1-number - p2-number;
if( diff0 )
{
return 1;
}
else if( diff0 )
{
return -1;
}
else
{
return 0;
}
}
int comparebyname(const void* e1, const void* e2)
{
const student *p1 = (const student*) e1;
const student *p2 = (const student*) e2;
return strcmp(p1-name, p2-name);
}
void findstudent(student *std, student **arr, int *pcurcnt , int totalcnt )
{
int number;
student *p = null;
int i, j, k;
int findmenu =0;
printf(찾을 학생의 학번을 입력하세요 : );
scanf(%d, &number);
for(i=0; i*pcurcnt; i++)
{
if(number == std[i].number)
{
p = &std[i];
break;
}
}
if( p == null )
{
printf(해당 학생 정보를 찾을 수 없습니다.\n);
return;
}
printf(%d %s\n, p-number, p-name);
while(1)
{
findmenu = selectfindmenu();
if( findmenu == previous_menu )
{
break;
}
switch( findmenu )
{
case modify_student:
printf(\n학번과 이름을 입력하세요 :\n);
scanf(%d %s, &p-number, &p-name);
break;
case delete_student:
printf(%d\n, sizeof(std)); //이코드는 std사이즈 변화좀 볼려고 그냥 넣어본겁니다.
for(j=i+1; j*pcurcnt; j++)
{
std[j-1].number = std[j].number;
strcpy(std[j-1].name, std[j].name);
}
(*pcurcnt)--;
if(totalcnt - (*pcurcnt) == 5) // 이부분이 free해주는 부분입니다.5개 이상 비어있으면 문장을 실행하게 됩니다.
{
for(k=(*pcurcnt)+1; ktotalcnt; k++)//전 이렇게 하면 소멸은 될거라 생각했는데실행오류가 납니다...
{ // 오류없이 실행됬다는 가정하에 totalcnt가 줄어야된다는건 알고 있습니다.
*arr = &std[k];
free(arr[k]);
}
}
printf(%d\n, sizeof(std)); //이코드는 std사이즈 변화좀 볼려고 그냥 넣어본겁니다.
break;
}
}
}
student* expandstdarray(student *std, int *ptotalcnt)
{
student *temp = (student*)realloc(std, sizeof(student) * ((*ptotalcnt) + 3));
if( temp == null )
{
return null;
}
*ptotalcnt = *ptotalcnt + 3;
return temp;
}
/* menu.h */
#ifndef menu_h
#define menu_h
int selectmenu(void);
int selectfindmenu(void);
#define add_student 1
#define list_by_num 2
#define list_by_name 3
#define find_student 4
#define quit_program 0
#define modify_student 1
#define delete_student 2
#define previous_menu 0
#endif
/* student.h */
#ifndef student_h
#define student_h
typedef struct student {
int number;
char name[20];
}student;
int addstudentinfo(student *std, int totalcnt, int *pcurcnt);
void listbynumber(student *std, int cnt);
void listbyname(student *std, int cnt);
void printstdlist(const student *std, int cnt);
int comparebynumber(const void* e1, const void* e2);
int comparebyname(const void* e1, const void* e2);
void findstudent(student *std, student **arr, int *pcurcnt, int totalcnt);
student* expandstdarray(student *std, int *ptotalcnt);
#endif
연결리스트로 구현중 이었습니다...
안되고 있는부분 주석달겠습니다.