2개의 데이터 파일을 읽어 이름을 오름차순으로 정렬하여 파일에 출력하는 프로그램을 작성하라. 반드시 연결리스트를 사용해야 한다.
...라는 문제입니다.
어떻게 코드를 짜긴 짰는데, 계속 막히네요.. 한수 가르쳐주시길 바랍니다.
#include <stdio.h>
#include <conio.h>
#include <process.h>
typedef struct student
{
int number;
char name[15];
int age;
char country[10];
char department[10];
struct student *next;
}STUDENT;
void concatenate (STUDENT *first, STUDENT *second);
void bubble (STUDENT *first);
void main()
{
STUDENT std1[20] = {{NULL, 0, NULL, NULL},};
STUDENT std2[20] = {{NULL, 0, NULL, NULL},};
FILE *fpin, *fpout;
int i=0;
if ((fpout = fopen ("c:d0801.dat", "r")) == NULL)
{
printf ("File open error..n");
getch();
exit (-1);
}
if ((fpout = fopen ("c:d0901.out", "w")) == NULL)
{
printf ("File open error..n");
getch();
exit (-1);
}
while (!feof (fpin))
{
fscanf (fpin, "%s", std1[i].name);
fscanf (fpin, "%d", &std1[i].age);
fscanf (fpin, "%s", std1[i].country);
fscanf (fpin, "%s", std1[i].department);
std1[i].next = &std1[i+1];
i++;
}
while (!feof (fpout))
{
fscanf (fpout, "%s", std2[i].name);
fscanf (fpout, "%d", &std2[i].age);
fscanf (fpout, "%s", std2[i].country);
fscanf (fpout, "%s", std2[i].department);
std2[i].next = &std2[i+1];
i++;
}
std1[i].next = NULL;
std2[i].next = NULL;
concatenate (std1, std2);
bubble (std1);
fclose (fpin);
fclose (fpout);
}
void concatenate (STUDENT *first, STUDENT *second)
{
if (first->next == NULL)
first->next = second;
else
concatenate (first->next, second);
}
void bubble (STUDENT *stptr)
{
int i, j;
char temp[20];
for (i=0; i<20; i++)
{
for (j=-1; j<20; j++)
{
if (stptr[j].name < stptr[j+1].name)
{
temp = stptr[j].name;
stptr[j].name = stptr[j+1].name; // 여기서 계속 막힙니다.
stptr[j+1].name = temp;
}
}
}
}
대충 보자면
1. 2개의 데이터파일을 읽으라고 했는데 하나밖에 안읽었고
2. 정렬하기도 전에 출력을 해버렸고
3. while문 2개 사이에 i=0으로 초기화가 빠졌고
4. 연결리스트를 사용하라고 했는데 버블소트할때 배열로 처리하셨네요