你是一个资深的C语言设计师,现在需要你设计一个包含教职工的人数 工号,姓名,性别,职位,入职时长,年薪的C语言课程设计的C语言程序
时间: 2024-03-05 07:49:48 浏览: 21
好的,以下是一个简单的包含教职工信息的C语言程序,仅供参考:
```c
#include <stdio.h>
#include <string.h>
#define MAX_EMPLOYEES 100 // 最多可记录的教职工数目
struct Employee {
int id;
char name[20];
char gender;
char position[20];
int years_of_service;
double salary;
};
struct Employee employees[MAX_EMPLOYEES]; // 全局变量,用于记录所有教职工
int num_employees = 0; // 记录当前教职工数目
// 添加教职工
void AddEmployee() {
if (num_employees >= MAX_EMPLOYEES) {
printf("The number of employees has reached the maximum.\n");
return;
}
struct Employee employee;
printf("Please enter the employee's information:\n");
printf("ID: ");
scanf("%d", &employee.id);
printf("Name: ");
scanf("%s", employee.name);
printf("Gender (M/F): ");
scanf(" %c", &employee.gender);
printf("Position: ");
scanf("%s", employee.position);
printf("Years of service: ");
scanf("%d", &employee.years_of_service);
printf("Salary: ");
scanf("%lf", &employee.salary);
employees[num_employees++] = employee;
printf("Employee added successfully.\n");
}
// 显示所有教职工信息
void DisplayAllEmployees() {
printf("ID\tName\tGender\tPosition\tYears of Service\tSalary\n");
for (int i = 0; i < num_employees; i++) {
struct Employee employee = employees[i];
printf("%d\t%s\t%c\t%s\t\t%d\t\t%.2f\n", employee.id, employee.name, employee.gender, employee.position, employee.years_of_service, employee.salary);
}
}
// 查找教职工
void FindEmployee() {
int id;
printf("Please enter the ID of the employee you want to find: ");
scanf("%d", &id);
for (int i = 0; i < num_employees; i++) {
struct Employee employee = employees[i];
if (employee.id == id) {
printf("ID\tName\tGender\tPosition\tYears of Service\tSalary\n");
printf("%d\t%s\t%c\t%s\t\t%d\t\t%.2f\n", employee.id, employee.name, employee.gender, employee.position, employee.years_of_service, employee.salary);
return;
}
}
printf("Employee not found.\n");
}
int main() {
while (1) {
printf("\nPlease select an operation:\n");
printf("1. Add employee\n");
printf("2. Display all employees\n");
printf("3. Find employee by ID\n");
printf("4. Exit\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
AddEmployee();
break;
case 2:
DisplayAllEmployees();
break;
case 3:
FindEmployee();
break;
case 4:
printf("Goodbye!\n");
return 0;
default:
printf("Invalid choice.\n");
break;
}
}
}
```
该程序使用`struct`定义了`Employee`结构体,包含教职工的各项信息。使用全局变量`employees`记录所有教职工的信息,使用变量`num_employees`记录当前教职工数目。
程序提供了三个操作:添加教职工、显示所有教职工信息和查找教职工。用户可以根据提示进行操作。
值得注意的是,因为本程序使用了字符串,所以在输入字符串时需要使用`scanf("%s", str)`的方式。`scanf`会自动忽略空格和换行符。在输入字符时,需要使用`scanf(" %c", &ch)`的方式,其中空格可以忽略用户输入的空格和换行符。