C语言实现学生信息管理:线性表(顺序表和链表)实战教程
C语言实现学生信息管理系统:线性表实战教程
一、实验目的
- 掌握线性表的定义。2. 掌握线性表的基本操作,如建立、查找、插入和删除等。
二、实验环境
- 计算机* VC6.0 或其他 C 语言 IDE
三、实验内容与实验过程及分析
3.1 实验内容
定义一个包含学生信息(学号,姓名,成绩)的顺序表和链表,使其具有如下功能:
- 输入学生信息。2. 显示学生表中所有学生的相关信息。3. 根据姓名进行查找,返回此学生的学号和成绩。4. 根据指定的位置可返回相应的学生信息(学号,姓名,成绩)。5. 给定一个学生信息,插入到表中指定的位置。6. 删除指定位置的学生记录。7. 统计表中学生个数。
3.2 实验要求
- 程序要添加适当的注释,程序的书写要采用缩进格式。2. 程序要具在一定的健壮性,即当输入数据非法时,程序也能适当地做出反应,如插入删除时指定的位置不对等等。3. 程序要做到界面友好,在程序运行时用户可以根据相应的提示信息进行操作。
四、C语言代码实现
下面是一个根据上述要求编写的 C 语言代码示例:c#include <stdio.h>#include <stdlib.h>#include <string.h>
#define MAX_STUDENTS 100
// 定义学生结构体typedef struct { int studentID; 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('Student Table: '); for (int i = 0; i < table->count; i++) { printf('Student #%d: ', i + 1); printf('Student ID: %d ', table->students[i].studentID); printf('Name: %s ', table->students[i].name); printf('Score: %d ', table->students[i].score); printf(' '); }}
// 按姓名查找学生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; // Found } } return 0; // Not found}
// 按位置查找学生int searchByPosition(StudentTable *table, int position, int *studentID, char *name, int *score) { if (position < 1 || position > table->count) { return 0; // Invalid position } *studentID = table->students[position - 1].studentID; strcpy(name, table->students[position - 1].name); *score = table->students[position - 1].score; return 1; // Success}
// 插入学生信息int insertStudent(StudentTable *table, int position, int studentID, char *name, int score) { if (position < 1 || position > table->count + 1) { return 0; // Invalid position } if (table->count == MAX_STUDENTS) { return 0; // Table is full } 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; // Success}
// 删除学生信息int deleteStudent(StudentTable *table, int position) { if (position < 1 || position > table->count) { return 0; // Invalid position } for (int i = position - 1; i < table->count - 1; i++) { table->students[i] = table->students[i + 1]; } table->count--; return 1; // Success}
// 统计学生人数int countStudents(StudentTable *table) { return table->count;}
// 主函数int main() { StudentTable table; initialize(&table); int choice, position, studentID, score; char name[50];
printf('Linear Table - Student Information
');
while (1) { printf('
-
Input student information '); printf('2. Display all students '); printf('3. Search by name '); printf('4. Search by position '); printf('5. Insert student '); printf('6. Delete student '); printf('7. Count students '); printf('8. Quit '); printf('Enter your choice: '); scanf('%d', &choice);
switch (choice) { case 1: printf('
Enter student ID: '); scanf('%d', &studentID); printf('Enter name: '); scanf('%s', name); printf('Enter score: '); scanf('%d', &score); insertStudent(&table, table.count + 1, studentID, name, score); break; case 2: displayAllStudents(&table); break; case 3: printf(' Enter name to search: '); scanf('%s', name); if (searchByName(&table, name, &studentID, &score)) { printf('Student found: '); printf('Student ID: %d ', studentID); printf('Score: %d ', score); } else { printf('Student not found. '); } break; case 4: printf(' Enter position to search: '); scanf('%d', &position); if (searchByPosition(&table, position, &studentID, name, &score)) { printf('Student found: '); printf('Name: %s ', name); printf('Student ID: %d ', studentID); printf('Score: %d ', score); } else { printf('Invalid position. '); } break; case 5: printf(' Enter position to insert: '); scanf('%d', &position); printf('Enter student ID: '); scanf('%d', &studentID); printf('Enter name: '); scanf('%s', name); printf('Enter score: '); scanf('%d', &score); if (insertStudent(&table, position, studentID, name, score)) { printf('Student inserted successfully. '); } else { printf('Invalid position or table is full. '); } break; case 6: printf(' Enter position to delete: '); scanf('%d', &position); if (deleteStudent(&table, position)) { printf('Student deleted successfully. '); } else { printf('Invalid position. '); } break; case 7: printf(' Number of students: %d ', countStudents(&table)); break; case 8: printf(' Program exited. '); exit(0); default: printf(' Invalid choice. Please try again. '); } }
return 0;}
五、总结
本教程介绍了如何使用 C 语言实现一个简单的学生信息管理系统,并详细讲解了线性表、顺序表和链表的概念及操作方法。代码中包含了丰富的注释,方便读者理解程序的逻辑和实现细节。
注意: 这只是一个简单的示例代码,实际开发中,您可能需要根据实际需求进行适当的修改和扩展,例如添加数据验证、错误处理、文件读写等功能,以提高程序的健壮性和实用性。
原文地址: https://www.cveoy.top/t/topic/EmV 著作权归作者所有。请勿转载和采集!