parent
403b71a214
commit
06d00f2933
@ -0,0 +1,330 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// 定义学生结构体
|
||||
struct Student {
|
||||
char *id;
|
||||
char *clas;
|
||||
char *name;
|
||||
double score1;
|
||||
double score2;
|
||||
double score3;
|
||||
double score;
|
||||
};
|
||||
|
||||
// 函数声明
|
||||
void displayMenu();
|
||||
void inputStudentInfo(struct Student students[], int *count);
|
||||
void outputStudentInfo(struct Student students[], int count);
|
||||
void orderStudentInfo(struct Student students[], int count);
|
||||
void insertStudentInfo(struct Student students[], int *count);
|
||||
void deleteStudentInfo(struct Student students[], int *count);
|
||||
void modifyStudentInfo(struct Student students[], int count);
|
||||
void searchStudentInfo(struct Student students[], int count);
|
||||
|
||||
int main() {
|
||||
struct Student students[100];
|
||||
int count = 0;
|
||||
int option;
|
||||
|
||||
do {
|
||||
displayMenu();
|
||||
scanf("%d", &option);
|
||||
|
||||
switch (option) {
|
||||
case 1:
|
||||
inputStudentInfo(students, &count);
|
||||
break;
|
||||
case 2:
|
||||
outputStudentInfo(students, count);
|
||||
break;
|
||||
case 3:
|
||||
orderStudentInfo(students, count);
|
||||
break;
|
||||
case 4:
|
||||
insertStudentInfo(students, &count);
|
||||
break;
|
||||
case 5:
|
||||
deleteStudentInfo(students, &count);
|
||||
break;
|
||||
case 6:
|
||||
modifyStudentInfo(students, count);
|
||||
break;
|
||||
case 7:
|
||||
searchStudentInfo(students, count);
|
||||
break;
|
||||
case 8:
|
||||
printf("Exiting the program.\n");
|
||||
break;
|
||||
default:
|
||||
printf("Invalid option. Please try again.\n");
|
||||
}
|
||||
} while (option!= 8);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 显示菜单
|
||||
void displayMenu() {
|
||||
printf(" 1. Input\n");
|
||||
printf(" 2. Output\n");
|
||||
printf(" 3. Order\n");
|
||||
printf(" 4. Insert\n");
|
||||
printf(" 5. Delete\n");
|
||||
printf(" 6. Modify\n");
|
||||
printf(" 7. Search\n");
|
||||
printf(" 8. Quit\n");
|
||||
printf("Please enter your option: ");
|
||||
}
|
||||
|
||||
// 录入学生信息
|
||||
void inputStudentInfo(struct Student students[], int *count) {
|
||||
for (int i = *count; i < *count + 3; i++) {
|
||||
students[i].id = (char *)malloc(sizeof(char) * 10);
|
||||
students[i].clas = (char *)malloc(sizeof(char) * 10);
|
||||
students[i].name = (char *)malloc(sizeof(char) * 20);
|
||||
|
||||
printf("Please input student %d's id: ", i + 1);
|
||||
scanf("%s", students[i].id);
|
||||
|
||||
printf("Please input student %d's class: ", i + 1);
|
||||
scanf("%s", students[i].clas);
|
||||
|
||||
printf("Please input student %d's name: ", i + 1);
|
||||
scanf("%s", students[i].name);
|
||||
|
||||
printf("Please input student %d's score1: ", i + 1);
|
||||
scanf("%lf", &students[i].score1);
|
||||
|
||||
printf("Please input student %d's score2: ", i + 1);
|
||||
scanf("%lf", &students[i].score2);
|
||||
|
||||
printf("Please input student %d's score3: ", i + 1);
|
||||
scanf("%lf", &students[i].score3);
|
||||
|
||||
students[i].score = students[i].score1 + students[i].score2 + students[i].score3;
|
||||
}
|
||||
*count += 3;
|
||||
}
|
||||
|
||||
// 输出学生信息
|
||||
void outputStudentInfo(struct Student students[], int count) {
|
||||
for (int i = 0; i < count; 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 orderStudentInfo(struct Student students[], int count) {
|
||||
for (int i = 0; i < count - 1; i++) {
|
||||
for (int j = 0; j < count - i - 1; j++) {
|
||||
if (strcmp(students[j].clas, students[j + 1].clas) > 0 ||
|
||||
(strcmp(students[j].clas, students[j + 1].clas) == 0 && students[j].score < students[j + 1].score)) {
|
||||
struct Student temp = students[j];
|
||||
students[j] = students[j + 1];
|
||||
students[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
outputStudentInfo(students, count);
|
||||
}
|
||||
|
||||
// 插入学生信息
|
||||
void insertStudentInfo(struct Student students[], int *count) {
|
||||
struct Student newStudent;
|
||||
newStudent.id = (char *)malloc(sizeof(char) * 10);
|
||||
newStudent.clas = (char *)malloc(sizeof(char) * 10);
|
||||
newStudent.name = (char *)malloc(sizeof(char) * 20);
|
||||
|
||||
printf("Please input the new student's id: ");
|
||||
scanf("%s", newStudent.id);
|
||||
|
||||
printf("Please input the new student's class: ");
|
||||
scanf("%s", newStudent.clas);
|
||||
|
||||
printf("Please input the new student's name: ");
|
||||
scanf("%s", newStudent.name);
|
||||
|
||||
printf("Please input the new student's score1: ");
|
||||
scanf("%lf", &newStudent.score1);
|
||||
|
||||
printf("Please input the new student's score2: ");
|
||||
scanf("%lf", &newStudent.score2);
|
||||
|
||||
printf("Please input the new student's score3: ");
|
||||
scanf("%lf", &newStudent.score3);
|
||||
|
||||
newStudent.score = newStudent.score1 + newStudent.score2 + newStudent.score3;
|
||||
|
||||
int i;
|
||||
for (i = 0; i < *count; i++) {
|
||||
if (strcmp(students[i].clas, newStudent.clas) > 0 ||
|
||||
(strcmp(students[i].clas, newStudent.clas) == 0 && students[i].score < newStudent.score)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for (int j = *count; j > i; j--) {
|
||||
students[j] = students[j - 1];
|
||||
}
|
||||
|
||||
students[i] = newStudent;
|
||||
(*count)++;
|
||||
outputStudentInfo(students, *count);
|
||||
}
|
||||
|
||||
// 删除学生信息
|
||||
void deleteStudentInfo(struct Student students[], int *count) {
|
||||
char idOrName[20];
|
||||
printf("Please input the id or name of the student to delete: ");
|
||||
scanf("%s", idOrName);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < *count; i++) {
|
||||
if (strcmp(students[i].id, idOrName) == 0 || strcmp(students[i].name, idOrName) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == *count) {
|
||||
printf("Student not found.\n");
|
||||
} else {
|
||||
for (int j = i; j < *count - 1; j++) {
|
||||
students[j] = students[j + 1];
|
||||
}
|
||||
(*count)--;
|
||||
outputStudentInfo(students, *count);
|
||||
}
|
||||
}
|
||||
|
||||
// 修改学生信息
|
||||
void modifyStudentInfo(struct Student students[], int count) {
|
||||
char id[10];
|
||||
printf("Please input the id of the student to modify: ");
|
||||
scanf("%s", id);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < count; i++) {
|
||||
if (strcmp(students[i].id, id) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (i == count) {
|
||||
printf("Student not found.\n");
|
||||
} else {
|
||||
free(students[i].id);
|
||||
free(students[i].clas);
|
||||
free(students[i].name);
|
||||
|
||||
students[i].id = (char *)malloc(sizeof(char) * 10);
|
||||
students[i].clas = (char *)malloc(sizeof(char) * 10);
|
||||
students[i].name = (char *)malloc(sizeof(char) * 20);
|
||||
|
||||
printf("Please input the new class: ");
|
||||
scanf("%s", students[i].clas);
|
||||
|
||||
printf("Please input the new name: ");
|
||||
scanf("%s", students[i].name);
|
||||
|
||||
printf("Please input the new score1: ");
|
||||
scanf("%lf", &students[i].score1);
|
||||
|
||||
printf("Please input the new score2: ");
|
||||
scanf("%lf", &students[i].score2);
|
||||
|
||||
printf("Please input the new score3: ");
|
||||
scanf("%lf", &students[i].score3);
|
||||
|
||||
students[i].score = students[i].score1 + students[i].score2 + students[i].score3;
|
||||
|
||||
orderStudentInfo(students, count);
|
||||
}
|
||||
}
|
||||
|
||||
// 查询学生信息
|
||||
void searchStudentInfo(struct Student students[], int count) {
|
||||
int option;
|
||||
printf("Please select the search option:\n");
|
||||
printf("1. Search by class range\n");
|
||||
printf("2. Search by id range\n");
|
||||
printf("3. Search by name prefix\n");
|
||||
printf("4. Search by total score\n");
|
||||
printf("5. Search by class and id range\n");
|
||||
scanf("%d", &option);
|
||||
|
||||
switch (option) {
|
||||
case 1: {
|
||||
int startClass, endClass;
|
||||
printf("Please input the start class and end class (e.g., 11 13): ");
|
||||
scanf("%d %d", &startClass, &endClass);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
int classValue = atoi(students[i].clas);
|
||||
if (classValue >= startClass && classValue <= endClass) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 2: {
|
||||
char startId[10], endId[10];
|
||||
printf("Please input the start id and end id (e.g., 10001 10004): ");
|
||||
scanf("%s %s", startId, endId);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (strcmp(students[i].id, startId) >= 0 && strcmp(students[i].id, endId) <= 0) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
char prefix[10];
|
||||
printf("Please input the name prefix (e.g., Zh): ");
|
||||
scanf("%s", prefix);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (strncmp(students[i].name, prefix, strlen(prefix)) == 0) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
double minScore;
|
||||
printf("Please input the minimum total score: ");
|
||||
scanf("%lf", &minScore);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (students[i].score >= minScore) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 5: {
|
||||
int classValue;
|
||||
char startId[10], endId[10];
|
||||
printf("Please input the class and the start id and end id (e.g., 11 10001 10004): ");
|
||||
scanf("%d %s %s", &classValue, startId, endId);
|
||||
|
||||
for (int i = 0; i < count; i++) {
|
||||
if (atoi(students[i].clas) == classValue &&
|
||||
strcmp(students[i].id, startId) >= 0 && strcmp(students[i].id, endId) <= 0) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
printf("Invalid search option.\n");
|
||||
}
|
||||
}
|
Loading…
Reference in new issue