C语言结构体数组学生信息管理
时间: 2023-06-23 17:55:20 浏览: 52
好的,那我来给你讲解一下C语言结构体数组实现学生信息管理的基本思路和方法吧。
首先,我们可以定义一个结构体类型,来表示学生的各种信息,比如学号、姓名、年龄、性别、成绩等等。定义方式如下:
```
struct student {
char id[20];
char name[20];
int age;
char sex[10];
float score;
};
```
然后,我们可以定义一个结构体数组,来存储多个学生的信息。定义方式如下:
```
struct student stu[100];
```
这样,我们就可以通过 stu[0]、stu[1]、stu[2] 等方式来访问不同学生的信息。
接下来,我们可以通过循环语句和条件语句,来实现学生信息的录入、查询、修改、删除等操作。具体实现方式可以参考下面的代码示例:
```
#include <stdio.h>
#include <string.h>
struct student {
char id[20];
char name[20];
int age;
char sex[10];
float score;
};
int main()
{
struct student stu[100];
int n, i, j, flag = 0;
char id[20];
printf("请输入学生人数:");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("请输入第%d个学生的信息:\n", i+1);
printf("学号:");
scanf("%s", stu[i].id);
printf("姓名:");
scanf("%s", stu[i].name);
printf("年龄:");
scanf("%d", &stu[i].age);
printf("性别:");
scanf("%s", stu[i].sex);
printf("成绩:");
scanf("%f", &stu[i].score);
}
printf("请输入要查询的学生学号:");
scanf("%s", id);
for (i = 0; i < n; i++) {
if (strcmp(stu[i].id, id) == 0) {
printf("该学生的信息为:\n");
printf("学号:%s\n", stu[i].id);
printf("姓名:%s\n", stu[i].name);
printf("年龄:%d\n", stu[i].age);
printf("性别:%s\n", stu[i].sex);
printf("成绩:%.2f\n", stu[i].score);
flag = 1;
break;
}
}
if (flag == 0) {
printf("没有找到该学生的信息!\n");
}
printf("请输入要修改的学生学号:");
scanf("%s", id);
for (i = 0; i < n; i++) {
if (strcmp(stu[i].id, id) == 0) {
printf("请输入修改后的学生信息:\n");
printf("学号:");
scanf("%s", stu[i].id);
printf("姓名:");
scanf("%s", stu[i].name);
printf("年龄:");
scanf("%d", &stu[i].age);
printf("性别:");
scanf("%s", stu[i].sex);
printf("成绩:");
scanf("%f", &stu[i].score);
flag = 1;
break;
}
}
if (flag == 0) {
printf("没有找到该学生的信息!\n");
}
printf("请输入要删除的学生学号:");
scanf("%s", id);
for (i = 0; i < n; i++) {
if (strcmp(stu[i].id, id) == 0) {
for (j = i; j < n-1; j++) {
stu[j] = stu[j+1];
}
n--;
flag = 1;
break;
}
}
if (flag == 0) {
printf("没有找到该学生的信息!\n");
}
printf("现在的学生信息为:\n");
for (i = 0; i < n; i++) {
printf("学号:%s\n", stu[i].id);
printf("姓名:%s\n", stu[i].name);
printf("年龄:%d\n", stu[i].age);
printf("性别:%s\n", stu[i].sex);
printf("成绩:%.2f\n", stu[i].score);
}
return 0;
}
```
以上就是一个基本的C语言结构体数组学生信息管理的实现方式,希望能对你有所帮助。
相关推荐
![](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)