ADD file via upload

main
ptslgaequ 1 week ago
parent 663a9f2e41
commit 630aed531a

@ -0,0 +1,153 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学生结构体,使用字符串数组来存储学号、班级和姓名
struct Student {
char id[20];
char clas[20];
char name[20];
double score1;
double score2;
double score3;
double score; // 用于存储总成绩,方便后续排序等操作
};
// 交换两个学生结构体的函数
void swap(struct Student *a, struct Student *b) {
struct Student temp = *a;
*a = *b;
*b = temp;
}
// 计算学生总成绩的函数
void calculateScore(struct Student *s) {
s->score = s->score1 + s->score2 + s->score3;
}
// 录入学生信息的函数
void inputStudents(struct Student students[], int *num_students) {
char choice[10];
do {
struct Student new_student;
printf("Id ");
scanf("%s", new_student.id);
printf("class ");
scanf("%s", new_student.clas);
printf("name ");
scanf("%s", new_student.name);
printf("score1 ");
scanf("%lf", &new_student.score1);
printf("score2 ");
scanf("%lf", &new_student.score2);
printf("score3 ");
scanf("%lf", &new_student.score3);
calculateScore(&new_student);
students[*num_students] = new_student;
(*num_students)++;
printf("continue?\n");
scanf("%s", choice);
} while (strcmp(choice, "yes") == 0);
}
void outputStudents(struct Student students[], int num_students) {
for (int i = 0; i < num_students; i++) {
printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n", students[i].id, students[i].clas, students[i].name,
students[i].score1, students[i].score2, students[i].score3, students[i].score);
}
}
// 根据学号或姓名删除学生信息的函数
void deleteStudents(struct Student students[], int *num_students, char target[20]) {
int found = 0;
for (int i = 0; i < *num_students; i++) {
if (strcmp(students[i].id, target) == 0 || strcmp(students[i].name, target) == 0) {
found = 1;
for (int j = i; j < *num_students - 1; j++) {
students[j] = students[j + 1];
}
(*num_students)--;
i--; // 因为删除元素后,当前位置元素改变了,需要重新检查当前位置
}
}
if (!found) {
// 如果没找到要删除的学生,原样输出所有学生信息
outputStudents(students, *num_students);
} else {
outputStudents(students, *num_students);
}
}
// 根据学号或班级查询学生信息的函数
void selectStudents(struct Student students[], int num_students, char target[20]) {
int found = 0;
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, target) == 0 || strcmp(students[i].clas, target) == 0) {
found = 1;
printf("%s,%s,%s,%.1lf,%.1lf,%.1lf,%.1lf\n", students[i].id, students[i].clas, students[i].name,
students[i].score1, students[i].score2, students[i].score3, students[i].score);
}
}
if (!found) {
printf("there is no eligible student\n");
}
}
// 按照班级从小到大,同一班级内总成绩从大到小排序学生信息的函数
void orderStudents(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++) {
if (strcmp(students[j].clas, students[j + 1].clas) > 0) {
swap(&students[j], &students[j + 1]);
} else if (strcmp(students[j].clas, students[j + 1].clas) == 0) {
if (students[j].score < students[j + 1].score) {
swap(&students[j], &students[j + 1]);
}
}
}
}
}
// 输出学生信息的函数
int main() {
struct Student students[100]; // 假设最多存储100个学生信息可根据实际情况调整大小
int num_students = 0;
int option;
while (1) {
printf("1.input\n2.delete\n3.select\n4.order\n5.output\n6.quit\nplease input your option\n");
scanf("%d", &option);
switch (option) {
case 1:
inputStudents(students, &num_students);
break;
case 2: {
char target[20];
scanf("%s", target);
deleteStudents(students, &num_students, target);
}
break;
case 3: {
char target[20];
scanf("%s", target);
selectStudents(students, num_students, target);
}
break;
case 4:
orderStudents(students, num_students);
break;
case 5:
outputStudents(students, num_students);
break;
case 6:
return 0;
default:
printf("wuxiao\n");
}
}
return 0;
}
Loading…
Cancel
Save