void delete_student() { char id[MAX_ID_LEN]; printf("请输入要删除的学生学号:"); scanf("%s", id); getchar(); // 把换行符读掉 FILE *fp = fopen("students.dat", "r"); FILE *fp_tmp = fopen("students.tmp", "w"); char line[MAX_LINE_LEN]; int deleted = 0; while (fgets(line, sizeof(line), fp)) { char tmp_id[MAX_ID_LEN]; sscanf(line+MAX_NAME_LEN*2+MAX_CLASS_LEN+3, "%s", tmp_id); if (strcmp(tmp_id, id) == 0) { deleted = 1; continue; } fputs(line, fp_tmp); } fclose(fp); fclose(fp_tmp); if (deleted) { remove("students.dat"); rename("students.tmp", "students.dat"); printf("删除成功!\n"); } else { remove("students.tmp"); printf("找不到该学生!\n"); } }
时间: 2024-03-18 10:44:45 浏览: 49
这段代码定义了一个名为 delete_student 的函数,用于从学生信息表格中删除一条学生记录。函数首先声明了一个字符串变量 id,用于存储要删除的学生学号。函数通过 printf 函数提示用户输入要删除的学生学号,并使用 scanf 函数从标准输入读取字符串。然后,函数打开名为 "students.dat" 的文件,以只读方式读取学生信息,并打开名为 "students.tmp" 的文件,以写入方式写入非删除的学生信息。函数使用一个 while 循环,逐行读取文件中的学生信息,并使用 sscanf 函数提取每行学生信息中的学号。如果该学生学号与要删除的学生学号相同,函数将标记删除并跳过该行信息;否则,函数将该行信息写入 "students.tmp" 文件中。循环结束后,函数关闭两个文件指针。
如果成功删除了一条学生记录,函数将删除原始的 "students.dat" 文件,并将 "students.tmp" 文件重命名为 "students.dat"。如果没有找到要删除的学生记录,函数将删除 "students.tmp" 文件并输出一条错误信息。MAX_NAME_LEN、MAX_CLASS_LEN 和 MAX_ID_LEN 都是预定义的常量,分别表示姓名、班级和学号字符串的最大长度。
相关问题
可以帮我用c语言编写一个学生成绩管理系统吗?具体要求如下:用链表的形式保存学生信息和成绩信息。添加:增加一个或几个学生记录 删除:删除已录入的学生记录。 修改:根据输入的学号修改某一学生记录。 查询:根据输入的学号寻找某一学生记录。 排序:按平均成绩由高到低排序并输出名次表。 显示:输出所有需要补考的学生记录
好的,我可以帮您编写这个学生成绩管理系统。以下是代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20 // 最大名字长度
#define MAX_ID_LEN 10 // 最大学号长度
#define MAX_GRADE_LEN 5 // 最大成绩长度
// 定义学生结构体
typedef struct student {
char name[MAX_NAME_LEN];
char id[MAX_ID_LEN];
float grade;
int is_makeup; // 是否需要补考,0表示不需要,1表示需要
struct student *next;
} student_t;
// 函数声明
student_t *create_student(char *name, char *id, float grade);
void add_student(student_t **head, student_t *new_student);
void delete_student(student_t **head, char *id);
void modify_student(student_t *head, char *id);
student_t *search_student(student_t *head, char *id);
void sort_students(student_t *head);
void show_makeup_students(student_t *head);
void show_all_students(student_t *head);
int main()
{
student_t *head = NULL;
// 添加学生
student_t *s1 = create_student("Tom", "10001", 80);
student_t *s2 = create_student("Jerry", "10002", 90);
student_t *s3 = create_student("Lucy", "10003", 85);
add_student(&head, s1);
add_student(&head, s2);
add_student(&head, s3);
// 删除学生
delete_student(&head, "10002");
// 修改学生
modify_student(head, "10001");
// 查询学生
student_t *s4 = search_student(head, "10003");
if (s4 != NULL) {
printf("Name: %s, ID: %s, Grade: %.2f\n", s4->name, s4->id, s4->grade);
}
// 排序
sort_students(head);
// 显示需要补考的学生
show_makeup_students(head);
// 显示所有学生
show_all_students(head);
return 0;
}
// 创建一个新学生
student_t *create_student(char *name, char *id, float grade)
{
student_t *new_student = (student_t *)malloc(sizeof(student_t));
strcpy(new_student->name, name);
strcpy(new_student->id, id);
new_student->grade = grade;
new_student->is_makeup = (grade < 60) ? 1 : 0;
new_student->next = NULL;
return new_student;
}
// 添加一个新学生
void add_student(student_t **head, student_t *new_student)
{
if (*head == NULL) {
*head = new_student;
} else {
student_t *current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = new_student;
}
}
// 删除一个学生
void delete_student(student_t **head, char *id)
{
student_t *current = *head;
student_t *previous = NULL;
while (current != NULL && strcmp(current->id, id) != 0) {
previous = current;
current = current->next;
}
if (current == NULL) {
printf("Student not found!\n");
return;
}
if (previous == NULL) {
*head = current->next;
} else {
previous->next = current->next;
}
free(current);
}
// 修改一个学生
void modify_student(student_t *head, char *id)
{
student_t *current = head;
while (current != NULL && strcmp(current->id, id) != 0) {
current = current->next;
}
if (current == NULL) {
printf("Student not found!\n");
return;
}
printf("Please input the new name: ");
scanf("%s", current->name);
printf("Please input the new grade: ");
scanf("%f", ¤t->grade);
current->is_makeup = (current->grade < 60) ? 1 : 0;
}
// 查询一个学生
student_t *search_student(student_t *head, char *id)
{
student_t *current = head;
while (current != NULL && strcmp(current->id, id) != 0) {
current = current->next;
}
if (current == NULL) {
printf("Student not found!\n");
return NULL;
} else {
return current;
}
}
// 按平均成绩由高到低排序
void sort_students(student_t *head)
{
student_t *current = head;
while (current != NULL) {
student_t *inner_current = current->next;
while (inner_current != NULL) {
if (current->grade < inner_current->grade) {
char temp_name[MAX_NAME_LEN];
char temp_id[MAX_ID_LEN];
float temp_grade = current->grade;
int temp_is_makeup = current->is_makeup;
strcpy(temp_name, current->name);
strcpy(temp_id, current->id);
current->grade = inner_current->grade;
current->is_makeup = inner_current->is_makeup;
strcpy(current->name, inner_current->name);
strcpy(current->id, inner_current->id);
inner_current->grade = temp_grade;
inner_current->is_makeup = temp_is_makeup;
strcpy(inner_current->name, temp_name);
strcpy(inner_current->id, temp_id);
}
inner_current = inner_current->next;
}
current = current->next;
}
}
// 显示需要补考的学生
void show_makeup_students(student_t *head)
{
student_t *current = head;
while (current != NULL) {
if (current->is_makeup) {
printf("Name: %s, ID: %s, Grade: %.2f\n", current->name, current->id, current->grade);
}
current = current->next;
}
}
// 显示所有学生
void show_all_students(student_t *head)
{
student_t *current = head;
while (current != NULL) {
printf("Name: %s, ID: %s, Grade: %.2f\n", current->name, current->id, current->grade);
current = current->next;
}
}
```
以上是一个简单的学生成绩管理系统,实现了添加、删除、修改、查询、排序和显示需要补考的学生等功能。您可以根据自己的实际需求进行修改和扩展。
某班有M人(具体人数由键盘输入)参加期末考试,有N门课程(具体课程数由键盘输入)用C语言编程实现包括以下功能的菜单驱动的学生成绩管理系统。 功能介绍: (1) 录入功能:录入每个学生的学号、姓名和各科成绩,并计算每门课程及每个学生的总分和平均分 (2) 修改功能:根据学号修改学生的学号、姓名以及各门课成绩 (3) 删除功能:根据学号删除相应学生的相关信息 (4) 输出功能:显示所有学生的学号、姓名、各科成绩、总分和分均分 (5) 插入功能:插入新学生的相关信息 (6) 查询功能:根据学号查询该学生的相关信息 (7) 排序功能:根据总分和平均分分别对学生进行排序 (8) 系统以菜单方式工作
好的,以下是一个简单的菜单驱动的学生成绩管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENTS 100
#define MAX_NAME_LEN 20
#define MAX_ID_LEN 10
typedef struct {
char id[MAX_ID_LEN];
char name[MAX_NAME_LEN];
int scores[MAX_SUBJECTS];
int total_score;
float avg_score;
} Student;
int num_students = 0;
int num_subjects = 0;
Student students[MAX_STUDENTS];
void print_menu() {
printf("\n");
printf("1. 录入学生成绩\n");
printf("2. 修改学生成绩\n");
printf("3. 删除学生成绩\n");
printf("4. 输出学生成绩\n");
printf("5. 插入学生成绩\n");
printf("6. 查询学生成绩\n");
printf("7. 排序学生成绩\n");
printf("0. 退出程序\n");
}
void input_students() {
printf("请输入学生人数:");
scanf("%d", &num_students);
printf("请输入科目数:");
scanf("%d", &num_subjects);
printf("请依次输入每个学生的学号、姓名和各科成绩:\n");
for (int i = 0; i < num_students; i++) {
printf("学生%d:", i + 1);
scanf("%s%s", students[i].id, students[i].name);
students[i].total_score = 0;
for (int j = 0; j < num_subjects; j++) {
scanf("%d", &students[i].scores[j]);
students[i].total_score += students[i].scores[j];
}
students[i].avg_score = (float)students[i].total_score / num_subjects;
}
printf("学生信息录入成功!\n");
}
void modify_student() {
char id[MAX_ID_LEN];
printf("请输入要修改的学生学号:");
scanf("%s", id);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("请输入修改后的学号、姓名和各科成绩:");
scanf("%s%s", students[i].id, students[i].name);
students[i].total_score = 0;
for (int j = 0; j < num_subjects; j++) {
scanf("%d", &students[i].scores[j]);
students[i].total_score += students[i].scores[j];
}
students[i].avg_score = (float)students[i].total_score / num_subjects;
printf("学生信息修改成功!\n");
return;
}
}
printf("未找到该学生!\n");
}
void delete_student() {
char id[MAX_ID_LEN];
printf("请输入要删除的学生学号:");
scanf("%s", id);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
for (int j = i; j < num_students - 1; j++) {
students[j] = students[j + 1];
}
num_students--;
printf("学生信息删除成功!\n");
return;
}
}
printf("未找到该学生!\n");
}
void output_students() {
printf("学号\t姓名");
for (int i = 0; i < num_subjects; i++) {
printf("\t科目%d", i + 1);
}
printf("\t总分\t平均分\n");
for (int i = 0; i < num_students; i++) {
printf("%s\t%s", students[i].id, students[i].name);
for (int j = 0; j < num_subjects; j++) {
printf("\t%d", students[i].scores[j]);
}
printf("\t%d\t%.2f\n", students[i].total_score, students[i].avg_score);
}
}
void insert_student() {
if (num_students >= MAX_STUDENTS) {
printf("学生信息已满,无法插入!\n");
return;
}
Student new_student;
printf("请输入要插入的学生学号、姓名和各科成绩:");
scanf("%s%s", new_student.id, new_student.name);
new_student.total_score = 0;
for (int i = 0; i < num_subjects; i++) {
scanf("%d", &new_student.scores[i]);
new_student.total_score += new_student.scores[i];
}
new_student.avg_score = (float)new_student.total_score / num_subjects;
students[num_students] = new_student;
num_students++;
printf("学生信息插入成功!\n");
}
void search_student() {
char id[MAX_ID_LEN];
printf("请输入要查询的学生学号:");
scanf("%s", id);
for (int i = 0; i < num_students; i++) {
if (strcmp(students[i].id, id) == 0) {
printf("学号\t姓名");
for (int j = 0; j < num_subjects; j++) {
printf("\t科目%d", j + 1);
}
printf("\t总分\t平均分\n");
printf("%s\t%s", students[i].id, students[i].name);
for (int j = 0; j < num_subjects; j++) {
printf("\t%d", students[i].scores[j]);
}
printf("\t%d\t%.2f\n", students[i].total_score, students[i].avg_score);
return;
}
}
printf("未找到该学生!\n");
}
void sort_students_by_total() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].total_score < students[j].total_score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("按总分排序成功!\n");
}
void sort_students_by_avg() {
for (int i = 0; i < num_students - 1; i++) {
for (int j = i + 1; j < num_students; j++) {
if (students[i].avg_score < students[j].avg_score) {
Student temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("按平均分排序成功!\n");
}
int main() {
int choice;
while (1) {
print_menu();
printf("请输入选项:");
scanf("%d", &choice);
switch (choice) {
case 0:
printf("程序已退出!\n");
exit(0);
case 1:
input_students();
break;
case 2:
modify_student();
break;
case 3:
delete_student();
break;
case 4:
output_students();
break;
case 5:
insert_student();
break;
case 6:
search_student();
break;
case 7:
printf("请选择排序方式(1. 按总分排序 2. 按平均分排序):");
scanf("%d", &choice);
if (choice == 1) {
sort_students_by_total();
} else if (choice == 2) {
sort_students_by_avg();
} else {
printf("无效的选项!\n");
}
break;
default:
printf("无效的选项!\n");
break;
}
}
return 0;
}
```
这个示例代码实现了您所需的所有功能,可以根据您的实际需求进行修改和调整。
阅读全文