给我关于c语言管理系统的代码
时间: 2023-09-23 12:06:35 浏览: 54
### 回答1:
当然,很高兴能够帮到您!以下是一个简单的C语言管理系统的示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LENGTH 100 // 定义最大字符串长度
// 声明结构体
typedef struct {
char name[MAX_LENGTH];
int age;
float salary;
} Employee;
// 函数声明
void addEmployee(Employee *employees, int *employeeCount);
void displayEmployees(Employee *employees, int employeeCount);
// 主函数
int main() {
Employee employees[MAX_LENGTH]; // 创建数组存储员工信息
int employeeCount = 0; // 记录员工数量
// 选择操作
int choice;
do {
printf("\n请选择操作:\n");
printf("1. 添加员工信息\n");
printf("2. 显示所有员工信息\n");
printf("3. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
addEmployee(employees, &employeeCount);
break;
case 2:
displayEmployees(employees, employeeCount);
break;
case 3:
printf("退出程序。\n");
break;
default:
printf("无效的选择,请重试。\n");
break;
}
} while (choice != 3);
return 0;
}
// 添加员工信息
void addEmployee(Employee *employees, int *employeeCount) {
Employee newEmployee;
printf("\n请输入员工姓名:");
scanf("%s", newEmployee.name);
printf("请输入员工年龄:");
scanf("%d", &newEmployee.age);
printf("请输入员工薪水:");
scanf("%f", &newEmployee.salary);
employees[*employeeCount] = newEmployee; // 将新员工添加到数组中
(*employeeCount)++; // 增加员工数量
printf("员工信息已添加。\n");
}
// 显示所有员工信息
void displayEmployees(Employee *employees, int employeeCount) {
printf("\n所有员工信息:\n");
for (int i = 0; i < employeeCount; i++) {
printf("姓名:%s,年龄:%d,薪水:%.2f\n", employees[i].name, employees[i].age, employees[i].salary);
}
}
```
希望这个示例代码能够帮助您理解如何使用C语言来管理员工信息。如果您有任何问题,请随时向我提问。
### 回答2:
下面是一个简单的C语言管理系统的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
char name[100];
int age;
} Student;
void addStudent(Student students[], int *count) {
if (*count >= MAX_SIZE) {
printf("错误:已达到最大学生数。\n");
return;
}
printf("请输入学生的姓名和年龄:\n");
scanf("%s %d", students[*count].name, &students[*count].age);
(*count)++;
}
void displayStudents(Student students[], int count) {
if (count == 0) {
printf("暂无学生信息。\n");
return;
}
printf("学生信息如下:\n");
for (int i = 0; i < count; i++) {
printf("姓名:%s\t年龄:%d\n", students[i].name, students[i].age);
}
}
int main() {
Student students[MAX_SIZE];
int count = 0;
int choice;
while (1) {
printf("请选择操作:\n");
printf("1. 添加学生\n");
printf("2. 显示学生\n");
printf("3. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
addStudent(students, &count);
break;
case 2:
displayStudents(students, count);
break;
case 3:
exit(0);
default:
printf("无效的选择。\n");
break;
}
}
return 0;
}
```
这段代码实现了一个简单的学生管理系统。用户可以选择添加学生信息或显示已有学生的信息。使用一个结构体数组来存储学生信息,每个学生有姓名和年龄两个属性。代码中使用了循环和switch语句来处理用户的选择,通过函数`addStudent`和`displayStudents`来实现添加学生和显示学生信息的功能。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)