马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
typedef struct Employee{
int id;
char password[50];
char name[50];
float salary;
float tax;
struct Employee*next;
}Employee;
// 全局变量,用于存储员工链表的头指针
Employee*head=NULL;
// 管理员登录函数
int adminLogin()
{
char password[20];
printf("请输入管理员密码:");
scanf("%s",password);
if(strcmp(password,"2597658468")==0)
{
return 1;
}
else
{
return 0;
}
}
// 录入员工信息并计算税金
void addEmployee()
{
int i,m;
printf("职工人数(1--100):");
scanf("%d",&m);
for(i=0;i<m;i++)
{
Employee*newEmployee =(Employee*)malloc(sizeof(Employee));
printf("请输入员工工号:");
scanf("%d",&newEmployee->id);
printf("请输入员工密码:");
scanf("%s",newEmployee->password);
printf("请输入员工姓名:");
scanf("%s",&newEmployee->name);
printf("请输入员工工资:");
scanf("%f",&newEmployee->salary);
if(newEmployee->salary<=3000)
{
newEmployee->tax=newEmployee->salary*0.03;
}
else if(newEmployee->salary>3000&&newEmployee->salary<=12000)
{
newEmployee->tax=(newEmployee->salary-3000)*0.10;
}
else if(newEmployee->salary>12000&&newEmployee->salary<=25000)
{
newEmployee->tax=(newEmployee->salary-12000)*0.20;
}
else if(newEmployee->salary>25000&&newEmployee->salary<=35000)
{
newEmployee->tax=(newEmployee->salary-25000)*0.25;
}
else if(newEmployee->salary>35000&&newEmployee->salary<=55000)
{
newEmployee->tax=(newEmployee->salary-35000)*0.30;
}
else if(newEmployee->salary>55000&&newEmployee->salary<=80000)
{
newEmployee->tax=(newEmployee->salary-55000)*0.35;
}
else
{
newEmployee->tax=(newEmployee->salary-80000)*0.45;
}
newEmployee->next=head;
head=newEmployee;
}
}
void creat()
{
FILE *fp;
// 使用读入方式打开文件
fp=fopen("employee_list.txt","r");
if(fp==NULL)
{
fp=fopen("employee_list.txt","w");
}
fclose(fp);
}
void save(Employee* head)
{
if(head==NULL)
{
printf("链表为空,保存失败\n");
return;
}
FILE* fp=fopen("employee_list.txt","wb");
if(fp==NULL)
{
printf("打开文件失败\n");
return;
}
Employee* p = head;
while(p!=NULL)
{
fwrite(p, sizeof(Employee), 1, fp);
p = p->next;
}
fclose(fp);
printf("员工信息已保存到文件中\n");
}
// 根据姓名查询员工信息
void serchEmployees()
{
int flag=1;
char name[20];
Employee* p=head;
printf("请输入你要查找的姓名: \n");
scanf("%s",name);
while(p!=NULL&&flag)
{
if(strcmp(name,p->name)==0)
{
flag=0;
}
else
{
p=p->next;
}
}
if(flag==0)
{
printf("工号\t姓名\t工资\n");
printf("%d\t",p->id);
printf("%s\t",p->name);
printf("%.2f\t",p->salary);
}
else
{
printf("没有查询到记录");
}
}
//根据姓名进行修改
void update() {
char name[100]; // 姓名字符串
printf("请输入要修改信息的学生姓名:\n");
scanf("%s", name);
Employee *current = head; // 当前节点指针
// 遍历链表,查找匹配姓名的学生节点
while (current != NULL) {
if (strcmp(current->name, name) == 0) {
printf("请输入新的工号:\n");
scanf("%d", &(current->id));
printf("请输入新的密码:\n");
scanf("%s", current->password);
printf("请输入修改的名字:\n");
scanf("%d", &(current->name));
printf("请输入修改的工资:\n");
scanf("%f", &(current->salary));
printf("修改成功!\n");
return; // 修改成功后直接返回
}
current = current->next;
}
printf("未找到该学生信息!\n");
}
// 显示所有员工的信息
void displayEmployees()
{
Employee* current =head;
while(current !=NULL)
{
printf("工号:%d\t姓名: %s\t工资: %.2f\t税金: %.2f\t",current->id,current->name,current->salary,current->tax);
current=current->next;
}
}
// 统计数目
void displayStatistics()
{
int count=0;
float totalSalary =0;
float totalTax =0;
Employee* current =head;
while(current!=NULL)
{
count++;
totalSalary+=current->salary;
totalTax+=current->tax;
current=current->next;
}
printf("员工数目:%d,工资总数: %.2f,税金总数:%.2f\n",count,totalSalary,totalTax);
}
// 主函数
int main()
{
int choice;
creat();
while(1)
{
printf("\n1.管理员登录\n2.录入员工信息\n3.查找员工信息\n4.修改员工信息\n5.显示所有员工信息\n6.显示统计信息\n7.退出\n");
printf("请输入你的选择:");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(adminLogin())
{
printf("管理员登录成功!");
}
else
{
printf("管理员密码错误!");
}
break;
case 2:addEmployee();break;
case 3:serchEmployees();save(head);break;
case 4:update();break;
case 5:displayEmployees();break;
case 6:displayStatistics();break;
case 7:exit(0);
default:printf("无效的选择!\n");
}
}
return 0;
}
|