用c语言代码实现以下功能:基本资料数据的维护管理 (1).添加:(批量、单个)添加员工基本资料数据。基本资料数据包括职工号、姓名、性别、部 门、岗位、工龄。 (2).修改:根据职工号来修改任意员工的除职工号外的其他基本资料数据。 (3).删除:根据职工号删除一个员工。 (4).查询:根据职工号查询一个员工的基本资料数据。

时间: 2024-03-08 21:48:56 浏览: 15
好的,以下是一个示例代码,它演示了如何通过 C 语言来实现基本资料数据的维护管理,包括添加、修改、删除和查询。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_EMPLOYEES 100 // 定义员工结构体 struct Employee { int id; // 职工号 char name[50]; // 姓名 char gender[10]; // 性别 char department[50]; // 部门 char position[50]; // 岗位 int years_of_service; // 工龄 }; // 定义全局变量,存储员工信息 struct Employee employees[MAX_EMPLOYEES]; int num_employees = 0; // 声明函数 void add_employee(); void add_employees(); void modify_employee(); void delete_employee(); void query_employee(); void print_employee(struct Employee employee); int main() { int choice; while (1) { printf("\n1. Add employee\n"); printf("2. Add employees\n"); printf("3. Modify employee\n"); printf("4. Delete employee\n"); printf("5. Query employee\n"); printf("6. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); switch (choice) { case 1: add_employee(); break; case 2: add_employees(); break; case 3: modify_employee(); break; case 4: delete_employee(); break; case 5: query_employee(); break; case 6: exit(0); default: printf("Invalid choice.\n"); } } return 0; } // 添加单个员工 void add_employee() { struct Employee employee; printf("\nEnter employee ID: "); scanf("%d", &employee.id); printf("Enter employee name: "); scanf("%s", employee.name); printf("Enter employee gender: "); scanf("%s", employee.gender); printf("Enter employee department: "); scanf("%s", employee.department); printf("Enter employee position: "); scanf("%s", employee.position); printf("Enter employee years of service: "); scanf("%d", &employee.years_of_service); employees[num_employees] = employee; num_employees++; printf("Employee added successfully.\n"); } // 添加多个员工 void add_employees() { int num_employees_to_add; printf("\nEnter the number of employees to add: "); scanf("%d", &num_employees_to_add); for (int i = 0; i < num_employees_to_add; i++) { printf("\nEnter employee #%d information:\n", i + 1); add_employee(); } } // 修改员工信息 void modify_employee() { int id_to_modify; int found = 0; printf("\nEnter employee ID to modify: "); scanf("%d", &id_to_modify); for (int i = 0; i < num_employees; i++) { if (employees[i].id == id_to_modify) { printf("Enter employee name: "); scanf("%s", employees[i].name); printf("Enter employee gender: "); scanf("%s", employees[i].gender); printf("Enter employee department: "); scanf("%s", employees[i].department); printf("Enter employee position: "); scanf("%s", employees[i].position); printf("Enter employee years of service: "); scanf("%d", &employees[i].years_of_service); printf("Employee modified successfully.\n"); found = 1; break; } } if (!found) { printf("Employee not found.\n"); } } // 删除员工 void delete_employee() { int id_to_delete; int found = 0; printf("\nEnter employee ID to delete: "); scanf("%d", &id_to_delete); for (int i = 0; i < num_employees; i++) { if (employees[i].id == id_to_delete) { for (int j = i; j < num_employees - 1; j++) { employees[j] = employees[j + 1]; } num_employees--; printf("Employee deleted successfully.\n"); found = 1; break; } } if (!found) { printf("Employee not found.\n"); } } // 查询员工信息 void query_employee() { int id_to_query; int found = 0; printf("\nEnter employee ID to query: "); scanf("%d", &id_to_query); for (int i = 0; i < num_employees; i++) { if (employees[i].id == id_to_query) { print_employee(employees[i]); found = 1; break; } } if (!found) { printf("Employee not found.\n"); } } // 打印员工信息 void print_employee(struct Employee employee) { printf("\nEmployee ID: %d\n", employee.id); printf("Employee name: %s\n", employee.name); printf("Employee gender: %s\n", employee.gender); printf("Employee department: %s\n", employee.department); printf("Employee position: %s\n", employee.position); printf("Employee years of service: %d\n", employee.years_of_service); } ``` 在上面的示例代码中,我们首先定义了一个员工结构体来表示员工的基本资料数据。然后,我们定义了一个全局变量 `employees` 数组来存储所有员工的信息,以及一个变量 `num_employees` 来记录员工数量。接下来,我们实现了四个函数,分别用于添加单个员工、添加多个员工、修改员工信息和删除员工。最后,我们实现了一个查询员工信息的函数,并且使用 `switch` 语句来调用这些函数。 请注意,上面的代码仅提供了一个示例。在实际应用中,您可能需要添加更多的错误检查和输入验证,以确保输入的数据是有效和安全的。同时,您需要将员工信息保存到文件中,并在程序启动时从文件中读取员工信息。

相关推荐

最新推荐

recommend-type

C++语言数据结构 串的基本操作实例代码

主要介绍了C语言数据结构 串的基本操作实例代码的相关资料,需要的朋友可以参考下
recommend-type

用C语言实现从文本文件中读取数据后进行排序的功能

是一个十分可靠的程序,这个程序的查错能力非常强悍。程序包含了文件操作,归并排序和字符串输入等多种技术。对大家学习C语言很有帮助,有需要的一起来看看。
recommend-type

单片机C语言程序设计:用计数器中断实现100以内的按键计数

名称:用计数器中断实现 100 以内的按键计数 说明:本例用 T0 计数器中断实现按键技术,由于计数寄存器初值为 1,因此 P3.4 引脚的每次负跳变都会触发 T0 中断,实现计数值累加。计数器的清零用外部中断 0 控制。
recommend-type

学生成绩管理系统(数据结构)实验报告.docx

期末实践周必备(数据结构)学生成绩管理系统,基于C语言完成的,百分百作者原创,资源保证!
recommend-type

C语言实现简单航班管理系统

主要为大家详细介绍了C语言实现简单航班管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。