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.

132 lines
3.9 KiB

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include<time.h>
#include "menu.h"
void insertStudentInfo(Student students[], int* count) {
if (*count >= MAX_STUDENTS) {
printf("The number of students is full and new students cannot be added.\n");
return;
}
Student newStudent;
int validInput;
do {
validInput = 1;
printf("Id: ");
scanf_s("%9s", newStudent.id, (unsigned)_countof(newStudent.id));
// 验证学号是否只由数字组成
for (int i = 0; i < strlen(newStudent.id); i++) {
if (!isdigit(newStudent.id[i])) {
validInput = 0;
break;
}
}
if (!validInput) {
printf("Student number can only be composed of numbers, please re-enter.\n");
}
} while (!validInput);
printf("Class: ");
scanf_s("%9s", newStudent.clas, (unsigned)_countof(newStudent.clas));
printf("Name: ");
scanf_s("%9s", newStudent.name, (unsigned)_countof(newStudent.name));
printf("Score1: ");
scanf_s("%lf", &newStudent.score1);
printf("Score2: ");
scanf_s("%lf", &newStudent.score2);
printf("Score3: ");
scanf_s("%lf", &newStudent.score3);
newStudent.score = newStudent.score1 + newStudent.score2 + newStudent.score3;
int i = *count;
while (i > 0 && (strcmp(students[i - 1].clas, newStudent.clas) > 0 ||
(strcmp(students[i - 1].clas, newStudent.clas) == 0 && students[i - 1].score < newStudent.score))) {
students[i] = students[i - 1];
i--;
}
students[i] = newStudent;
(*count)++;
}
void deleteStudentInfo(Student students[], int* count) {
char input[20];
printf("Enter the student ID or name to delete: ");
scanf_s("%19s", input, (unsigned)_countof(input));
int i = 0;
while (i < *count && (strcmp(students[i].id, input) != 0 && strcmp(students[i].name, input) != 0)) {
i++;
}
if (i == *count) {
printf("Student not found.\n");
}
else {
for (int j = i; j < *count - 1; j++) {
students[j] = students[j + 1];
}
(*count)--;
printf("Student deleted successfully.\n");
}
}
void modifyStudentInfo(Student students[], int count) {
char input[20];
printf("Enter the student ID to modify: ");
scanf_s("%19s", input, (unsigned)_countof(input));
int i = 0;
while (i < count && strcmp(students[i].id, input) != 0) {
i++;
}
if (i == count) {
printf("Student not found.\n");
}
else {
printf("Id: ");
scanf_s("%9s", students[i].id, (unsigned)_countof(students[i].id));
printf("Class: ");
scanf_s("%9s", students[i].clas, (unsigned)_countof(students[i].clas));
printf("Name: ");
scanf_s("%9s", students[i].name, (unsigned)_countof(students[i].name));
printf("Score1: ");
scanf_s("%lf", &students[i].score1);
printf("Score2: ");
scanf_s("%lf", &students[i].score2);
printf("Score3: ");
scanf_s("%lf", &students[i].score3);
students[i].score = students[i].score1 + students[i].score2 + students[i].score3; // Fixed here
printf("Student information updated successfully.\n");
}
}
void queryStudentInfo(Student students[], int count) {
char queryClass[MAX_STRING];
printf("Please enter the class you want to query:");
scanf_s("%9s", queryClass, (unsigned)_countof(queryClass));
printf("Query result:\n");
for (int i = 0; i < count; i++) {
if (strcmp(students[i].clas, queryClass) == 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);
}
}
}