|
发表于 2023-12-31 23:16:57
|
显示全部楼层
问题出在你定义结构体`teacher`的时候,姓名应该是一个字符串,而不是单个字符。因此,需要修改`name`的数据类型为字符串数组,如下所示:struct teacher{ int id; char name[50]; int age; float salary; char department[50];};同时,请确保你已经正确引入了`stdio.h`和`stdlib.h`头文件。修改之后的代码如下:#include #include struct teacher{ int id; char name[50]; int age; float salary; char department[50];};void insert(struct teacher a, int n, struct teacher newteacher){ int i,j; for(j=0;jnewteacher.id) break; } for(i=n-1;i>=j;i--) { a=a; a=newteacher; }}void output(struct teacher a, int n){ printf("教师信息如下:\n"); for (int i = 0; i < n; i++){ printf("工号:%d,姓名:%s,年龄:%d,工资:%.2f,部门:%s\n", a.id, a.name, a.age, a.salary, a.department); }}void count(struct teacher a, int n, int *young, int *old){ *young = 0; *old = 0; for (int i = 0; i < n; i++){ if (a.age < 35){ (*young)++; } else if(a.age>50){ (*old)++; } }}int main(){ struct teacher teachers[6]={ {1001, "Tom", 35, 5000.00, "Math"}, {1002, "Jerry", 40, 5500.00, "Science"}, {1003, "Ming", 45, 6000.00, "English"}, {1004, "Lisa", 55, 7000.00, "History"}, {1005, "Bob", 30, 4500.00, "PE"}, }; struct teacher newteacher = {1006, "Alex", 38, 5800.00, "Art"}; insert(teachers, 5, newteacher); output(teachers, 6); int young, old; count(teachers, 6, &young, &old); printf("青年教师数量:%d\n", young); printf("老年教师数量:%d\n", old); return 0;}我在主函数中定义了一个包含6位教师信息的数组,并且修复了输入错误的问题。然后,我传入6作为输出函数的参数,并将指针传递给计数函数来统计青年和老年教师的数量。现在应该可以正确地显示结果了。如果还有其他问题,请随时告诉我!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |
|