用c语言实现教师工资管理系统,带注解
时间: 2024-03-01 08:56:07 浏览: 115
教师工资管理系统是一个比较简单的系统,可以使用 C 语言来实现。下面是一个简单的教师工资管理系统的示例代码,带有注释说明。
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_TEACHERS 100
struct Teacher {
char name[20];
int salary;
};
struct Teacher teachers[MAX_TEACHERS];
int numTeachers = 0;
// 添加教师信息
void addTeacher() {
if (numTeachers >= MAX_TEACHERS) {
printf("Error: Too many teachers!\n");
return;
}
printf("Please enter teacher's name: ");
scanf("%s", teachers[numTeachers].name);
printf("Please enter teacher's salary: ");
scanf("%d", &teachers[numTeachers].salary);
numTeachers++;
}
// 显示教师信息
void showTeachers() {
if (numTeachers == 0) {
printf("There are no teachers!\n");
return;
}
printf("Name\tSalary\n");
for (int i = 0; i < numTeachers; i++) {
printf("%s\t%d\n", teachers[i].name, teachers[i].salary);
}
}
// 计算总工资
int calculateTotalSalary() {
int total = 0;
for (int i = 0; i < numTeachers; i++) {
total += teachers[i].salary;
}
return total;
}
int main() {
while (1) {
printf("\nTeacher Salary Management System\n");
printf("1. Add teacher\n");
printf("2. Show teachers\n");
printf("3. Calculate total salary\n");
printf("4. Quit\n");
int choice;
printf("Please enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addTeacher();
break;
case 2:
showTeachers();
break;
case 3:
printf("Total salary: %d\n", calculateTotalSalary());
break;
case 4:
exit(0);
break;
default:
printf("Invalid choice!\n");
break;
}
}
return 0;
}
```
该代码定义了一个 `Teacher` 结构体,包含教师的姓名和工资信息,以及一个 `teachers` 数组用于存储所有教师的信息。通过 `addTeacher` 函数可以添加教师信息,通过 `showTeachers` 函数可以显示所有教师信息,通过 `calculateTotalSalary` 函数可以计算所有教师的总工资。在 `main` 函数中,通过一个无限循环来不断接收用户输入,根据用户输入的选择来执行相应的操作。
阅读全文