mergesort 를 사용하는 코드를 짰는데 ~ 안되네요~~
연블랙
mergesort 를 사용하는 코드를 짰는데 ~ 안되네요~~
student.txt 에서 파일 입력을 받아와서 mergesort 로 정렬하는 프로그램인데..
출력이 2개는 잘되는데 ~~ 2개가 안나오네요~ 답변 부탁드리겠습니다..
struct Student
{
int hakbun;
char name[20];
int score;
};
void input(struct Student *stu,int count);
void output(struct Student *stu,int count);
void Merge_Sort(struct Student *stu,struct Student *tmp, int start , int end);
void Merge(struct Student *stu , struct Student *tmp , int start , int mid , int end );
int main(void)
{
int count;
struct Student *stu;
struct Student *tmp;
FILE *file = fopen(student.txt, rt);
printf(hw3_1 : 최승용\n\n);
fscanf(file,%d,&count);
stu = (struct Student*)malloc(sizeof(stu) * count); // 동적 할당
tmp = (struct Student*)malloc(sizeof(tmp) * count); // 정렬 할때 사용할 배열 선언
input(stu,count);
printf(\n\n===정렬 이전\n);
output(stu,count);
Merge_Sort(stu,tmp,1,count);
printf(\n\n=== 정렬 결과\n);
output(stu,count);
return 0;
}
void input(struct Student *stu,int count) // 입력함수
{
int i,temp;
FILE *file = fopen(student.txt, rt);
fscanf(file,%d, &temp);
for(i=0 ; icount ; i++)
{
fscanf(file,%d, &stu[i].hakbun);
fscanf(file,%s, stu[i].name);
fscanf(file,%d, &stu[i].score);
}
}
void Merge_Sort(struct Student *stu,struct Student *tmp, int start , int end)
{
int half = 0;
if(start end)
{
half = ( start + end) / 2 ;
//printf( half 값은 %d ,half);
Merge_Sort(stu , tmp , start , half);
Merge_Sort(stu , tmp , half + 1 , end);
Merge(stu , tmp , start , half , end);
}
}
void Merge(struct Student *stu , struct Student *tmp , int start , int half , int end )
{
int i , j , t;
i = start ;
j = half + 1 ;
t = 0 ;
while(i = half && j = end)
{
if ( stu[i-1].score = stu[j-1].score)
{
tmp[t] = stu[i-1];
//tmp[t].score = stu[i-1].score;
i++;
t++;
}
else
{
tmp[t] = stu[j-1];
j++;
t++;
}
}
while ( i = half)
{
tmp[t] = stu[i-1];
i++;
t++;
}
while ( j = end )
{
tmp[t] = stu[j-1];
j++;
t++;
}
i=start;
t=0;
while(i = end)
{
stu[i-1] = tmp[t];
i++;
t++;
}
}
void output(struct Student *stu,int count) // 출력함수
{
int i;
printf(%4s %-10s %5s \n,
학번, 이름, 점수);
for(i=0; icount; i++)
{
printf(%4d %-10s %5d\n,
(stu+i)-hakbun,(stu+i)-name,(stu+i)-score);
}
}
번호 | 제 목 | 글쓴이 | 날짜 |
---|---|---|---|
2700562 | 함수포인터에서요 (7) | 소심한여자 | 2025-07-06 |
2700530 | 전처리문 질문입니다. (1) | 아놀드 | 2025-07-05 |
2700510 | c언어를 어케하면 잘할수 있을까요.. | 연연두 | 2025-07-05 |
2700484 | 두 개가 차이가 뭔지 알려주세요...(소수 찾는 프로그램) (2) | 날위해 | 2025-07-05 |
2700426 | 인터넷 창 띄우는 질문이요 (1) | 정훈 | 2025-07-04 |
2700400 | 원넓이를 계산이요 ㅜㅜ | 천칭자리 | 2025-07-04 |
2700368 | if에 관해서 질문이요... | Orange | 2025-07-04 |
2700339 | 이거 결과값이 왜이런건지.. (4) | 그댸와나 | 2025-07-04 |
2700313 | 파일 읽어서 저장하는데 빈파일일 경우 문재가 발생하네요.. (2) | 크나 | 2025-07-03 |
2700287 | 구조체 동적할당 연습을 하는데 오류가 뜹니다...(해결) (3) | 아련나래 | 2025-07-03 |
2700264 | 문자와 숫자 동시에 입력??? | 글고운 | 2025-07-03 |
2700236 | txt파일로만 쓰고 읽게 하려면 어떻게 해야 하나요..?? (8) | 미국녀 | 2025-07-03 |
2700211 | 전위 연산자 (2) | 어른처럼 | 2025-07-02 |
2700183 | C에서 파일이름을 받고, 그 파일의 사이즈를 출력해줘야하는데 내용이 출력이 안되네요 ;ㅅ; | 피스케스 | 2025-07-02 |
2700150 | 꼭좀 도와주세요ㅠㅠㅠ | 호습다 | 2025-07-02 |
2700095 | 연산문제...질문... | 오빤테앵겨 | 2025-07-01 |
2700070 | while문 , 3의배수 출력하는 프로그램좀 짜주세욤. | 횃불 | 2025-07-01 |
2700041 | 초보인데요 ㅎ 배열안에 배열을 집어넣을수 있나요?? | 헛장사 | 2025-07-01 |
2700012 | 배열// (1) | 전갈자리 | 2025-07-01 |
2699895 | 무한루프에 빠집니다.!! 해결좀부탁드려요 (10) | 선아 | 2025-06-30 |