c语言代码 设有若干个人员的数据,其中有学生和教师。学生的数据中包括:姓名、号码、性别、职业、班级。教师的数据包括:姓名、号码、性别、职业、职务。要求输入人员的数据,然后再输出。
时间: 2023-07-10 16:04:36 浏览: 79
以下是一个简单的 C 语言代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 50
#define MAX_NUM_LEN 20
#define MAX_OCCUPATION_LEN 20
#define MAX_CLASS_LEN 10
#define MAX_POST_LEN 20
struct Person {
char name[MAX_NAME_LEN];
char number[MAX_NUM_LEN];
char sex[MAX_OCCUPATION_LEN];
char occupation[MAX_OCCUPATION_LEN];
};
struct Student {
struct Person person;
char class[MAX_CLASS_LEN];
};
struct Teacher {
struct Person person;
char post[MAX_POST_LEN];
};
int main() {
int i, n;
printf("请输入人员数量:");
scanf("%d", &n);
struct Student students[n];
struct Teacher teachers[n];
for (i = 0; i < n; i++) {
printf("请输入第 %d 个人的信息(学生:姓名 号码 性别 职业 班级,教师:姓名 号码 性别 职业 职务):", i + 1);
char name[MAX_NAME_LEN], number[MAX_NUM_LEN], sex[MAX_OCCUPATION_LEN], occupation[MAX_OCCUPATION_LEN];
scanf("%s %s %s %s", name, number, sex, occupation);
if (strcmp(occupation, "学生") == 0) {
char class[MAX_CLASS_LEN];
scanf("%s", class);
strcpy(students[i].person.name, name);
strcpy(students[i].person.number, number);
strcpy(students[i].person.sex, sex);
strcpy(students[i].person.occupation, occupation);
strcpy(students[i].class, class);
} else if (strcmp(occupation, "教师") == 0) {
char post[MAX_POST_LEN];
scanf("%s", post);
strcpy(teachers[i].person.name, name);
strcpy(teachers[i].person.number, number);
strcpy(teachers[i].person.sex, sex);
strcpy(teachers[i].person.occupation, occupation);
strcpy(teachers[i].post, post);
} else {
printf("职业输入错误!\n");
exit(1);
}
}
printf("\n输出人员信息:\n");
for (i = 0; i < n; i++) {
if (strcmp(students[i].person.occupation, "学生") == 0) {
printf("姓名:%s 号码:%s 性别:%s 职业:%s 班级:%s\n",
students[i].person.name, students[i].person.number, students[i].person.sex,
students[i].person.occupation, students[i].class);
} else if (strcmp(teachers[i].person.occupation, "教师") == 0) {
printf("姓名:%s 号码:%s 性别:%s 职业:%s 职务:%s\n",
teachers[i].person.name, teachers[i].person.number, teachers[i].person.sex,
teachers[i].person.occupation, teachers[i].post);
}
}
return 0;
}
```
这个代码使用了结构体来表示学生和教师的数据,输入时根据职业进行区分,输出时也根据职业进行区分。
阅读全文