티스토리 뷰

성적 처리 프로그램을 만듭니다.

학생 다섯 명의 국어, 영어, 수학 점수를 입력하여 총점, 평균, 학점을 구하고 총점 순으로 정렬하여 출력합니다.

학점은 평균이 90점 이상이면 A, 80점 이상이면 B, 70점 이상이면 C, 그 외는 F로 평가합니다.

 

실행결과는 다음과 같습니다.

/*
실행결과

학번 : 315
이름 : 홍길동
국어, 영어, 수학 점수 : 80 75 90
학번 : 316
이름 : 이순신
국어, 영어, 수학 점수 : 88 92 100
학번 : 317
이름 : 유관순
국어, 영어, 수학 점수 : 95 99 98
학번 : 318
이름 : 안중근
국어, 영어, 수학 점수 : 84 70 72
학번 : 319
이름 : 박신혜
국어, 영어, 수학 점수 : 60 65 40

# 정렬 전 데이터...
315 홍길동  80  75  90  245  81.7   B
316 이순신  88  92 100  280  93.3   A
317 유관순  95  99  98  292  97.3   A
318 안중근  84  70  72  226  75.3   C
319 박신혜  60  65  40  165  55.0   F

# 정렬 후 데이터...
317 유관순  95  99  98  292  97.3   A
316 이순신  88  92 100  280  93.3   A
315 홍길동  80  75  90  245  81.7   B
318 안중근  84  70  72  226  75.3   C
319 박신혜  60  65  40  165  55.0   F
*/

다음은 제가 작성한 코드입니다.

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

typedef struct
{
	double average;
	int number;
	int korean;
	int english;
	int math;
	int total;
	char grade;
	char name[20];
} Student;

void inputData(Student*);
void setTotalAndAverageAndGrade(Student*);
void sort(Student**);

void main(void)
{
	Student student[5];

	//학번, 이름, 과목 점수 입력
	inputData(student);

	//총점과 평균, 학점을 계산하여 저장
	setTotalAndAverageAndGrade(student);

	Student* pStudent[5];

	//포인터와 구조체 연결
	for (int i = 0; i < 5; i++)
	{
		pStudent[i] = &student[i];
	}

	//총점 순으로 정렬
	sort(pStudent);

	printf("# 정렬 전 데이터...\n");
	for (int i = 0; i < 5; i++)
	{
		printf("%5d", student[i].number);
		printf("%8s", student[i].name);
		printf("%5d", student[i].korean);
		printf("%5d", student[i].english);
		printf("%5d", student[i].math);
		printf("%5d", student[i].total);
		printf("%5.1lf", student[i].average);
		printf("%5c", student[i].grade);
		printf("\n");
	}
	printf("\n");

	printf("# 정렬 후 데이터...\n");
	for (int i = 0; i < 5; i++)
	{
		printf("%5d", pStudent[i]->number);
		printf("%8s", pStudent[i]->name);
		printf("%5d", pStudent[i]->korean);
		printf("%5d", pStudent[i]->english);
		printf("%5d", pStudent[i]->math);
		printf("%5d", pStudent[i]->total);
		printf("%5.1lf", pStudent[i]->average);
		printf("%5c", pStudent[i]->grade);
		printf("\n");
	}
}

void inputData(Student* pStudent)
{
	for (int i = 0; i < 5; i++)
	{
		printf("학번 : ");
		scanf("%d", &(pStudent[i].number));
		getchar();

		printf("이름 : ");
		fgets(pStudent[i].name, 20, stdin);
		pStudent[i].name[strlen(pStudent[i].name) - 1] = '\0';

		printf("국어, 영어, 수학 점수 : ");
		scanf("%d%d%d", &(pStudent[i].korean), &(pStudent[i].english), &(pStudent[i].math));
	}
}

void setTotalAndAverageAndGrade(Student* pStudent)
{
	for (int i = 0; i < 5; i++)
	{
		pStudent[i].total = 0;
		pStudent[i].total += pStudent[i].korean;
		pStudent[i].total += pStudent[i].english;
		pStudent[i].total += pStudent[i].math;
		pStudent[i].average = pStudent[i].total / 3.0;

		if (pStudent[i].average >= 70)
		{
			if (pStudent[i].average >= 80)
			{
				if (pStudent[i].average >= 90)
				{
					pStudent[i].grade = 'A';
				}
				else
				{
					pStudent[i].grade = 'B';
				}
			}
			else
			{
				pStudent[i].grade = 'C';
			}
		}
		else
		{
			pStudent[i].grade = 'F';
		}
	}
}

void sort(Student** ppStudent)
{
	int count = 4;
	while (count >= 1)
	{
		for (int i = 0; i < count; i++)
		{
			if (ppStudent[i]->total < ppStudent[i + 1]->total)
			{
				Student* temp;
				temp = ppStudent[i];
				ppStudent[i] = ppStudent[i + 1];
				ppStudent[i + 1] = temp;
			}
		}
		count--;
	}
}

/*
실행결과

학번 : 316
이름 : 이순신
국어, 영어, 수학 점수 : 88 92 100
학번 : 317
이름 : KOEY
국어, 영어, 수학 점수 : 95 99 98
학번 : 318
이름 : 유관순
국어, 영어, 수학 점수 : 84 70 72
학번 : 319
이름 : 박신혜
국어, 영어, 수학 점수 : 60 65 40
# 정렬 전 데이터...
  315  홍길동   80   75   90  245 81.7    B
  316  이순신   88   92  100  280 93.3    A
  317    KOEY   95   99   98  292 97.3    A
  318  유관순   84   70   72  226 75.3    C
  319  박신혜   60   65   40  165 55.0    F

# 정렬 후 데이터...
  317    KOEY   95   99   98  292 97.3    A
  316  이순신   88   92  100  280 93.3    A
  315  홍길동   80   75   90  245 81.7    B
  318  유관순   84   70   72  226 75.3    C
  319  박신혜   60   65   40  165 55.0    F
*/

실전 문제 풀이는 이것을 끝으로 다음 시간부터는 '파일 입출력'에 대해 배워보겠습니다.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함