본문 바로가기

IT

이건 뭘까

#include<stdio.h>

#define MAX_DATA_SIZE 100


typedef struct{

int number;

char name[20];

int avail;

} dat;


dat data[MAX_DATA_SIZE];

int first = 0 ;

int count = 0 ;


void dataoperation(char *cho); 

void sort(void);


int main(void)

{

char choice;


data[first].avail=1;


for(;;)

{

printf("i(삽입), d(삭제), f(탐색), r(전체 읽기), f(작업종료)를 선택하시오:");

scanf("%c",&choice);


if(choice=='q')  //작업 종료

return 0;


dataoperation(&choice);



}


}


void dataoperation(char *cho)

{

int i,j,num;

int front,now;

if(*cho=='i') // 삽입

do{

sort();

printf("삽입할 자료(학번, 이름)를 입력하시오:");

scanf("%d %s",&data[++count].number,&data[count].name);

}while(!feof(stdin));


else if(*cho=='d')  // 삭제

{

printf("삭제할 자료의 학번을 입력하시오:");

scanf("%d",&num);


for(i=1;i<=count;i++)

if(num==data[i].number) now = i;


if(data[0].avail == now) front=0;

for(i=1;i<=count;i++)

for(j=1;j<=count;j++)

if(now==data[i].avail) front = j;

data[front].avail = data[now].avail;

count--;


}

else if(*cho=='f')  // 탐색

{

printf("탐색할 자료의 학번을 입력하시오:");

scanf("%d",&num);

for(i=1;i<=count;i++)

if(num==data[i].number) 

printf("<학번, 이름> = <%d,%s>",num,data[i].name);

}


else if(*cho=='r')  //전체 읽기

{

printf("%d\n",count);

printf("<학번,이름> : ");

for(i=data[0].avail,j=1 ;j<count ;i=data[i].avail,j++)

{

printf("<%d,%s>, ",data[i].number,data[i].name);

}

printf("\n");

}

}


void sort(void)

{


int i,j,k;


if(count==2)

{

if(data[1].number<data[count].number)

data[1].avail=count;

else {

data[first].avail=count;

data[count].avail=1;

}

}


else if(count>2)

{

if(data[ data[0].avail ].number > data[count].number)

{

data[count].avail = data[ data[0].avail ].avail;

data[0].avail = count;

}


for(i=data[0].avail , j= data[ data[0].avail] .avail,k=3 ; k<=count ;k++, i = data[i].avail, j = data[j].avail)

{

if(data[i].number<=data[count].number&&data[count].number<=data[j].number)

{

data[i].avail=count;

data[count].avail=j;

}


if(k==count)

{

if(data[j].number<=data[count].number)

{

data[j].avail = count;

}


}


}


}




}

'IT' 카테고리의 다른 글

일단 저장  (0) 2014.05.14
몰라  (0) 2014.05.14
이게 뭐지  (0) 2014.05.14
뭔진 잘 모르겠다.  (0) 2014.05.14
힙소트 // 자세한 설명은 생략한다  (0) 2014.05.14