c语言设有若干个人员的数据,其中有学生和教师。学生的数据中包括:姓名、号码、性别、职业、班级。教师的数据包括:姓名、号码、性别、职业、职务。要求输入人员的数据,然后再输出
时间: 2023-07-10 16:04:31 浏览: 142
学生信息管理系统,实现学生信息包括:学号、姓名、性别、年龄、班级等信息。
5星 · 资源好评率100%
以下是一个简单的 C 语言程序,可以实现输入人员数据并输出:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LEN 20
#define MAX_ID_LEN 10
#define MAX_OCCUPATION_LEN 20
#define MAX_TITLE_LEN 20
#define MAX_CLASS_LEN 10
typedef enum { STUDENT, TEACHER } PersonType;
typedef struct {
char name[MAX_NAME_LEN];
char id[MAX_ID_LEN];
char gender;
char occupation[MAX_OCCUPATION_LEN];
char class[MAX_CLASS_LEN];
} Student;
typedef struct {
char name[MAX_NAME_LEN];
char id[MAX_ID_LEN];
char gender;
char occupation[MAX_OCCUPATION_LEN];
char title[MAX_TITLE_LEN];
} Teacher;
void input_student(Student *s) {
printf("Please input student name: ");
scanf("%s", s->name);
printf("Please input student ID: ");
scanf("%s", s->id);
printf("Please input student gender (M/F): ");
scanf(" %c", &s->gender);
printf("Please input student occupation: ");
scanf("%s", s->occupation);
printf("Please input student class: ");
scanf("%s", s->class);
}
void input_teacher(Teacher *t) {
printf("Please input teacher name: ");
scanf("%s", t->name);
printf("Please input teacher ID: ");
scanf("%s", t->id);
printf("Please input teacher gender (M/F): ");
scanf(" %c", &t->gender);
printf("Please input teacher occupation: ");
scanf("%s", t->occupation);
printf("Please input teacher title: ");
scanf("%s", t->title);
}
void print_student(Student s) {
printf("Student name: %s\n", s.name);
printf("Student ID: %s\n", s.id);
printf("Student gender: %c\n", s.gender);
printf("Student occupation: %s\n", s.occupation);
printf("Student class: %s\n", s.class);
}
void print_teacher(Teacher t) {
printf("Teacher name: %s\n", t.name);
printf("Teacher ID: %s\n", t.id);
printf("Teacher gender: %c\n", t.gender);
printf("Teacher occupation: %s\n", t.occupation);
printf("Teacher title: %s\n", t.title);
}
int main() {
int num_students, num_teachers;
printf("Please input the number of students: ");
scanf("%d", &num_students);
printf("Please input the number of teachers: ");
scanf("%d", &num_teachers);
Student *students = malloc(num_students * sizeof(Student));
Teacher *teachers = malloc(num_teachers * sizeof(Teacher));
for (int i = 0; i < num_students; i++) {
printf("Input data for student %d:\n", i+1);
input_student(&students[i]);
}
for (int i = 0; i < num_teachers; i++) {
printf("Input data for teacher %d:\n", i+1);
input_teacher(&teachers[i]);
}
printf("Data for all students:\n");
for (int i = 0; i < num_students; i++) {
print_student(students[i]);
printf("\n");
}
printf("Data for all teachers:\n");
for (int i = 0; i < num_teachers; i++) {
print_teacher(teachers[i]);
printf("\n");
}
free(students);
free(teachers);
return 0;
}
```
这个程序使用了结构体来保存学生和教师的数据,同时定义了两个函数 `input_student()` 和 `input_teacher()` 来分别输入学生和教师的数据,另外还定义了两个函数 `print_student()` 和 `print_teacher()` 来分别输出学生和教师的数据。在 `main()` 函数中,先输入学生和教师的数量,然后根据数量动态分配内存空间,接着依次输入每个学生和教师的数据,并分别输出全部学生和教师的数据。最后释放动态分配的内存空间。
阅读全文