C_C++

(C언어) 구조체를 이용한 성적처리 프로그램, 총점 평균 석차 구하기

고니자니 2024. 1. 10. 11:34
반응형

C언어에서 구조체를 이용해서 성적처리를 하는 프로그램입니다.

데이터를 키보드로 입력하도록 했으면

번호, 이름, 국어점수, 영어점수, 수학점수를 입력하면

총점, 평균,석차를 구해서 출력해 줍니다.

인원수는 아래와 같이 define으로 정의했으므로, 임으로 인원수를 변경해서 사용하면 좋습니다.

 #define  N    5

 

구조체는 다음과 같이 정의했습니다.

// Define the structure for a student
struct Student {
    int number;
    char name[20];
    int koreanScore;
    int englishScore;
    int mathScore;
    int totalScore;
    float averageScore;
    int rank;
};

 

 

C언어 코드는 다음과 같습니다.

#define _CRT_SECURE_NO_WARNINGS  // Visual Studio
#include <stdio.h>
#include <string.h>
#define N     5

// Define the structure for a student
struct Student {
    int number;
    char name[20];
    int koreanScore;
    int englishScore;
    int mathScore;
    int totalScore;
    float averageScore;
    int rank;
};

// Function to calculate total and average scores
void calculateScores(struct Student* students, int size) {
    for (int i = 0; i < size; i++) {
        students[i].totalScore = students[i].koreanScore + students[i].englishScore + students[i].mathScore;
        students[i].averageScore = (float)students[i].totalScore / 3;
    }
}

// Function to rank the students based on total scores
void rankStudents(struct Student* students, int size) {
    for (int i = 0; i < size; i++) {
        students[i].rank = 1;
        for (int j = 0; j < size; j++) {
            if (students[j].totalScore > students[i].totalScore) {
                students[i].rank++;
            }
        }
    }
}

// Function to display student information
void displayStudentInfo(struct Student student) {
    printf("%-5d%-20s%-10d%-10d%-10d%-10d%-15.2f%-5d\n",
        student.number, student.name, student.koreanScore, student.englishScore,
        student.mathScore, student.totalScore, student.averageScore, student.rank);
}

int main() {
    // Define an array of N students
    struct Student students[N];

    // Input information for each student
    for (int i = 0; i < N; i++) {
        printf("Enter information for student %d:\n", i + 1);
        students[i].number = i + 1;
        printf("Name: ");
        scanf("%s", students[i].name);
        printf("Korean score: ");
        scanf("%d", &students[i].koreanScore);
        printf("English score: ");
        scanf("%d", &students[i].englishScore);
        printf("Math score: ");
        scanf("%d", &students[i].mathScore);
        printf("\n");
    }

    // Calculate total and average scores
    calculateScores(students, N);

    // Rank the students
    rankStudents(students, N);

    // Display the header for the table
    printf("\n%-5s%-20s%-10s%-10s%-10s%-10s%-15s%-5s\n",
        "번호", "이름", "국어", "영어", "수학", "총점", "평균", "석차");

    // Display information for each student
    for (int i = 0; i < N; i++) {
        displayStudentInfo(students[i]);
    }

    return 0;
}

 

실행하면, 먼저 인원수 만큼의 데이터를 입력해야 합니다.

(C언어) 구조체를 이용한 성적처리 프로그램, 총점 평균 석차 구하기

 

데이터가 입력되면 아래와 같은 결과를 출력합니다.

(C언어) 구조체를 이용한 성적처리 프로그램, 총점 평균 석차 구하기

 

728x90
반응형