C语言实现学生信息管理系统:线性表操作详解
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义最大学生人数
#define MAX_STUDENTS 100
// 定义学生结构体
typedef struct {
int studentID; // 学生ID
char name[50]; // 学生姓名
int score; // 学生成绩
} Student;
// 定义学生表结构体
typedef struct {
Student students[MAX_STUDENTS]; // 学生数组
int count; // 学生数量
} StudentTable;
// 初始化学生表
void initialize(StudentTable *table) {
table->count = 0;
}
// 显示所有学生信息
void displayAllStudents(StudentTable *table) {
printf('学生表:\n');
for (int i = 0; i < table->count; i++) {
printf('学生 #%d:\n', i + 1);
printf('学生ID: %d\n', table->students[i].studentID);
printf('姓名: %s\n', table->students[i].name);
printf('成绩: %d\n', table->students[i].score);
printf('\n');
}
}
// 根据姓名查找学生信息
int searchByName(StudentTable *table, char *name, int *studentID, int *score) {
for (int i = 0; i < table->count; i++) {
if (strcmp(table->students[i].name, name) == 0) {
*studentID = table->students[i].studentID;
*score = table->students[i].score;
return 1; // 找到
}
}
return 0; // 未找到
}
// 根据位置查找学生信息
int searchByPosition(StudentTable *table, int position, int *studentID, char *name, int *score) {
if (position < 1 || position > table->count) {
return 0; // 位置无效
}
*studentID = table->students[position - 1].studentID;
strcpy(name, table->students[position - 1].name);
*score = table->students[position - 1].score;
return 1; // 成功
}
// 在指定位置插入学生信息
int insertStudent(StudentTable *table, int position, int studentID, char *name, int score) {
if (position < 1 || position > table->count + 1) {
return 0; // 位置无效
}
if (table->count == MAX_STUDENTS) {
return 0; // 表已满
}
for (int i = table->count; i >= position; i--) {
table->students[i] = table->students[i - 1];
}
table->students[position - 1].studentID = studentID;
strcpy(table->students[position - 1].name, name);
table->students[position - 1].score = score;
table->count++;
return 1; // 成功
}
// 删除指定位置的学生记录
int deleteStudent(StudentTable *table, int position) {
if (position < 1 || position > table->count) {
return 0; // 位置无效
}
for (int i = position - 1; i < table->count - 1; i++) {
table->students[i] = table->students[i + 1];
}
table->count--;
return 1; // 成功
}
// 统计学生数量
int countStudents(StudentTable *table) {
return table->count;
}
int main() {
StudentTable table;
initialize(&table);
int choice, position, studentID, score;
char name[50];
printf('线性表 - 学生信息\n');
while (1) {
printf('\n1. 输入学生信息\n');
printf('2. 显示所有学生\n');
printf('3. 根据姓名查找学生\n');
printf('4. 根据位置查找学生\n');
printf('5. 插入学生信息\n');
printf('6. 删除学生信息\n');
printf('7. 统计学生数量\n');
printf('8. 退出\n');
printf('请输入您的选择: ');
scanf('%d', &choice);
switch (choice) {
case 1:
printf('\n输入学生ID: ');
scanf('%d', &studentID);
printf('输入姓名: ');
scanf('%s', name);
printf('输入成绩: ');
scanf('%d', &score);
insertStudent(&table, table.count + 1, studentID, name, score);
break;
case 2:
displayAllStudents(&table);
break;
case 3:
printf('\n输入要查找的姓名: ');
scanf('%s', name);
if (searchByName(&table, name, &studentID, &score)) {
printf('找到学生:\n');
printf('学生ID: %d\n', studentID);
printf('成绩: %d\n', score);
} else {
printf('未找到学生.\n');
}
break;
case 4:
printf('\n输入要查找的位置: ');
scanf('%d', &position);
if (searchByPosition(&table, position, &studentID, name, &score)) {
printf('找到学生:\n');
printf('姓名: %s\n', name);
printf('学生ID: %d\n', studentID);
printf('成绩: %d\n', score);
} else {
printf('位置无效.\n');
}
break;
case 5:
printf('\n输入要插入的位置: ');
scanf('%d', &position);
printf('输入学生ID: ');
scanf('%d', &studentID);
printf('输入姓名: ');
scanf('%s', name);
printf('输入成绩: ');
scanf('%d', &score);
if (insertStudent(&table, position, studentID, name, score)) {
printf('学生插入成功.\n');
} else {
printf('位置无效或表已满.\n');
}
break;
case 6:
printf('\n输入要删除的位置: ');
scanf('%d', &position);
if (deleteStudent(&table, position)) {
printf('学生删除成功.\n');
} else {
printf('位置无效.\n');
}
break;
case 7:
printf('\n学生数量: %d\n', countStudents(&table));
break;
case 8:
printf('\n程序已退出.\n');
exit(0);
default:
printf('\n无效的选择, 请重试.\n');
}
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/EWC 著作权归作者所有。请勿转载和采集!