优化这段代码typedef struct student { int no; //学号 char name[10]; //姓名 double score; //成绩 } ElemType e[10]={{1, "Tom", 70},{2, "Scott", 85},{3, "Mary", 65}, {4, "John", 90},{5, "Charles", 88},{6, "Judy", 92}, {7, "Kevin", 70{8, "Martin", 70},{9, "Amanda", 95}, {10, "Fiona", 68}};
时间: 2023-04-04 10:03:40 浏览: 85
这段代码可以进行优化,可以使用动态内存分配来存储学生信息,这样可以避免固定数组大小的限制。同时,可以使用链表或者树等数据结构来存储学生信息,方便进行增删改查等操作。另外,可以考虑使用结构体指针来操作学生信息,提高代码的效率和可读性。
相关问题
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STUDENTS 100 // 最多支持100个学生 struct date{ int year; int month; int day; }; typedef struct { int num; // 学号 char name[20]; // 姓名 char sex[5]; // 性别 struct date birthday; // 出生日期 int score[3]; // 三门功课成绩 int total; // 总分 double average; // 平均分 } student; student students[MAX_STUDENTS]; // 学生数组 int num_students = 0; // 学生数量
这段代码定义了一个名为student的结构体,其中包含了学生的学号、姓名、性别、出生日期、三门功课成绩、总分和平均分等信息。同时还定义了一个名为date的结构体,用来表示学生的出生日期。
另外还定义了一个名为students的数组来存储学生信息,数组的长度为MAX_STUDENTS,即最多支持100个学生。还定义了一个变量num_students用来记录当前学生数量。
这段代码的作用是定义了一些数据结构和变量,用来存储和处理学生信息。具体的操作需要在其他代码中实现。
定义学生结构体struct Student类型如下: char id[10]; //学号 char Name[20];//姓名 double Score[3];//3门课程成绩 定义并初始化struct Student数组stus[10],显示stus数组数据,输入学生学号,查找该生的数据;删除符合某个条件的所有学生数据;建立struct Student类型的变量并赋值,将该变量插入到stus数组的末尾。用c语言编写
以下是实现上述功能的C语言代码:
```c
#include <stdio.h>
#include <string.h>
typedef struct {
char id[10];
char Name[20];
double Score[3];
} Student;
int main() {
// 初始化学生数组
Student stus[10] = {
{"001", "张三", {90, 88, 92}},
{"002", "李四", {78, 85, 80}},
{"003", "王五", {92, 94, 88}},
{"004", "赵六", {85, 80, 90}},
{"005", "钱七", {88, 90, 86}},
{"006", "孙八", {91, 84, 89}}
};
int n = 6; // 学生个数
// 显示学生数据
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\n");
for (int i = 0; i < n; i++) {
printf("%s\t%s\t%.2f\t%.2f\t%.2f\n", stus[i].id, stus[i].Name, stus[i].Score[0], stus[i].Score[1], stus[i].Score[2]);
}
// 查找学生
char searchId[10];
printf("请输入要查找的学生学号:");
scanf("%s", searchId);
int found = 0;
for (int i = 0; i < n; i++) {
if (strcmp(stus[i].id, searchId) == 0) {
printf("查找结果:\n");
printf("学号:%s\n姓名:%s\n成绩1:%.2f\n成绩2:%.2f\n成绩3:%.2f\n", stus[i].id, stus[i].Name, stus[i].Score[0], stus[i].Score[1], stus[i].Score[2]);
found = 1;
break;
}
}
if (!found) {
printf("未找到该学生!\n");
}
// 删除符合条件的学生数据
double threshold = 90; // 删除成绩大于等于90分的学生
int count = 0;
for (int i = 0; i < n; i++) {
if (stus[i].Score[0] >= threshold && stus[i].Score[1] >= threshold && stus[i].Score[2] >= threshold) {
count++;
} else {
stus[i - count] = stus[i];
}
}
n -= count;
printf("删除符合条件的学生数据后,剩余%d个学生:\n", n);
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\n");
for (int i = 0; i < n; i++) {
printf("%s\t%s\t%.2f\t%.2f\t%.2f\n", stus[i].id, stus[i].Name, stus[i].Score[0], stus[i].Score[1], stus[i].Score[2]);
}
// 插入学生数据
Student newStu = {"007", "周九", {88, 92, 86}};
stus[n] = newStu;
n++;
printf("插入新学生后,共%d个学生:\n", n);
printf("学号\t姓名\t成绩1\t成绩2\t成绩3\n");
for (int i = 0; i < n; i++) {
printf("%s\t%s\t%.2f\t%.2f\t%.2f\n", stus[i].id, stus[i].Name, stus[i].Score[0], stus[i].Score[1], stus[i].Score[2]);
}
return 0;
}
```
这段代码实现了定义并初始化学生结构体数组、显示学生数据、查找学生、删除符合条件的学生数据以及插入新学生数据等功能。其中,通过循环遍历数组并使用strcmp函数进行字符串比较来查找学生;通过循环遍历数组并使用条件语句来删除符合条件的学生;通过将新学生数据赋值给数组的最后一个元素来插入新学生数据。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)