You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
3.2 KiB
107 lines
3.2 KiB
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
struct Student {
|
|
int id;
|
|
int class;
|
|
char name[50];
|
|
float score1;
|
|
float score2;
|
|
float score3;
|
|
};
|
|
|
|
void search(struct Student students[], int num_students, char query[]);
|
|
int compare(const void *a, const void *b);
|
|
void swap(struct Student *a, struct Student *b);
|
|
void print_students(struct Student students[], int num_students);
|
|
|
|
int main() {
|
|
int num_students = 7;
|
|
struct 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}
|
|
};
|
|
|
|
printf("请输入查询要求:\n");
|
|
char query[100];
|
|
fgets(query, sizeof(query), stdin);
|
|
|
|
query[strcspn(query, "\n")] = '\0';
|
|
|
|
search(students, num_students, query);
|
|
|
|
return 0;
|
|
}
|
|
|
|
void search(struct Student students[], int num_students, char query[]) {
|
|
int type;
|
|
char value[50];
|
|
sscanf(query, "%d %s", &type, value);
|
|
|
|
struct Student result[100];
|
|
int result_count = 0;
|
|
|
|
for (int i = 0; i < num_students; i++) {
|
|
struct Student student = students[i];
|
|
|
|
switch (type) {
|
|
case 1:
|
|
if (student.class >= value[0] - '0' && student.class <= value[2] - '0') {
|
|
result[result_count++] = student;
|
|
}
|
|
break;
|
|
case 2:
|
|
if (student.id >= atoi(value) && student.id <= atoi(value + 6)) {
|
|
result[result_count++] = student;
|
|
}
|
|
break;
|
|
case 3:
|
|
if (strstr(student.name, value) == student.name) {
|
|
result[result_count++] = student;
|
|
}
|
|
break;
|
|
case 4:
|
|
if (student.score1 >= atof(value) || student.score2 >= atof(value) || student.score3 >= atof(value)) {
|
|
result[result_count++] = student;
|
|
}
|
|
break;
|
|
case 5:
|
|
if (student.class == value[0] - '0' && student.id >= atoi(value + 2) && student.id <= atoi(value + 8)) {
|
|
result[result_count++] = student;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
qsort(result, result_count, sizeof(struct Student), compare);
|
|
print_students(result, result_count);
|
|
}
|
|
|
|
int compare(const void *a, const void *b) {
|
|
struct Student *studentA = (struct Student *)a;
|
|
struct Student *studentB = (struct Student *)b;
|
|
|
|
if (studentA->class != studentB->class) {
|
|
return studentA->class - studentB->class;
|
|
}
|
|
|
|
return (int)(studentB->score1 + studentB->score2 + studentB->score3 - studentA->score1 - studentA->score2 - studentA->score3);
|
|
}
|
|
|
|
void swap(struct Student *a, struct Student *b) {
|
|
struct Student temp = *a;
|
|
*a = *b;
|
|
*b = temp;
|
|
}
|
|
|
|
void print_students(struct Student students[], int num_students) {
|
|
for (int i = 0; i < num_students; i++) {
|
|
printf("%d %d %s %.1f %.1f %.1f\n", students[i].id, students[i].class, students[i].name, students[i].score1, students[i].score2, students[i].score3);
|
|
}
|
|
}
|