Compare commits
8 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
6dfcb9c304 | 1 year ago |
|
|
0323d40e92 | 1 year ago |
|
|
1f3206f374 | 1 year ago |
|
|
70c8d40628 | 1 year ago |
|
|
9e5ed30bad | 1 year ago |
|
|
4ccd64c5d6 | 1 year ago |
|
|
e126828031 | 1 year ago |
|
|
33fb2e37a6 | 1 year ago |
@ -0,0 +1,46 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// 函数声明
|
||||
void displayMenu();
|
||||
|
||||
int main() {
|
||||
char input; // 用于存储用户输入的字符
|
||||
|
||||
while (1) {
|
||||
displayMenu(); // 显示菜单
|
||||
|
||||
printf("Please enter your choice: ");
|
||||
scanf(" %c", &input); // 读取用户输入
|
||||
|
||||
switch (input) {
|
||||
case 'i':
|
||||
printf("You are trying to Input info\n");
|
||||
break;
|
||||
case 'o':
|
||||
printf("You are trying to Output info\n");
|
||||
break;
|
||||
case 'm':
|
||||
printf("You are trying to Make things ordered\n");
|
||||
break;
|
||||
case 'q':
|
||||
printf("You are about to Quit\n");
|
||||
return 0; // 退出程序
|
||||
default:
|
||||
printf("Wrong input\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 菜单显示函数
|
||||
void displayMenu() {
|
||||
printf("\n");
|
||||
printf(" 1.Input\n");
|
||||
printf(" 2.Output\n");
|
||||
printf(" 3.Order\n");
|
||||
printf(" 4.Quit\n");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
@ -0,0 +1,117 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_STUDENTS 4 // 最多 4 个学生(原有 3 个 + 新插入 1 个)
|
||||
|
||||
typedef struct {
|
||||
int id; // 学号
|
||||
int clas; // 班级
|
||||
double math; // 高数成绩
|
||||
double physics; // 大学物理成绩
|
||||
double english; // 英语成绩
|
||||
double total; // 总成绩
|
||||
} Student;
|
||||
|
||||
// 函数声明
|
||||
void displayStudents(const Student students[], int studentCount);
|
||||
void sortStudents(Student students[], int studentCount);
|
||||
void insertStudent(Student students[], int *studentCount);
|
||||
|
||||
int main() {
|
||||
// 初始化已有的三个学生信息
|
||||
Student students[MAX_STUDENTS] = {
|
||||
{10001, 11, 85.0, 88.0, 92.0, 85.0 + 88.0 + 92.0},
|
||||
{10002, 12, 75.0, 65.0, 70.0, 75.0 + 65.0 + 70.0},
|
||||
{10003, 11, 95.0, 98.0, 90.0, 95.0 + 98.0 + 90.0}
|
||||
};
|
||||
int studentCount = 3; // 初始已有 3 个学生
|
||||
|
||||
printf("Original student information:\n");
|
||||
displayStudents(students, studentCount);
|
||||
|
||||
// 插入一个新学生信息
|
||||
insertStudent(students, &studentCount);
|
||||
printf("\nStudent information after insertion:\n");
|
||||
displayStudents(students, studentCount);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 显示学生信息
|
||||
void displayStudents(const Student students[], int studentCount) {
|
||||
int i; // 循环变量声明移到外部
|
||||
printf(" ID Class Math Physics English Total\n");
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
printf("%05d %-7d%-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].clas,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
}
|
||||
}
|
||||
|
||||
// 按班级和总成绩排序学生信息
|
||||
void sortStudents(Student students[], int studentCount) {
|
||||
int i, j; // 循环变量声明移到外部
|
||||
for (i = 0; i < studentCount - 1; i++) {
|
||||
for (j = 0; j < studentCount - i - 1; j++) {
|
||||
if (students[j].clas > students[j + 1].clas ||
|
||||
(students[j].clas == students[j + 1].clas && students[j].total < students[j + 1].total)) {
|
||||
// 交换学生信息
|
||||
Student temp = students[j];
|
||||
students[j] = students[j + 1];
|
||||
students[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 插入学生信息
|
||||
void insertStudent(Student students[], int *studentCount) {
|
||||
if (*studentCount >= MAX_STUDENTS) {
|
||||
printf("Student list is full. Cannot insert more students.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
Student newStudent;
|
||||
int i; // 循环变量声明移到外部
|
||||
|
||||
printf("\nEnter information for the new student:\n");
|
||||
|
||||
// 输入学号
|
||||
printf("ID: ");
|
||||
scanf("%d", &newStudent.id);
|
||||
|
||||
// 检查学号是否重复
|
||||
for (i = 0; i < *studentCount; i++) {
|
||||
if (students[i].id == newStudent.id) {
|
||||
printf("Student with ID %d already exists. Insertion failed.\n", newStudent.id);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 输入班级和成绩
|
||||
printf("Class: ");
|
||||
scanf("%d", &newStudent.clas);
|
||||
printf("Math score: ");
|
||||
scanf("%lf", &newStudent.math);
|
||||
printf("Physics score: ");
|
||||
scanf("%lf", &newStudent.physics);
|
||||
printf("English score: ");
|
||||
scanf("%lf", &newStudent.english);
|
||||
|
||||
// 计算总成绩
|
||||
newStudent.total = newStudent.math + newStudent.physics + newStudent.english;
|
||||
|
||||
// 插入新学生
|
||||
students[*studentCount] = newStudent;
|
||||
(*studentCount)++;
|
||||
|
||||
// 重新排序
|
||||
sortStudents(students, *studentCount);
|
||||
|
||||
printf("New student inserted successfully.\n");
|
||||
}
|
||||
|
||||
@ -0,0 +1,91 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_STUDENTS 3 // 最多 3 个学生(初始已知)
|
||||
|
||||
typedef struct {
|
||||
int id; // 学号
|
||||
int clas; // 班级
|
||||
double math; // 高数成绩
|
||||
double physics; // 大学物理成绩
|
||||
double english; // 英语成绩
|
||||
double total; // 总成绩
|
||||
} Student;
|
||||
|
||||
// 函数声明
|
||||
void displayStudents(const Student students[], int studentCount);
|
||||
void deleteStudent(Student students[], int *studentCount);
|
||||
|
||||
int main() {
|
||||
// 初始化已知的三个学生信息
|
||||
Student students[MAX_STUDENTS] = {
|
||||
{10001, 11, 99.5, 88.5, 89.5, 99.5 + 88.5 + 89.5},
|
||||
{10003, 11, 92.5, 99.0, 60.5, 92.5 + 99.0 + 60.5},
|
||||
{10002, 12, 77.9, 56.5, 87.5, 77.9 + 56.5 + 87.5}
|
||||
};
|
||||
int studentCount = 3; // 当前学生数量
|
||||
|
||||
printf("Original student information:\n");
|
||||
displayStudents(students, studentCount);
|
||||
|
||||
// 删除一个学生信息
|
||||
deleteStudent(students, &studentCount);
|
||||
|
||||
printf("\nStudent information after deletion:\n");
|
||||
displayStudents(students, studentCount);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 显示学生信息
|
||||
void displayStudents(const Student students[], int studentCount) {
|
||||
int i; // 循环变量声明移到外部
|
||||
if (studentCount == 0) {
|
||||
printf("No student information available.\n");
|
||||
return;
|
||||
}
|
||||
printf(" ID Class Math Physics English Total\n");
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
printf("%05d %-7d%-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].clas,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除学生信息
|
||||
void deleteStudent(Student students[], int *studentCount) {
|
||||
int deleteId, i, j;
|
||||
int found = 0; // 标志是否找到学生信息
|
||||
|
||||
if (*studentCount == 0) {
|
||||
printf("No student information to delete.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
printf("\nEnter the ID of the student to delete: ");
|
||||
scanf("%d", &deleteId);
|
||||
|
||||
// 查找学生信息
|
||||
for (i = 0; i < *studentCount; i++) {
|
||||
if (students[i].id == deleteId) {
|
||||
found = 1;
|
||||
|
||||
// 删除学生,将后续元素前移
|
||||
for (j = i; j < *studentCount - 1; j++) {
|
||||
students[j] = students[j + 1];
|
||||
}
|
||||
(*studentCount)--; // 学生数量减少
|
||||
printf("Student with ID %d has been deleted successfully.\n", deleteId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
printf("Student with ID %d does not exist.\n", deleteId);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,147 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_STUDENTS 3 // 最多 3 个学生(初始已知)
|
||||
|
||||
typedef struct {
|
||||
int id; // 学号
|
||||
int clas; // 班级
|
||||
double math; // 高数成绩
|
||||
double physics; // 大学物理成绩
|
||||
double english; // 英语成绩
|
||||
double total; // 总成绩
|
||||
} Student;
|
||||
|
||||
// 函数声明
|
||||
void displayStudents(const Student students[], int studentCount);
|
||||
void modifyStudent(Student students[], int studentCount);
|
||||
void sortStudents(Student students[], int studentCount);
|
||||
|
||||
int main() {
|
||||
// 初始化已知的三个学生信息
|
||||
Student students[MAX_STUDENTS] = {
|
||||
{10001, 11, 99.5, 88.5, 89.5, 99.5 + 88.5 + 89.5},
|
||||
{10002, 12, 77.9, 56.5, 87.5, 77.9 + 56.5 + 87.5},
|
||||
{10003, 11, 92.5, 99.0, 60.5, 92.5 + 99.0 + 60.5}
|
||||
};
|
||||
int studentCount = 3; // 当前学生数量
|
||||
|
||||
printf("Original student information:\n");
|
||||
displayStudents(students, studentCount);
|
||||
|
||||
// 修改一个学生信息
|
||||
modifyStudent(students, studentCount);
|
||||
|
||||
printf("\nStudent information after modification:\n");
|
||||
displayStudents1(students, studentCount);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 显示学生信息
|
||||
void displayStudents(const Student students[], int studentCount) {
|
||||
int i; // 循环变量声明移到外部
|
||||
if (studentCount == 0) {
|
||||
printf("No student information available.\n");
|
||||
return;
|
||||
}
|
||||
printf(" ID Class Math Physics English Total\n");
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
printf("%05d %-7d%-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].clas,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
}
|
||||
}
|
||||
|
||||
// 修改学生信息
|
||||
void modifyStudent(Student students[], int studentCount) {
|
||||
int modifyId, i;
|
||||
int found = 0; // 标志是否找到学生信息
|
||||
|
||||
printf("\nEnter the ID of the student to modify: ");
|
||||
scanf("%d", &modifyId);
|
||||
|
||||
// 查找学生信息
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
if (students[i].id == modifyId) {
|
||||
found = 1;
|
||||
|
||||
printf("Enter new information for the student:\n");
|
||||
|
||||
// 输入新的班级
|
||||
printf("Class: ");
|
||||
scanf("%d", &students[i].clas);
|
||||
|
||||
// 输入新的成绩
|
||||
printf("Math score: ");
|
||||
scanf("%lf", &students[i].math);
|
||||
printf("Physics score: ");
|
||||
scanf("%lf", &students[i].physics);
|
||||
printf("English score: ");
|
||||
scanf("%lf", &students[i].english);
|
||||
|
||||
// 重新计算总成绩
|
||||
students[i].total = students[i].math + students[i].physics + students[i].english;
|
||||
|
||||
printf("Student with ID %d has been modified successfully.\n", modifyId);
|
||||
|
||||
// 重新排序数组
|
||||
sortStudents(students, studentCount);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
printf("Student with ID %d does not exist.\n", modifyId);
|
||||
}
|
||||
}
|
||||
|
||||
// 按班级和总成绩排序学生信息
|
||||
void sortStudents(Student students[], int studentCount) {
|
||||
int i, j; // 循环变量声明移到外部
|
||||
for (i = 0; i < studentCount - 1; i++) {
|
||||
for (j = 0; j < studentCount - i - 1; j++) {
|
||||
if (students[j].clas > students[j + 1].clas ||
|
||||
(students[j].clas == students[j + 1].clas && students[j].total < students[j + 1].total)) {
|
||||
// 交换学生信息
|
||||
Student temp = students[j];
|
||||
students[j] = students[j + 1];
|
||||
students[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("Students sorted by class and total score.\n");
|
||||
}
|
||||
// 显示学生信息
|
||||
void displayStudents1(const Student students[], int studentCount) {
|
||||
int i; // 循环变量声明移到外部
|
||||
if (studentCount == 0) {
|
||||
printf("No student information available.\n");
|
||||
return;
|
||||
}
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
if(i>0&&(students[i].clas==students[i-1].clas))
|
||||
{
|
||||
printf(" %05d %-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("%-7d%05d %-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].clas,
|
||||
students[i].id,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,179 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_STUDENTS 7 // 初始化 7 个学生
|
||||
|
||||
typedef struct {
|
||||
int id; // 学号
|
||||
int clas; // 班级
|
||||
char name[50]; // 学生名字
|
||||
double math; // 高数成绩
|
||||
double physics; // 大学物理成绩
|
||||
double english; // 英语成绩
|
||||
double total; // 总成绩
|
||||
} Student;
|
||||
|
||||
// 函数声明
|
||||
void sortStudents(Student students[], int studentCount); // 排序函数
|
||||
void displayStudents(const Student students[], int studentCount); // 输出函数
|
||||
void queryStudents(const Student students[], int studentCount); // 查询主函数
|
||||
void searchStudents(const Student students[], int studentCount, int queryType); // 查询逻辑处理
|
||||
|
||||
int main() {
|
||||
// 初始化 7 个学生信息
|
||||
Student students[MAX_STUDENTS] = {
|
||||
{10001, 11, "Alice", 99.5, 88.5, 89.5, 99.5 + 88.5 + 89.5},
|
||||
{10002, 12, "Bob", 77.9, 56.5, 87.5, 77.9 + 56.5 + 87.5},
|
||||
{10003, 11, "Charlie", 92.5, 99.0, 60.5, 92.5 + 99.0 + 60.5},
|
||||
{10004, 11, "David", 89.6, 56.9, 90.5, 89.6 + 56.9 + 90.5},
|
||||
{10005, 14, "Eve", 55.6, 67.9, 98.9, 55.6 + 67.9 + 98.9},
|
||||
{10006, 12, "Frank", 22.1, 45.9, 99.2, 22.1 + 45.9 + 99.2},
|
||||
{10007, 13, "Grace", 35.6, 67.9, 88.0, 35.6 + 67.9 + 88.0}
|
||||
};
|
||||
int studentCount = MAX_STUDENTS; // 当前学生数量
|
||||
|
||||
// 调用排序函数
|
||||
sortStudents(students, studentCount);
|
||||
|
||||
// 调用输出函数,显示排序后的学生信息
|
||||
displayStudents(students, studentCount);
|
||||
|
||||
// 调用查询函数,进行学生信息查询
|
||||
queryStudents(students, studentCount);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 排序函数:按班级升序,同班级内按总成绩降序排序
|
||||
void sortStudents(Student students[], int studentCount) {
|
||||
int i, j;
|
||||
for (i = 0; i < studentCount - 1; i++) {
|
||||
for (j = 0; j < studentCount - i - 1; j++) {
|
||||
if (students[j].clas > students[j + 1].clas ||
|
||||
(students[j].clas == students[j + 1].clas && students[j].total < students[j + 1].total)) {
|
||||
Student temp = students[j];
|
||||
students[j] = students[j + 1];
|
||||
students[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 输出函数:显示学生信息
|
||||
void displayStudents(const Student students[], int studentCount) {
|
||||
int i;
|
||||
printf("\nStudent Information:\n");
|
||||
printf(" ID Class Name Math Physics English Total\n");
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
printf("%05d %-7d%-12s%-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].clas,
|
||||
students[i].name,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
}
|
||||
}
|
||||
|
||||
// 查询主函数:提供查询选项
|
||||
void queryStudents(const Student students[], int studentCount) {
|
||||
int choice;
|
||||
while (1) {
|
||||
printf("\nQuery Options:\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");
|
||||
printf("6. Exit\n");
|
||||
printf("Enter your choice: ");
|
||||
scanf("%d", &choice);
|
||||
|
||||
if (choice == 6) {
|
||||
printf("Exiting query process.\n");
|
||||
break;
|
||||
}
|
||||
|
||||
searchStudents(students, studentCount, choice);
|
||||
}
|
||||
}
|
||||
|
||||
// 搜索学生函数:根据不同查询条件进行处理
|
||||
void searchStudents(const Student students[], int studentCount, int queryType) {
|
||||
int classStart = 0, classEnd = 0, idStart = 0, idEnd = 0;
|
||||
double minTotal = 0.0;
|
||||
char prefix[50];
|
||||
int i, found = 0;
|
||||
|
||||
// 根据查询类型,输入相应的参数
|
||||
switch (queryType) {
|
||||
case 1: // 按班级范围查询
|
||||
printf("Enter class range (start-end): ");
|
||||
scanf("%d-%d", &classStart, &classEnd);
|
||||
break;
|
||||
case 2: // 按学号范围查询
|
||||
printf("Enter ID range (start-end): ");
|
||||
scanf("%d-%d", &idStart, &idEnd);
|
||||
break;
|
||||
case 3: // 按名字前缀查询
|
||||
printf("Enter the name prefix: ");
|
||||
scanf("%s", prefix);
|
||||
break;
|
||||
case 4: // 按总成绩范围查询
|
||||
printf("Enter the minimum total score: ");
|
||||
scanf("%lf", &minTotal);
|
||||
break;
|
||||
case 5: // 按班级和学号组合查询
|
||||
printf("Enter class range (start-end): ");
|
||||
scanf("%d-%d", &classStart, &classEnd);
|
||||
printf("Enter ID range (start-end): ");
|
||||
scanf("%d-%d", &idStart, &idEnd);
|
||||
break;
|
||||
default:
|
||||
printf("Invalid choice. Please try again.\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// 输出符合条件的学生信息
|
||||
printf("\nMatching Students:\n");
|
||||
printf(" ID Class Name Math Physics English Total\n");
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
int match = 0;
|
||||
switch (queryType) {
|
||||
case 1: // 按班级范围查询
|
||||
match = (students[i].clas >= classStart && students[i].clas <= classEnd);
|
||||
break;
|
||||
case 2: // 按学号范围查询
|
||||
match = (students[i].id >= idStart && students[i].id <= idEnd);
|
||||
break;
|
||||
case 3: // 按名字前缀查询
|
||||
match = (strncmp(students[i].name, prefix, strlen(prefix)) == 0);
|
||||
break;
|
||||
case 4: // 按总成绩范围查询
|
||||
match = (students[i].total >= minTotal);
|
||||
break;
|
||||
case 5: // 按班级和学号组合查询
|
||||
match = (students[i].clas >= classStart && students[i].clas <= classEnd &&
|
||||
students[i].id >= idStart && students[i].id <= idEnd);
|
||||
break;
|
||||
}
|
||||
|
||||
if (match) {
|
||||
printf("%05d %-7d%-12s%-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].clas,
|
||||
students[i].name,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
printf("No students found matching the given criteria.\n");
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,273 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MAX_STUDENTS 100 // 初始化最多 100 个学生
|
||||
|
||||
typedef struct {
|
||||
char *id; // 学号
|
||||
char *clas; // 班级
|
||||
char *name; // 学生名字
|
||||
double math; // 高数成绩
|
||||
double physics; // 大学物理成绩
|
||||
double english; // 英语成绩
|
||||
double total; // 总成绩
|
||||
} Student;
|
||||
|
||||
// 函数声明
|
||||
void inputStudents(Student students[], int *studentCount); // 录入函数
|
||||
void deleteStudent(Student students[], int *studentCount); // 删除函数
|
||||
void sortStudents(Student students[], int studentCount); // 排序函数
|
||||
void displayStudents(const Student students[], int studentCount); // 输出函数
|
||||
void queryStudents(const Student students[], int studentCount); // 查询主函数
|
||||
|
||||
int main() {
|
||||
Student students[MAX_STUDENTS]; // 初始化学生数组
|
||||
int studentCount = 0; // 当前学生数量
|
||||
|
||||
int choice;
|
||||
while (1) {
|
||||
printf("\nMenu Options:\n");
|
||||
printf("1. Input student information\n");
|
||||
printf("2. Delete student information\n");
|
||||
printf("3. Sort student information\n");
|
||||
printf("4. Output student information\n");
|
||||
printf("5. Query student information\n");
|
||||
printf("6. Quit\n");
|
||||
printf("Enter your choice: ");
|
||||
scanf("%d", &choice);
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
inputStudents(students, &studentCount);
|
||||
break;
|
||||
case 2:
|
||||
deleteStudent(students, &studentCount);
|
||||
break;
|
||||
case 3:
|
||||
sortStudents(students, studentCount);
|
||||
break;
|
||||
case 4:
|
||||
displayStudents(students, studentCount);
|
||||
break;
|
||||
case 5:
|
||||
queryStudents(students, studentCount);
|
||||
break;
|
||||
case 6:
|
||||
printf("Exiting program.\n");
|
||||
return 0;
|
||||
default:
|
||||
printf("Invalid choice. Please try again.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 录入函数:输入学生信息
|
||||
void inputStudents(Student students[], int *studentCount) {
|
||||
int moreInput = 1;
|
||||
while (moreInput && *studentCount < MAX_STUDENTS) {
|
||||
students[*studentCount].id = (char *)malloc(20 * sizeof(char));
|
||||
students[*studentCount].clas = (char *)malloc(20 * sizeof(char));
|
||||
students[*studentCount].name = (char *)malloc(50 * sizeof(char));
|
||||
|
||||
printf("Enter student ID: ");
|
||||
scanf("%s", students[*studentCount].id);
|
||||
printf("Enter class: ");
|
||||
scanf("%s", students[*studentCount].clas);
|
||||
printf("Enter name: ");
|
||||
scanf("%s", students[*studentCount].name);
|
||||
printf("Enter math score: ");
|
||||
scanf("%lf", &students[*studentCount].math);
|
||||
printf("Enter physics score: ");
|
||||
scanf("%lf", &students[*studentCount].physics);
|
||||
printf("Enter english score: ");
|
||||
scanf("%lf", &students[*studentCount].english);
|
||||
students[*studentCount].total = students[*studentCount].math + students[*studentCount].physics + students[*studentCount].english;
|
||||
(*studentCount)++;
|
||||
|
||||
printf("Continue? (1 for yes, 0 for no): ");
|
||||
scanf("%d", &moreInput);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除函数:通过学号删除学生信息
|
||||
void deleteStudent(Student students[], int *studentCount) {
|
||||
char id[20];
|
||||
printf("Enter student ID to delete: ");
|
||||
scanf("%s", id);
|
||||
int i,j, found = 0;
|
||||
for (i = 0; i < *studentCount; i++) {
|
||||
if (strcmp(students[i].id, id) == 0) {
|
||||
found = 1;
|
||||
free(students[i].id);
|
||||
free(students[i].clas);
|
||||
free(students[i].name);
|
||||
for (j = i; j < *studentCount - 1; j++) {
|
||||
students[j] = students[j + 1];
|
||||
}
|
||||
(*studentCount)--;
|
||||
printf("Student with ID %s deleted.\n", id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!found) {
|
||||
printf("No student found with ID %s.\n", id);
|
||||
}
|
||||
}
|
||||
|
||||
// 排序函数:按班级升序,同班级内按总成绩降序排序
|
||||
void sortStudents(Student students[], int studentCount) {
|
||||
int i, j;
|
||||
for (i = 0; i < studentCount - 1; i++) {
|
||||
for (j = 0; j < studentCount - i - 1; j++) {
|
||||
if (strcmp(students[j].clas, students[j + 1].clas) > 0 ||
|
||||
(strcmp(students[j].clas, students[j + 1].clas) == 0 && students[j].total < students[j + 1].total)) {
|
||||
Student temp = students[j];
|
||||
students[j] = students[j + 1];
|
||||
students[j + 1] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 输出函数:显示学生信息
|
||||
void displayStudents(const Student students[], int studentCount) {
|
||||
int i;
|
||||
printf("\nStudent Information:\n");
|
||||
printf(" ID Class Name Math Physics English Total\n");
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
printf("%s %-7s%-12s%-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].clas,
|
||||
students[i].name,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
}
|
||||
}
|
||||
|
||||
// 查询主函数:提供查询选项
|
||||
void queryStudents(const Student students[], int studentCount) {
|
||||
int choice;
|
||||
while (1) {
|
||||
printf("\nQuery Options:\n");
|
||||
printf("1. Search by class\n");
|
||||
printf("2. Search by ID\n");
|
||||
printf("3. Search by name prefix\n");
|
||||
printf("4. Search by total score\n");
|
||||
printf("5. Search by class and ID range\n");
|
||||
printf("6. Exit query\n");
|
||||
printf("Enter your choice: ");
|
||||
scanf("%d", &choice);
|
||||
|
||||
if (choice == 6) {
|
||||
printf("Exiting query process.\n");
|
||||
break;
|
||||
}
|
||||
|
||||
char input[50];
|
||||
double minTotal;
|
||||
int idStart, idEnd;
|
||||
int i, found = 0;
|
||||
|
||||
switch (choice) {
|
||||
case 1:
|
||||
printf("Enter class: ");
|
||||
scanf("%s", input);
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
if (strcmp(students[i].clas, input) == 0) {
|
||||
printf("%s %-7s%-12s%-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].clas,
|
||||
students[i].name,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
printf("Enter ID: ");
|
||||
scanf("%s", input);
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
if (strcmp(students[i].id, input) == 0) {
|
||||
printf("%s %-7s%-12s%-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].clas,
|
||||
students[i].name,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
printf("Enter name prefix: ");
|
||||
scanf("%s", input);
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
if (strncmp(students[i].name, input, strlen(input)) == 0) {
|
||||
printf("%s %-7s%-12s%-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].clas,
|
||||
students[i].name,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 4:
|
||||
printf("Enter the minimum total score: ");
|
||||
scanf("%lf", &minTotal);
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
if (students[i].total >= minTotal) {
|
||||
printf("%s %-7s%-12s%-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].clas,
|
||||
students[i].name,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 5:
|
||||
printf("Enter class: ");
|
||||
scanf("%s", input);
|
||||
printf("Enter ID range (start-end): ");
|
||||
scanf("%d-%d", &idStart, &idEnd);
|
||||
for (i = 0; i < studentCount; i++) {
|
||||
int studentId = atoi(students[i].id);
|
||||
if (strcmp(students[i].clas, input) == 0 && studentId >= idStart && studentId <= idEnd) {
|
||||
printf("%s %-7s%-12s%-8.1f%-8.1f%-8.1f%-8.1f\n",
|
||||
students[i].id,
|
||||
students[i].clas,
|
||||
students[i].name,
|
||||
students[i].math,
|
||||
students[i].physics,
|
||||
students[i].english,
|
||||
students[i].total);
|
||||
found = 1;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
printf("Invalid choice. Please try again.\n");
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
printf("No students found matching the given criteria.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in new issue