ADD file via upload

main
plwhm8ti7 1 year ago
parent b98027ba9a
commit 18b0bdc22a

@ -0,0 +1,130 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生信息结构体
typedef struct {
int student_id;
int class_num;
char name[20];
float score1;
float score2;
float score3;
} Student;
// 定义全局的学生信息数组
Student students[] = {
{10001, 11, "Zhang", 99.5, 88.5, 89.5},
{10002, 12, "Yang", 77.9, 56.5, 87.5},
{10003, 11, "Liang", 92.5, 99.0, 60.5},
{10004, 11, "Cai", 89.6, 56.9, 90.5},
{10005, 14, "Fu", 55.6, 67.9, 98.9},
{10006, 12, "Mao", 22.1, 45.9, 99.2},
{10007, 13, "Zhan", 35.6, 67.9, 88.0}
};
// 查询函数
void search_students(char* query) {
int query_type;
char query_value[20];
sscanf(query, "%d %[^\n]", &query_type, query_value);
if (query_type == 1) {
int start_class, end_class;
sscanf(query_value, "%d-%d", &start_class, &end_class);
for (int i = 0; i < sizeof(students) / sizeof(students[0]); i++) {
if (students[i].class_num >= start_class && students[i].class_num <= end_class) {
printf("%d %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].class_num,
students[i].name, students[i].score1, students[i].score2, students[i].score3);
}
}
}
else if (query_type == 2) {
int start_student, end_student;
sscanf(query_value, "%d-%d", &start_student, &end_student);
for (int i = 0; i < sizeof(students) / sizeof(students[0]); i++) {
if (students[i].student_id >= start_student && students[i].student_id <= end_student) {
printf("%d %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].class_num,
students[i].name, students[i].score1, students[i].score2, students[i].score3);
}
}
}
else if (query_type == 3) {
char name_prefix[3];
strncpy(name_prefix, query_value, 2);
name_prefix[2] = '\0';
for (int i = 0; i < sizeof(students) / sizeof(students[0]); i++) {
if (strncmp(students[i].name, name_prefix, 2) == 0) {
printf("%d %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].class_num,
students[i].name, students[i].score1, students[i].score2, students[i].score3);
}
}
}
else if (query_type == 4) {
float min_score;
sscanf(query_value, "%f", &min_score);
for (int i = 0; i < sizeof(students) / sizeof(students[0]); i++) {
if (students[i].score1 + students[i].score2 + students[i].score3 >= min_score) {
printf("%d %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].class_num,
students[i].name, students[i].score1, students[i].score2, students[i].score3);
}
}
}
else if (query_type == 5) {
int class_num, start_student, end_student;
sscanf(query_value, "%d.%d-%d", &class_num, &start_student, &end_student);
for (int i = 0; i < sizeof(students) / sizeof(students[0]); i++) {
if (students[i].class_num == class_num && students[i].student_id >= start_student
&& students[i].student_id <= end_student) {
printf("%d %d %s %.1f %.1f %.1f\n", students[i].student_id, students[i].class_num,
students[i].name, students[i].score1, students[i].score2, students[i].score3);
}
}
}
}
// 排序函数
int compare_students(const void* a, const void* b) {
Student* student_a = (Student*)a;
Student* student_b = (Student*)b;
if (student_a->class_num < student_b->class_num) {
return -1;
}
else if (student_a->class_num > student_b->class_num) {
return 1;
}
else {
float total_score_a = student_a->score1 + student_a->score2 + student_a->score3;
float total_score_b = student_b->score1 + student_b->score2 + student_b->score3;
if (total_score_a > total_score_b) {
return -1;
}
else if (total_score_a < total_score_b) {
return 1;
}
else {
return 0;
}
}
}
void sort_students(Student* students, int num_students) {
qsort(students, num_students, sizeof(Student), compare_students);
}
// 主函数
int main() {
char query[50];
printf("请输入查询要求:");
fgets(query, sizeof(query), stdin);
query[strcspn(query, "\n")] = '\0';
search_students(query);
sort_students(students, sizeof(students) / sizeof(students[0]));
return 0;
}
Loading…
Cancel
Save