给我一个上述问题完整的代码
时间: 2024-03-26 21:39:13 浏览: 19
好的,以下是一个简单的学生成绩管理系统的完整代码,包含输入、查找和删除功能:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
typedef struct {
char name[20];
int id;
float score;
} Student;
Student students[MAX_STUDENTS];
int num_students = 0;
void input_student() {
if (num_students >= MAX_STUDENTS) {
printf("Too many students!\n");
return;
}
Student s;
printf("Enter student name: ");
scanf("%s", s.name);
printf("Enter student id: ");
scanf("%d", &s.id);
printf("Enter student score: ");
scanf("%f", &s.score);
students[num_students++] = s;
printf("Added student %s with id %d and score %f.\n", s.name, s.id, s.score);
}
void find_student() {
int id;
printf("Enter student id: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("Found student %s with id %d and score %f.\n", students[i].name, students[i].id, students[i].score);
return;
}
}
printf("Student with id %d not found.\n", id);
}
void delete_student() {
int id;
printf("Enter student id: ");
scanf("%d", &id);
for (int i = 0; i < num_students; i++) {
if (students[i].id == id) {
printf("Deleted student %s with id %d and score %f.\n", students[i].name, students[i].id, students[i].score);
num_students--;
for (int j = i; j < num_students; j++) {
students[j] = students[j+1];
}
return;
}
}
printf("Student with id %d not found.\n", id);
}
int main() {
int choice;
do {
printf("\n1. Add student\n");
printf("2. Find student\n");
printf("3. Delete student\n");
printf("4. Quit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
input_student();
break;
case 2:
find_student();
break;
case 3:
delete_student();
break;
case 4:
printf("Goodbye!\n");
break;
default:
printf("Invalid choice!\n");
break;
}
} while (choice != 4);
return 0;
}
```
在该代码中,使用了 `do-while` 循环来不断提示用户输入操作选项,直到用户选择退出。在每个选项中,调用了相应的函数来实现输入、查找和删除功能。其中,删除函数使用了一个循环来将要删除的学生后面的所有学生向前移动一个位置,以便删除指定学生。
当然,该代码还可以改进,例如可以添加修改学生信息的功能,可以对输入进行一些验证等。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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://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://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://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://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://csdnimg.cn/download_wenku/file_type_ask_c1.png)