职工信息管理系统

题目如下图(本人严格按照要求+简单)

请添加图片描述
说明一下用到的这几个函数:
fread(void *buffer, size_t size, size_t count, FILE * stream):buffer为接收数据的地址,size为一个单元的大小,count为单元个数,stream为文件流,读取后光标向后移动size*count个字节(就是到没读的位置);
fwrite(void *buffer, size_t size, size_t count, FILE * stream):同上;
feof(FILE * stream): 检测文件结尾,到结尾返回1;
atoi(str):ASCII to integer 的缩写;
atof:ASCII to float 的缩写;

代码:

//职工号 姓名 性别 年龄 学历 工资 住址 电话
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define STF stf[i].num,stf[i].name,stf[i].sex,stf[i].age,stf[i].qua,stf[i].sal,stf[i].ads,stf[i].phone
#define LEN sizeof(struct staff)
struct staff{
	int num;
	char name[9];
	char sex[3];
	int age;
	char qua[9];//qualification学历
	float sal;//salary
	char ads[99];//address
	long phone;
}stf[99];
void menu();//菜单 
void in();//录入 
void browse();//浏览 
void search();//查询 
void _delete();//删除 
void modify();//修改 
int main()
{
	int select;
	system("color f0");//白底黑字 
	while(true)
	{
		system("cls");
		menu();
		scanf("%d",&select);
		switch(select)
		{
			case 1:in();break;
			case 2:browse();break;
			case 3:search();break;
			case 4:_delete();break;
			case 5:modify();break;
			case 0:exit(0);break;
		}
	}
	return 0;
}
void menu() 
{
	puts("\t职工信息管理系统\n"); 
	puts("\t1.信息录入"); 
	puts("\t2.信息浏览"); 
	puts("\t3.信息查询"); 
	puts("\t4.信息删除"); 
	puts("\t5.信息修改"); 
	puts("\t0.退出"); 
}
void in()
{
	FILE *fp;
	int n=0;
	if((fp=fopen("a.txt","a+"))==NULL){
		puts("error");
		return;
	}
	while(!feof(fp)){
		if((fread(&stf[n],LEN,1,fp)))
			n++;
	}
	puts("职工号:");
	scanf("%d",&stf[n].num);
	for(int i=0;i<n;i++){
		if(stf[n].num==stf[i].num){
			puts("存在");
			fclose(fp); 
			return; 
		}
	}
	puts("姓名");
	scanf("%s",stf[n].name);
	puts("性别");
	scanf("%s",stf[n].sex);
	puts("年龄");
	scanf("%d",&stf[n].age);
	puts("学历");
	scanf("%s",stf[n].qua);
	puts("工资");
	scanf("%f",&stf[n].sal);
	puts("住址");
	scanf("%s",stf[n].ads);
	puts("电话");
	scanf("%ld",&stf[n].phone);
	fwrite(&stf[n],LEN,1,fp);
	fclose(fp);
} 
void browse()
{
	FILE *fp;
	int n=0;
	if((fp=fopen("a.txt","r"))==NULL){
		puts("error");
		return;
	}
	while(!feof(fp)){
		if((fread(&stf[n],LEN,1,fp)))
			n++;
	}
	puts("职工号 姓名 性别 年龄 学历 工资 住址 电话");
	if(n==0)
	{
		puts("无信息");
		system("pause");
		return;
	}
	for (int i=0;i<n;i++){
		printf("%-9d %s %s %d %s %.2f %s %ld\n",STF);
	}
	system("pause");
}
void search()
{
	FILE *fp;
	int n=0;
	if((fp=fopen("a.txt","r"))==NULL){
		puts("error");
		return;
	}
	while(!feof(fp)){
		if((fread(&stf[n],LEN,1,fp)))
			n++;
	}
	fclose(fp); 
	char dx[99];
	puts("输入查找的职工号或者姓名:"); 
	scanf("%s",dx);
	for(int i=0;i<n;i++)
	{
		if(atoi(dx)==stf[i].num || strcmp(dx,stf[i].name)==0){
			printf("%-9d %s %s %d %s %.2f %s %ld\n",STF);
			system("pause");return;
		}
	}
	puts("不存在");system("pause");
	
}
void _delete()
{
	FILE *fp;
	int n=0;
	if((fp=fopen("a.txt","r"))==NULL){
		puts("error");
		return;
	}
	while(!feof(fp)){
		if((fread(&stf[n],LEN,1,fp)))
			n++;
	}
	fclose(fp);
	char dx[99];
	puts("输入删除的职工号或者姓名:"); 
	scanf("%s",dx);
	for(int i=0;i<n;i++)
	{
		if(atoi(dx)==stf[i].num || strcmp(dx,stf[i].name)==0){
			for(int j=i;i<n-1;i++)
				stf[i]=stf[i+1];
			n--;
			puts("ok");
		}
	}
	if((fp=fopen("a.txt","w"))==NULL)
	{
		puts("error");
		return;
	}
	for(int i=0;i<n;i++){
		fwrite(&stf[i],LEN,1,fp);
	}
	fclose(fp);
	system("pause");
} 
void modify()
{
	FILE *fp;
	int n=0;
	if((fp=fopen("a.txt","r"))==NULL){
		puts("error");
		return;
	}
	while(!feof(fp)){
		if((fread(&stf[n],LEN,1,fp)))
			n++;
	}
	fclose(fp);
	char dx[99];
	puts("输入修改的职工号:"); 
	scanf("%s",dx);
	getchar();
	for(int i=0;i<n;i++)
	{
		if(atoi(dx)==stf[i].num){ 
			puts("你要修改什么:");
			puts("职工号 姓名 性别 年龄 学历 工资 住址 电话");
			char  d[9];
			scanf("%s",d);
			getchar();
			if(strcmp(d,"职工号")==0)
				scanf("%d",&stf[i].num);
			else if(strcmp("姓名",d)==0)
				scanf("%s",stf[i].name);
			else if(strcmp("性别",d)==0)
				scanf("%s",stf[i].sex);
			else if(strcmp("年龄",d)==0)
				scanf("%d",&stf[i].age);
			else if(strcmp("学历",d)==0)
				scanf("%s",stf[i].qua);
			else if(strcmp("工资",d)==0)
				scanf("%f",&stf[i].name);
			else if(strcmp("住址",d)==0)
				scanf("%s",stf[i].ads);
			else if(strcmp("电话",d)==0)
				scanf("%ld",&stf[i].phone);
			else{
				puts("输入错误");system("pause");return ;
			}
		}
	}	
	if((fp=fopen("a.txt","w"))==NULL)
	{
		puts("error");
		return;
	}
	for(int i=0;i<n;i++){
		fwrite(&stf[i],LEN,1,fp);
	}
	fclose(fp);
	system("pause");
}
  • 30
    点赞
  • 98
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论
职工信息管理系统是一个比较常见的企业内部管理系统,使用Python语言进行开发也是可行的。 以下是一个简单的职工信息管理系统的Python代码示例: ```python # 员工信息类 class Employee: def __init__(self, id, name, department, salary): self.id = id self.name = name self.department = department self.salary = salary def __str__(self): return f"{self.id}\t{self.name}\t{self.department}\t{self.salary}" # 员工信息管理系统类 class EmployeeManagementSystem: def __init__(self): self.employees = [] # 添加员工信息 def add_employee(self, employee): self.employees.append(employee) # 删除员工信息 def delete_employee(self, id): for employee in self.employees: if employee.id == id: self.employees.remove(employee) return True return False # 修改员工信息 def modify_employee(self, id, new_name, new_department, new_salary): for employee in self.employees: if employee.id == id: employee.name = new_name employee.department = new_department employee.salary = new_salary return True return False # 查询员工信息 def search_employee(self, id): for employee in self.employees: if employee.id == id: return employee return None # 显示所有员工信息 def display_all_employees(self): print("ID\tName\tDepartment\tSalary") for employee in self.employees: print(employee) # 测试代码 ems = EmployeeManagementSystem() ems.add_employee(Employee(1, "Alice", "Technology", 10000)) ems.add_employee(Employee(2, "Bob", "Finance", 8000)) ems.add_employee(Employee(3, "Charlie", "Marketing", 12000)) ems.display_all_employees() employee = ems.search_employee(2) if employee: print("Employee found:", employee) else: print("Employee not found.") if ems.modify_employee(3, "David", "Sales", 15000): print("Employee information modified.") else: print("Failed to modify employee information.") if ems.delete_employee(2): print("Employee deleted.") else: print("Failed to delete employee.") ``` 这个示例代码实现了一个简单的员工信息管理系统,包括添加、删除、修改、查询和显示所有员工信息等功能。当然,这只是一个简单的示例,实际上可以根据需求进行更加复杂的开发。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小木荣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值