0% found this document useful (0 votes)
4 views4 pages

Studentsmanage

Uploaded by

ayanokoujikurea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Studentsmanage

Uploaded by

ayanokoujikurea
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <stdio.

h>
#include <string.h>

#define MAX_NAME 20
#define NUM_SUBJECTS 5
#define NUM_STUDENTS 5

typedef struct {
char name[MAX_NAME];
int age;
int scores[NUM_SUBJECTS];
} Student;
/*students = [
["조수겸", 18, [85, 90, 78, 88, 95]],
["김서준", 17, [92, 86, 98, 77, 89]],
["김래연", 18, [78, 84, 88, 92, 70]],
["우정훈", 17, [95, 91, 93, 89, 82]],
["황태현", 18, [67, 72, 75, 80, 75]]
]
*/
Student students[NUM_STUDENTS] = {
{"조수겸", 18, {85, 90, 78, 88, 95}},
{"김서준", 17, {92, 86, 98, 77, 89}},
{"김래연", 18, {78, 84, 88, 92, 70}},
{"우정훈", 17, {95, 91, 93, 89, 82}},
{"황태현", 18, {67, 72, 75, 80, 75}}
};

const char* subjects[NUM_SUBJECTS] = {"국어", "영어", "수학", "과학", "사회"};

char get_grade(int score);


void print_all_students();
void analyze_student();
void print_top_scores();
void print_top_three();
void clear_input_buffer();
/*
def get_grade(score):
if score >= 90:
return 'A'
elif score >= 80:
return 'B'
elif score >= 70:
return 'C'
elif score >= 60:
return 'D'
else:
return 'F'
*/
char get_grade(int score) {
if (score >= 90) return 'A';
else if (score >= 80) return 'B';
else if (score >= 70) return 'C';
else if (score >= 60) return 'D';
else return 'F';
}

void clear_input_buffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
//if choice == '1':
void print_all_students() {
for (int i = 0; i < NUM_STUDENTS; i++) {
printf("\n 이름: %s, 나이: %d\n", students[i].name, students[i].age);
for (int j = 0; j < NUM_SUBJECTS; j++) {
printf("%s: %d 점 (%c)\n",
subjects[j],
students[i].scores[j],
get_grade(students[i].scores[j]));
}
}
}
//elif choice == '2':
void analyze_student() {
char name[MAX_NAME];
int found = 0;

printf("분석할 학생의 이름을 입력하세요: ");


scanf("%s", name);
clear_input_buffer();

for (int i = 0; i < NUM_STUDENTS; i++) {


if (strcmp(students[i].name, name) == 0) {
int total = 0;
printf("\n%s 의 성적 분석:\n", name);

for (int j = 0; j < NUM_SUBJECTS; j++) {


total += students[i].scores[j];
}

float avg = (float)total / NUM_SUBJECTS;


printf("총점: %d 점\n", total);
printf("평균: %.2f 점\n", avg);
printf("과목별 성적:\n");

for (int j = 0; j < NUM_SUBJECTS; j++) {


printf("%s: %d 점 (%c)\n",
subjects[j],
students[i].scores[j],
get_grade(students[i].scores[j]));
}

found = 1;
break;
}
}

if (!found) {
printf("해당 이름의 학생을 찾을 수 없습니다.\n");
}
}
//elif choice == '3':
void print_top_scores() {
for (int i = 0; i < NUM_SUBJECTS; i++) {
int max_score = 0;

for (int j = 0; j < NUM_STUDENTS; j++) {


if (students[j].scores[i] > max_score) {
max_score = students[j].scores[i];
}
}

printf("%s 최고 점수: %d 점 (학생: ", subjects[i], max_score);


int first = 1;
for (int j = 0; j < NUM_STUDENTS; j++) {
if (students[j].scores[i] == max_score) {
if (!first) {
printf(", ");
}
printf("%s", students[j].name);
first = 0;
}
}
printf(")\n");
}
}
//elif choice ==' 4':
void print_top_three() {
struct {
char name[MAX_NAME];
float avg;
} avg_scores[NUM_STUDENTS];

for (int i = 0; i < NUM_STUDENTS; i++) {


int total = 0;
for (int j = 0; j < NUM_SUBJECTS; j++) {
total += students[i].scores[j];
}
strcpy(avg_scores[i].name, students[i].name);
avg_scores[i].avg = (float)total / NUM_SUBJECTS;
}

for (int i = 0; i < NUM_STUDENTS - 1; i++) {


for (int j = 0; j < NUM_STUDENTS - i - 1; j++) {
if (avg_scores[j].avg < avg_scores[j + 1].avg) {
float temp_avg = avg_scores[j].avg;
char temp_name[MAX_NAME];
strcpy(temp_name, avg_scores[j].name);

avg_scores[j].avg = avg_scores[j + 1].avg;


strcpy(avg_scores[j].name, avg_scores[j + 1].name);

avg_scores[j + 1].avg = temp_avg;


strcpy(avg_scores[j + 1].name, temp_name);
}
}
}

printf("전체 평균 상위 3 명:\n");
for (int i = 0; i < 3; i++) {
printf("%d. %s: %.2f 점\n", i + 1, avg_scores[i].name, avg_scores[i].avg);
}
}
//def main():
int main() {
int choice;
while (1) {
printf("\n1. 전체 학생 정보 출력\n");
printf("2. 특정 학생의 성적 분석\n");
printf("3. 과목별 최고 점수와 해당 학생\n");
printf("4. 전체 평균 상위 3 명\n");
printf("5. 종료\n");
printf("원하는 기능의 번호를 입력하세요: ");

if (scanf("%d", &choice) != 1) {
printf("잘못된 입력입니다. 다시 시도해주세요.\n");
clear_input_buffer();
continue;
}
clear_input_buffer();

switch (choice) {
case 1:
print_all_students();
break;
case 2:
analyze_student();
break;
case 3:
print_top_scores();
break;
case 4:
print_top_three();
break;
case 5:
printf("프로그램을 종료합니다.\n");
return 0;
default:
printf("잘못된 입력입니다. 다시 시도해주세요.\n");
}
}

return 0;
}

You might also like