#include
#include
#include
#define MAX 255
typedef struct _std{
int no,kor,eng,math;
char name[MAX];
float avr;
struct _std * next;
}std;
int main()
{
std *head = NULL, *cur, *newstd, ** ptr, *temp, *del_ptr, *find_ptr,*pre_ptr;
int kno,count, i, j,del_no,find_no;
float kko,ken,kma;
char str[MAX],tmp;
FILE *wfp;
while(1)
{
system("cls");
printf("<< MENU >>n");
printf("1.데이터 입력n");
printf("2.데이터 보기n");
printf("3.정렬하기n");
printf("4.파일출력n");
printf("5.데이터 찾기n");
printf("6.데이터 선택 삭제n");
printf("7.모든 데이터 삭제n");
printf("8.종료n");
printf("nSelect Menu : ");
switch(getchar()){
case '1':
system("cls");
newstd = (std *)malloc(sizeof(std));
printf("학번 : "); scanf("%d", &kno); fflush(stdin);
printf("이름 : "); gets(str);
printf("국어 : "); i=0;
// 점수 0~100점, 문자입력시, 소수점 입력시 예외처리
while(i<1){
if(scanf("%f", &kko) == 0) {printf("문자를 입력하셨습니다 : ");fflush(stdin);}
else if(kko100) printf("0이상 100이하의 숫자를 입력해주세요 : ");
else if(kko-(int)kko > 0) printf("자연수를 입력하시오 : ");
else i++;}
printf("영어 : "); i=0;
// 점수 0~100점, 문자입력시, 소수점 입력시 예외처리
while(i<1){
if(scanf("%f", &ken) == 0) {printf("문자를 입력하셨습니다 : ");fflush(stdin);}
else if(ken100) printf("0이상 100이하의 숫자를 입력해주세요 : ");
else if(ken-(int)ken > 0) printf("자연수를 입력하시오 : ");
else i++;}
printf("수학 : "); i=0;
// 점수 0~100점, 문자입력시, 소수점 입력시 예외처리
while(i<1){
if(scanf("%f", &kma) == 0){printf("문자를 입력하셨습니다 : ");fflush(stdin);}
else if(kma100) printf("0이상 100이하의 숫자를 입력해주세요 : ");
else if(kma-(int)kma > 0) printf("자연수를 입력하시오 : ");
else i++;}
newstd->no = kno;
newstd->eng = (int)ken;
newstd->kor = (int)kko;
newstd->math = (int)kma;
newstd->avr = (float)(ken+kko+kma)/3;
strcpy(newstd->name, str);
newstd->next = NULL;
if(head == NULL)
head = newstd;
else{
cur = head;
while(cur->next != NULL)
cur = cur->next;
cur->next = newstd;}
system("pause"); break;
case '2':
system("cls");
if(head == NULL)
printf(" 입력 정보가 없습니다.");
else{
cur = head;
while(cur != NULL){
printf("이름 : %sn",cur->name);
printf("학번 : %dn",cur->no);
printf("국어 : %dn",cur->kor);
printf("영어 : %dn",cur->eng);
printf("수학 : %dn",cur->math);
printf("평균 : %fn",cur->avr);
printf("n");
cur = cur->next;}}
printf("n");
system("pause"); break;
case '3':
system("cls");
cur = head;
count = 0;
tmp=0;
if(head==NULL){
printf("데이터가 없습니다n");
system("pause");
break;
}
else{
printf("SELECT MENUn");
printf("1.학번으로 정렬하기n2.평균평으로 정렬하기n3.나가기n");
scanf("%s",&tmp);
while(1){
if(tmp=='1'){
system("cls");
if(head == NULL){
printf("errorn");
break;}
while(cur != NULL){
++count;
cur = cur->next;}
ptr = (std **)malloc(sizeof(std *) * count);
for(i = 0, cur = head; i < count; cur = cur->next)
ptr[i++] = cur;
for(i = 0; i < count-1; i++){
for(j = i+1; j < count; j++){
if(ptr[i]->no > ptr[j]->no){
temp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=temp;}}}
head = ptr[0];
for(i = 1, cur = head; cur && i < count; cur = cur->next)
cur->next = ptr[i++];
cur->next = NULL;
free(ptr);
break;
}
else if(tmp=='2'){
system("cls");
if(head == NULL){
printf("errorn");
break;}
while(cur != NULL){
++count;
cur = cur->next;}
ptr = (std **)malloc(sizeof(std *) * count);
for(i = 0, cur = head; i < count; cur = cur->next)
ptr[i++] = cur;
for(i = 0; i < count-1; i++){
for(j = i+1; j < count; j++){
if(ptr[i]->avr < ptr[j]->avr){
temp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=temp;}}}
head = ptr[0];
for(i = 1, cur = head; cur && i < count; cur = cur->next)
cur->next = ptr[i++];
cur->next = NULL;
free(ptr);
break;
}
else if(tmp=='3')
break;
else
break;
}
if(tmp=='3')
break;
else{
cur = head;
while(cur != NULL){
printf("이름 : %sn",cur->name);
printf("학번 : %dn",cur->no);
printf("국어 : %dn",cur->kor);
printf("영어 : %dn",cur->eng);
printf("수학 : %dn",cur->math);
printf("평균 : %fn",cur->avr);
printf("n");
cur = cur->next;}}
printf("n");
system("pause"); break;
}
case '4':
system("cls");
tmp=0;
if(head==NULL){
printf("데이터가 없습니다n");
system("pause");
break;
}
else{
printf("SELECT MENUn");
printf("1.학번으로 출력하기n2.평균으로 출력하기n3.나가기n");
scanf("%s",&tmp);
while(1){
if(tmp=='1'){
system("cls");
if(head == NULL){
printf("errorn");
break;}
while(cur != NULL){
++count;
cur = cur->next;}
ptr = (std **)malloc(sizeof(std *) * count);
for(i = 0, cur = head; i < count; cur = cur->next)
ptr[i++] = cur;
for(i = 0; i < count-1; i++){
for(j = i+1; j < count; j++){
if(ptr[i]->no > ptr[j]->no){
temp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=temp;}}}
head = ptr[0];
for(i = 1, cur = head; cur && i < count; cur = cur->next)
cur->next = ptr[i++];
cur->next = NULL;
free(ptr);
break;
}
else if(tmp=='2'){
system("cls");
if(head == NULL){
printf("errorn");
break;}
while(cur != NULL){
++count;
cur = cur->next;}
ptr = (std **)malloc(sizeof(std *) * count);
for(i = 0, cur = head; i < count; cur = cur->next)
ptr[i++] = cur;
for(i = 0; i < count-1; i++){
for(j = i+1; j < count; j++){
if(ptr[i]->avr < ptr[j]->avr){
temp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=temp;}}}
head = ptr[0];
for(i = 1, cur = head; cur && i < count; cur = cur->next)
cur->next = ptr[i++];
cur->next = NULL;
free(ptr);
break;
}
else if(tmp=='3')
break;
else
break;
}
if(tmp=='3')
break;
}
wfp = fopen("output.txt","w");
if(head == NULL)
printf(" 입력 정보가 없습니다.");
else{
printf("output.txt로 저장되었습니다");
cur = head;
while(cur != NULL){
fprintf(wfp,"이름 : %sn",cur->name);
fprintf(wfp,"학번 : %dn",cur->no);
fprintf(wfp,"국어 : %dn",cur->kor);
fprintf(wfp,"영어 : %dn",cur->eng);
fprintf(wfp,"수학 : %dn",cur->math);
fprintf(wfp,"평균 : %fn",cur->avr);
fprintf(wfp,"n");
cur = cur->next;}}
fclose(wfp);
printf("n");
system("pause"); break;
/*********************************학번으로 검색하기************************************/
case '5':
system("cls");
tmp=0;
if(head == NULL)
printf(" 입력 정보가 없습니다.");
else{
printf("SELECT MENUn");
printf("1.학번으로 삭제하기netc.나가기n");
scanf("%s",&tmp);
system("cls");
if(tmp=='1'){
while(1){
cur = head;
printf("찾을 데이터의 학번을 입력하시오 : ");
scanf("%d",&find_no);
while(cur != NULL){
if(cur->no==find_no){
printf("이름 : %sn",cur->name);
printf("학번 : %dn",cur->no);
printf("국어 : %dn",cur->kor);
printf("영어 : %dn",cur->eng);
printf("수학 : %dn",cur->math);
printf("평균 : %fn",cur->avr);
printf("n");
}
cur = cur->next;
}
printf("n");
break;
}
}
else;
}
system("pause"); break;
/*********************************데이터 선택 삭제*************************************/
case '6':
system("cls");
tmp=0;
if(head == NULL)
printf(" 입력 정보가 없습니다.");
else{
printf("SELECT MENUn");
printf("1.학번으로 삭제하기netc.나가기n");
scanf("%s",&tmp);
system("cls");
if(tmp=='1'){
while(1){
if(head->next==NULL){
cur = head;
printf("삭제할 데이터의 학번을 입력하시오 : ");
scanf("%d",&del_no);
if(head->no==del_no){
cur=head->next;
free(head);
head=cur;
}
}
else{
cur = head;
printf("삭제할 데이터의 학번을 입력하시오 : ");
scanf("%d",&del_no);
while(cur != NULL){
if(head->no==del_no){
cur=head->next;
free(head);
head=cur;
}
else{
if(cur->no==del_no){
pre_ptr=head;
del_ptr=cur;
while(pre_ptr->next!=cur)
pre_ptr=pre_ptr->next;
pre_ptr->next=cur->next;
free(del_ptr);
cur=pre_ptr;
}
}
cur=cur->next;
}
}
printf("n");
break;
}
}
else ;
}
system("pause"); break;
/*************************************************************************************/
case '7':
system("cls");
find_ptr=head;
while(1){
if(head==NULL)
break;
else if(head->next==NULL){
free(head);
head=NULL;
break;}
else{
for(find_ptr=head;find_ptr!=NULL;find_ptr=find_ptr->next){
del_ptr=find_ptr->next;
find_ptr->next = find_ptr->next->next;
free(del_ptr);
head=NULL;
break;}}
}
system("pause"); break;
case '8':
system("cls");
find_ptr=head;
if(head==NULL)
exit(1);
else if(head->next==NULL){
free(head);
head=NULL;
exit(1);}
else{
for(find_ptr=head;find_ptr!=NULL;find_ptr=find_ptr->next){
del_ptr=find_ptr->next;
find_ptr->next = find_ptr->next->next;
free(del_ptr);
head=NULL;
exit(1);}
}
break;
}
}
}
관련