ADD file via upload

main
pk29n3fu4 2 days ago
parent ab62d64805
commit 39a7592385

@ -0,0 +1,78 @@
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
typedef struct {
int id;
int classs;
float scores[3];
float total_score;
int isnew;
} Student;
Student initial_students[] = {
{10001, 11, {99.5, 88.5, 89.5}, 277.5, 0},
{10002, 12, {77.9, 56.5, 87.5}, 221.9, 0},
{10003, 11, {92.5, 99.0, 60.5}, 252.0, 0}
};
Student* students;
int num_students = 3;
int compare(const void* a, const void* b) {
Student* student_a = (Student*)a;
Student* student_b = (Student*)b;
if (student_a->classs != student_b->classs) {
return student_a->classs - student_b->classs;
}
else {
return student_b->total_score - student_a->total_score;
}
}
int check_id_exists(int id) {
int i;for ( i = 0; i < num_students; i++) {
if (students[i].id == id) {
return 1;
}
}
return 0;
}
void insert_student(Student new_student) {
new_student.isnew = 1;
num_students++;
students = (Student*)realloc(students, num_students * sizeof(Student));
students[num_students - 1] = new_student;
qsort(students, num_students, sizeof(Student), compare);
}
void print_students() {
int i;for ( i = 0; i < num_students; i++) {
printf("%d %d %.1f %.1f %.1f", students[i].id, students[i].classs, students[i].scores[0], students[i].scores[1], students[i].scores[2]);
if (students[i].isnew) {
printf(" inserted");
}
printf("\n");
}
}
int main() {
students = (Student*)malloc(MAX_STUDENTS * sizeof(Student));
memcpy(students, initial_students, num_students * sizeof(Student));
Student new_student;
printf("请输入新学生的信息(学号 班级 成绩1 成绩2 成绩3\n");
scanf("%d %d %f %f %f", &new_student.id, &new_student.classs, &new_student.scores[0], &new_student.scores[1], &new_student.scores[2]);
new_student.total_score = new_student.scores[0] + new_student.scores[1] + new_student.scores[2];
if (check_id_exists(new_student.id)) {
printf("学号 %d 已存在,信息如下:\n", new_student.id);
int i;for ( i = 0; i < num_students; i++) {
if (students[i].id == new_student.id) {
printf("%d %d %.1f %.1f %.1f\n", students[i].id, students[i].classs, students[i].scores[0], students[i].scores[1], students[i].scores[2]);
break;
}
}
}
else {
insert_student(new_student);
print_students();
}
free(students);
return 0;
}
Loading…
Cancel
Save