用C语言编程,设有若干个人员的数据,其中有学生和教师。学生的数据中包括:姓名、号码、性别、职业、班级。教师的数据包括:姓名、号码、性别、职业、职务。要求输入人员的数据,然后再输出。
时间: 2023-07-10 18:04:26 浏览: 81
以下是用C语言编写的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME_LEN 20
#define MAX_NUM_LEN 10
#define MAX_OCCUPATION_LEN 20
#define MAX_CLASS_LEN 10
#define MAX_POSITION_LEN 20
typedef struct {
char name[MAX_NAME_LEN];
char num[MAX_NUM_LEN];
char gender;
char occupation[MAX_OCCUPATION_LEN];
char class[MAX_CLASS_LEN];
} Student;
typedef struct {
char name[MAX_NAME_LEN];
char num[MAX_NUM_LEN];
char gender;
char occupation[MAX_OCCUPATION_LEN];
char position[MAX_POSITION_LEN];
} Teacher;
void input_student(Student* student);
void input_teacher(Teacher* teacher);
void output_student(const Student* student);
void output_teacher(const Teacher* teacher);
int main() {
int n;
printf("Enter the number of people: ");
scanf("%d", &n);
Student* students = (Student*) malloc(n * sizeof(Student));
Teacher* teachers = (Teacher*) malloc(n * sizeof(Teacher));
printf("Enter the data of %d students:\n", n);
for (int i = 0; i < n; i++) {
input_student(&students[i]);
}
printf("Enter the data of %d teachers:\n", n);
for (int i = 0; i < n; i++) {
input_teacher(&teachers[i]);
}
printf("The data of %d students:\n", n);
for (int i = 0; i < n; i++) {
output_student(&students[i]);
}
printf("The data of %d teachers:\n", n);
for (int i = 0; i < n; i++) {
output_teacher(&teachers[i]);
}
free(students);
free(teachers);
return 0;
}
void input_student(Student* student) {
printf("Enter the name of the student: ");
scanf("%s", student->name);
printf("Enter the number of the student: ");
scanf("%s", student->num);
printf("Enter the gender of the student (M or F): ");
scanf(" %c", &student->gender);
printf("Enter the occupation of the student: ");
scanf("%s", student->occupation);
printf("Enter the class of the student: ");
scanf("%s", student->class);
}
void input_teacher(Teacher* teacher) {
printf("Enter the name of the teacher: ");
scanf("%s", teacher->name);
printf("Enter the number of the teacher: ");
scanf("%s", teacher->num);
printf("Enter the gender of the teacher (M or F): ");
scanf(" %c", &teacher->gender);
printf("Enter the occupation of the teacher: ");
scanf("%s", teacher->occupation);
printf("Enter the position of the teacher: ");
scanf("%s", teacher->position);
}
void output_student(const Student* student) {
printf("Name: %s\n", student->name);
printf("Number: %s\n", student->num);
printf("Gender: %c\n", student->gender);
printf("Occupation: %s\n", student->occupation);
printf("Class: %s\n", student->class);
}
void output_teacher(const Teacher* teacher) {
printf("Name: %s\n", teacher->name);
printf("Number: %s\n", teacher->num);
printf("Gender: %c\n", teacher->gender);
printf("Occupation: %s\n", teacher->occupation);
printf("Position: %s\n", teacher->position);
}
```
运行示例:
```
Enter the number of people: 2
Enter the data of 2 students:
Enter the name of the student: Alice
Enter the number of the student: 1001
Enter the gender of the student (M or F): F
Enter the occupation of the student: Student
Enter the class of the student: ClassA
Enter the name of the student: Bob
Enter the number of the student: 1002
Enter the gender of the student (M or F): M
Enter the occupation of the student: Student
Enter the class of the student: ClassB
Enter the data of 2 teachers:
Enter the name of the teacher: Carol
Enter the number of the teacher: T1001
Enter the gender of the teacher (M or F): F
Enter the occupation of the teacher: Teacher
Enter the position of the teacher: Principal
Enter the name of the teacher: Dave
Enter the number of the teacher: T1002
Enter the gender of the teacher (M or F): M
Enter the occupation of the teacher: Teacher
Enter the position of the teacher: Vice Principal
The data of 2 students:
Name: Alice
Number: 1001
Gender: F
Occupation: Student
Class: ClassA
Name: Bob
Number: 1002
Gender: M
Occupation: Student
Class: ClassB
The data of 2 teachers:
Name: Carol
Number: T1001
Gender: F
Occupation: Teacher
Position: Principal
Name: Dave
Number: T1002
Gender: M
Occupation: Teacher
Position: Vice Principal
```