利用C语言,设计编写一个传统的小型学生信息管理系统。1、该系统可以用于管理一个班的学生信息。学生人数在50人左右。2、该系统存放了有关学生的某些方面的信息:如学号、姓名、性别、出生日期、成绩、所在系等,具体包含的数据内容由设计者分析确定。3、所有学生信息的数据结构及其存储结构由设计者分析确定。4、所有学生的信息数据可以用文件形式存放。(可选项)5、该系统具有相关的数据输入、输出、查询、修改、插入、删除、统计、排序等功能。(功能的多少设计者可根据需要自行调整,但必须具备输出、输出、查询、某一方面数据统计的基本功能。)6、系统应具有友好的用户界面,并能够让用户通过菜单进行相应功能的选择。
时间: 2023-07-10 22:26:22 浏览: 100
以下是一个简单的小型学生信息管理系统的 C 语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 50
#define MAX_NAME_LENGTH 20
#define MAX_DEPT_LENGTH 10
#define MAX_GRADE_LENGTH 5
struct student {
int id;
char name[MAX_NAME_LENGTH];
char gender;
char birthday[11];
char dept[MAX_DEPT_LENGTH];
char grade[MAX_GRADE_LENGTH];
};
int num_students = 0;
struct student students[MAX_STUDENTS];
void read_data_from_file(char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("Error: cannot open file %s\n", filename);
return;
}
char line[100];
while (fgets(line, sizeof(line), fp)) {
if (num_students >= MAX_STUDENTS) {
printf("Error: too many students\n");
break;
}
struct student s;
sscanf(line, "%d %s %c %s %s %s", &s.id, s.name, &s.gender, s.birthday, s.dept, s.grade);
students[num_students++] = s;
}
fclose(fp);
}
void write_data_to_file(char *filename) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("Error: cannot open file %s\n", filename);
return;
}
for (int i = 0; i < num_students; i++) {
struct student s = students[i];
fprintf(fp, "%d %s %c %s %s %s\n", s.id, s.name, s.gender, s.birthday, s.dept, s.grade);
}
fclose(fp);
}
void print_student(struct student s) {
printf("%d %s %c %s %s %s\n", s.id, s.name, s.gender, s.birthday, s.dept, s.grade);
}
void print_all_students() {
printf("ID Name Gender Birthday Dept Grade\n");
for (int i = 0; i < num_students; i++) {
struct student s = students[i];
print_student(s);
}
}
void add_student() {
if (num_students >= MAX_STUDENTS) {
printf("Error: too many students\n");
return;
}
struct student s;
printf("Please enter student information:\n");
printf("ID: ");
scanf("%d", &s.id);
printf("Name: ");
scanf("%s", s.name);
printf("Gender (M/F): ");
scanf(" %c", &s.gender);
printf("Birthday (YYYY-MM-DD): ");
scanf("%s", s.birthday);
printf("Dept: ");
scanf("%s", s.dept);
printf("Grade: ");
scanf("%s", s.grade);
students[num_students++] = s;
printf("Student added successfully\n");
}
void search_student() {
int id;
printf("Please enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
struct student s = students[i];
if (s.id == id) {
printf("Student found:\n");
print_student(s);
return;
}
}
printf("Student not found\n");
}
void delete_student() {
int id;
printf("Please enter student ID: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
struct student s = students[i];
if (s.id == id) {
for (int j = i; j < num_students - 1; j++) {
students[j] = students[j+1];
}
num_students--;
printf("Student deleted successfully\n");
return;
}
}
printf("Student not found\n");
}
void sort_students() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = 0; j < num_students - i - 1; j++) {
struct student s1 = students[j];
struct student s2 = students[j+1];
if (strcmp(s1.name, s2.name) > 0) {
students[j] = s2;
students[j+1] = s1;
}
}
}
printf("Students sorted by name\n");
}
void menu() {
printf("1. Add student\n");
printf("2. Search student\n");
printf("3. Delete student\n");
printf("4. Print all students\n");
printf("5. Sort students\n");
printf("6. Exit\n");
}
int main() {
read_data_from_file("students.txt");
while (1) {
menu();
int choice;
printf("Please enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_student();
break;
case 2:
search_student();
break;
case 3:
delete_student();
break;
case 4:
print_all_students();
break;
case 5:
sort_students();
break;
case 6:
write_data_to_file("students.txt");
printf("Goodbye!\n");
exit(0);
default:
printf("Invalid choice\n");
}
}
return 0;
}
```
这个程序实现了基本的学生信息管理功能,包括读写文件、添加、查询、删除、打印、排序等。你可以根据自己的需要对其进行修改和扩展。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![c](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://img-home.csdnimg.cn/images/20250102104920.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)