用结构体输入3个学生的信息
时间: 2024-05-13 13:13:43 浏览: 14
好的,我可以回答这个问题。以下是一个示例结构体,用于输入3个学生的信息:
```
struct Student {
char name[50];
int age;
double GPA;
};
Student student1, student2, student3;
printf("请输入第一个学生的姓名:");
scanf("%s", student1.name);
printf("请输入第一个学生的年龄:");
scanf("%d", &student1.age);
printf("请输入第一个学生的GPA:");
scanf("%lf", &student1.GPA);
printf("请输入第二个学生的姓名:");
scanf("%s", student2.name);
printf("请输入第二个学生的年龄:");
scanf("%d", &student2.age);
printf("请输入第二个学生的GPA:");
scanf("%lf", &student2.GPA);
printf("请输入第三个学生的姓名:");
scanf("%s", student3.name);
printf("请输入第三个学生的年龄:");
scanf("%d", &student3.age);
printf("请输入第三个学生的GPA:");
scanf("%lf", &student3.GPA);
```
希望这个回答对您有所帮助。
相关问题
C语言用结构体输入三个学生信息
可以使用如下的C语言代码定义一个学生的结构体,并使用结构体数组来存储多个学生的信息:
```
#include <stdio.h>
// 定义学生结构体
struct student {
char name[20];
int age;
float score;
};
int main() {
// 定义结构体数组
struct student stu[3];
// 输入三个学生的信息
for (int i = 0; i < 3; i++) {
printf("请输入第%d个学生的姓名、年龄、成绩:", i+1);
scanf("%s %d %f", stu[i].name, &stu[i].age, &stu[i].score);
}
// 输出学生信息
for (int i = 0; i < 3; i++) {
printf("第%d个学生的信息:\n", i+1);
printf("姓名:%s\n", stu[i].name);
printf("年龄:%d\n", stu[i].age);
printf("成绩:%.2f\n", stu[i].score);
}
return 0;
}
```
运行程序后,你可以依次输入三个学生的姓名、年龄、成绩,程序会自动存储这些信息,并输出每个学生的信息。
用结构体输入学生信息存入信息库
好的,下面是用结构体输入学生信息并存入信息库的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STUDENT_NUM 100
struct Student {
char name[20];
int age;
float score;
};
int main() {
struct Student students[MAX_STUDENT_NUM];
int student_num = 0;
// 输入学生信息
while (student_num < MAX_STUDENT_NUM) {
struct Student student;
printf("请输入第%d个学生的信息(name, age, score): ", student_num + 1);
scanf("%s %d %f", student.name, &student.age, &student.score);
students[student_num] = student;
student_num++;
char c;
printf("是否继续输入?(y/n)");
scanf(" %c", &c);
if (c != 'y' && c != 'Y') {
break;
}
}
// 输出学生信息
printf("学生信息:\n");
for (int i = 0; i < student_num; i++) {
printf("name=%s, age=%d, score=%.2f\n", students[i].name, students[i].age, students[i].score);
}
return 0;
}
```
在这个示例代码中,我们定义了一个结构体 `Student`,包含了学生的姓名、年龄和成绩。然后定义了一个数组 `students` 来保存多个学生的信息,最多支持存储 `MAX_STUDENT_NUM` 个学生的信息。在主函数中,我们通过一个循环来输入学生信息,并将其存入 `students` 数组中。在输入过程中,我们通过一个变量 `student_num` 来记录已经输入的学生数量,并在达到最大数量时退出输入循环。最后,我们通过另一个循环来输出所有学生的信息。
需要注意的是,这个示例代码中没有输入学生的id信息。如果需要记录学生的id,可以将 `Student` 结构体中加入一个 `int` 类型的 `id` 成员,并在输入时要求用户输入。另外,如果需要记录更多的信息,可以根据需要在 `Student` 结构体中加入其他成员。
这样就完成了用结构体输入学生信息并存入信息库的操作。
相关推荐
![](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)