ADD file via upload

main
pui26hfw7 1 year ago
parent f406c235a5
commit 8790f2f3e5

@ -0,0 +1,114 @@
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
int id;
int class;
char name[20];
float score1;
float score2;
float score3;
};
void search(struct student students[], int num_students, int query);
void sort(struct student students[], int num_students);
void output(struct student students[], int num_students);
int main() {
struct student students[MAX_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}
};
int num_students = 7;
int query = 0;
printf("请输入查询要求:\n");
scanf("%d", &query);
search(students, num_students, query);
sort(students, num_students);
return 0;
}
void search(struct student students[], int num_students, int query) {
printf("查询结果如下:\n");
if (query == 1) {
int class_start, class_end;
scanf("%d-%d", &class_start, &class_end);
for (int k = 0; k < num_students; k++) {
struct student s = students[k];
if (s.class >= class_start && s.class <= class_end) {
printf("%d %d %s %.1f %.1f %.1f\n", s.id, s.class, s.name, s.score1, s.score2, s.score3);
}
}
}
else if (query == 2) {
int id_start, id_end;
scanf("%d-%d", &id_start, &id_end);
for (int k = 0; k < num_students; k++) {
struct student s = students[k];
if (s.id >= id_start && s.id <= id_end) {
printf("%d %d %s %.1f %.1f %.1f\n", s.id, s.class, s.name, s.score1, s.score2, s.score3);
}
}
}
else if (query == 3) {
char nam[20];
scanf("%s", nam);
for (int k = 0; k < num_students; k++) {
struct student s = students[k];
if (strstr(s.name, nam) == s.name) {
printf("%d %d %s %.1f %.1f %.1f\n", s.id, s.class, s.name, s.score1, s.score2, s.score3);
}
}
}
else if (query == 4) {
float score;
scanf("%f", &score);
for (int k = 0; k < num_students; k++) {
struct student s = students[k];
if (s.score1 + s.score2 + s.score3 >= score) {
printf("%d %d %s %.1f %.1f %.1f\n", s.id, s.class, s.name, s.score1, s.score2, s.score3);
}
}
}
}
void sort(struct student students[], int num_students) {
// 冒泡排序示例
for (int i = 0; i < num_students - 1; i++) {
for (int j = 0; j < num_students - i - 1; j++) {
// 计算总成绩
float total_j = students[j].score1 + students[j].score2 + students[j].score3;
float total_j1 = students[j + 1].score1 + students[j + 1].score2 + students[j + 1].score3;
if (total_j < total_j1) {
// 交换学生数据
struct student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
}
void output(struct student students[], int num_students) {
int i;
printf("学号\t班级\t姓名\t\t科目1\t科目2\t科目3\n");
for (i = 0; i < num_students; i++) {
struct student s = students[i];
printf("%d\t%d\t%-12s\t%.1f\t%.1f\t%.1f\n", s.id, s.class, s.name, s.score1, s.score2, s.score3);
}
}
Loading…
Cancel
Save