C, C++/C

[C언어] 구조체 배열을 이용해서 성적을 입력받는 프로그램

SeongJo 2022. 5. 29. 23:28
반응형

안녕하세요 성조입니다.

오늘 코테보고 블로그 정리를 추가적으로 진행하려다 보니 이전에 학교 수업으로 제출했던 과제 파일을 찾아서 포스팅 및 C언어 간단한 복습을 진행했습니다.

이 코드는 Scanf_s를 사용하는 최신 버전이 아닌 구버전을 기준으로 작성된 코드입니다. 


 

구현 방향

C언어 구조체를 활용하여 값을 입력받고 총점과, 평균을 구하는 프로그램을 작성하라.

 

코드

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
 
typedef struct {
    char name[100];
    int student_id;
    int DB_score;
    int DS_score;
    int Java_score;
}student
 
int main() {
    int count = 1;
    int number_of_student;
    student student[5];
    printf("학생수: ");
    scanf("%d"&number_of_student);
 
    for (; count <= number_of_student; count++) {
    printf("\n[%d번째학생] \n\n", count);
    printf("학생이름입력: ");
    scanf("%s"&student[count].name);
    printf("학번입력:");
    scanf("%d"&student[count].student_id);
    printf("DB 점수입력:");
    scanf("%d"&student[count].DB_score);
    printf("DS 점수입력:");
    scanf("%d"&student[count].DS_score);
    printf("Java 점수입력:");
    scanf("%d"&student[count].Java_score);
    printf("\n\n");
    }
 
    for (count = 1; count <= number_of_student; count++){
    printf("\n");
    printf("이름 : %s \n", student[count].name);
    printf("학번 : %d \n", student[count].student_id);
    printf("DB 점수 : %d \n", student[count].DB_score);
    printf("DS 점수 : %d \n", student[count].DS_score);
    printf("Java 점수: %d \n", student[count].Java_score);
    printf("총점: %d \n", student[count].DB_score + student[count].DB_score + student[count].Java_score);
    printf("평균: %d \n", (student[count].DB_score + student[count].DB_score + student[count].Java_score)/3);
    }
}
 
 

풀이

구조체를 선언한다.

선언 후 첫번 째 반복문에서 입력받은 학생 수까지 반복하면서 입력받아낸다.

이후 다음 반복문에서 값을 모두 호출한다.

 

 


오타 또는 질문, 코드 에러 사항 등이 있으신 경우 댓글 부탁드리겠습니다!

추가적으로 이전 C언어, Python 코드들이 너무 정리가 잘 안되어있는 부분이 발견되면서 조금 더 보기 편하게 수정에 들어갔습니다!

다음 포스팅 때 뵙겠습니다!

감사합니다.

반응형