Work | Posted by 흑목 2007/03/29 17:59

TimeAttack

VI로 코딩하느라 무려 20분이나 걸렸다....

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define NAME_MAX 256

typedef struct _student
{
 char name[NAME_MAX];
 int id;
 int course_count;
 int course_time;
} STUDENT;

int main( void )
{
int student_count;
STUDENT *stdarray;
int i_for;
int course_max = 0;
int totaltime = 0;
int j_for;
STUDENT tempStudent;
FILE *fp;

printf( "학생 수를 입력하세요. : " );
scanf( "%d", &student_count );
fflush( stdin );

/* 학생 수 입력 */
if( student_count <= 0 )
{
 printf( "학생 수를 잘못 입력하였습니다." );
 return -1;
}

/* 학생 수 배열 설정 */
stdarray = ( STUDENT * )malloc( sizeof( STUDENT ) * student_count );

if( stdarray == NULL )
{
 printf( "시스템 에러입니다." );
 return -1;
}

/* 학생 정보 입력 시작 */
for( i_for = 0; i_for < student_count; i_for ++ )
{
 printf( "학생%d의 이름: ", i_for + 1 );
 fgets( stdarray[i_for].name, NAME_MAX, stdin );

 /* 개행 제거 */
 stdarray[i_for].name[strlen(stdarray[i_for].name) - 1] = '\0';
 
 printf( "학생%d의 학번: ", i_for + 1 );
 scanf( "%d", &stdarray[i_for].id );
 fflush( stdin );

 printf( "학생%d의 수강 과목 수: ", i_for + 1 );
 scanf( "%d", &stdarray[i_for].course_count );
 fflush( stdin );
 
 printf( "학생%d의 수강 과목 시간: ", i_for + 1 );
 scanf( "%d", &stdarray[i_for].course_time );
 fflush( stdin );
}

/*  학생 정보 입력 끝 */

/* 신청 과목 수가 가장 많은 학생 찾기 시작 */
for( i_for = 0; i_for < student_count; i_for ++ )
{
 if( course_max <= stdarray[i_for].course_count )
   course_max = stdarray[i_for].course_count;
}

/* 신청 과목 수가 가장 많은 학생 찾기 끝, 화면 출력 */
printf( "==신청 과목 수가 가장 많은 학생==\n" );
for( i_for = 0; i_for < student_count; i_for ++ )
{
 if( stdarray[i_for].course_count == course_max )
 {
  printf( "%s, %d, 과목수: %d, 과목당 시간: %d\n",
   stdarray[i_for].name, stdarray[i_for].id,
   stdarray[i_for].course_count, stdarray[i_for].course_time );
 }
}

/* 총 수강 시간 구하기 */
for( i_for = 0; i_for < student_count; i_for++ )
{
 totaltime += stdarray[i_for].course_count * stdarray[i_for].course_time;
}
printf( "==모든 학생들의 총 수강 시간 = %d\n", totaltime );

/* 파일 출력을 위한 내림차순 정렬 시작 */
for( i_for = 0; i_for < student_count; i_for++ )
{
 for( j_for = i_for + 1; j_for < student_count; j_for++ )
 {
  if( stdarray[i_for].course_count * stdarray[i_for].course_time <
   stdarray[j_for].course_count * stdarray[j_for].course_time )
  {
   /* swap */
   strcpy( tempStudent.name, stdarray[i_for].name );
   tempStudent.id = stdarray[i_for].id;
   tempStudent.course_count = stdarray[i_for].course_count;
   tempStudent.course_time = stdarray[i_for].course_time;

   strcpy( stdarray[i_for].name, stdarray[j_for].name );
   stdarray[i_for].id = stdarray[j_for].id;
   stdarray[i_for].course_count = stdarray[j_for].course_count;
   stdarray[i_for].course_time = stdarray[j_for].course_time;

   strcpy( stdarray[j_for].name, tempStudent.name );
   stdarray[j_for].id = tempStudent.id;
   stdarray[j_for].course_count = tempStudent.course_count;
   stdarray[j_for].course_time = tempStudent.course_time;
  }
 }
}

/* 파일 출력 시작 */
fp = fopen( "out.txt", "w" );

if( fp == NULL )
{
 printf( "출력 파일 열기 에러.\n" );
 return -1;
}

for( i_for = 0; i_for < student_count; i_for++ )
{
 fprintf( fp, "%s, %d, 과목수: %d, 과목당 시간: %d\n",
  stdarray[i_for].name, stdarray[i_for].id, stdarray[i_for].course_count,
  stdarray[i_for].course_time );
}

fclose( fp );

/* 배열 해제 */
free( stdarray );

return 0;
}

'Work' 카테고리의 다른 글

자원의 한계  (0) 2007/11/20
Visual Studio의 fflush 이야기  (1) 2007/11/20
TimeAttack  (0) 2007/03/29
Buffering Error  (0) 2006/05/18
Builder Listview Column Sorting  (0) 2006/03/31
Variant Conversion  (0) 2006/03/31