3.用定义结构类型的方法,编写程序建立一个具有学生学号、姓名、总分和联系电话的记录文件。编写程序,从键盘上输入一个学号,可以查寻到此人其他信息。C语言程序
时间: 2024-02-26 11:53:05 浏览: 14
好的,以下是一个简单的实现:
```c
#include <stdio.h>
#include <string.h>
#define MAX_STUDENTS 100
struct student {
int id;
char name[20];
int total_score;
char phone[20];
};
int main() {
struct student students[MAX_STUDENTS];
// 假设已经从文件中读取了学生信息并存储在students数组中
int id_to_find;
printf("请输入学生的学号:");
scanf("%d", &id_to_find);
int i;
for (i = 0; i < MAX_STUDENTS; i++) {
if (students[i].id == id_to_find) {
printf("学号:%d\n姓名:%s\n总分:%d\n联系电话:%s\n", students[i].id, students[i].name, students[i].total_score, students[i].phone);
break;
}
}
if (i == MAX_STUDENTS) {
printf("未找到该学生\n");
}
return 0;
}
```
这个程序定义了一个结构体`student`,包含学号、姓名、总分和联系电话四个成员。然后声明了一个`students`数组,用于存储所有学生的信息。程序从键盘上读取一个学号,然后遍历`students`数组,找到该学号对应的学生信息并输出。如果找不到则输出“未找到该学生”。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)