本文实例为大家分享了C语言链表实现工资管理系统的具体代码,供大家参考,具体内容如下
自己的作业,分享一下,自己为了调试方便,又多加入了一些功能
题目:建立工资管理系统,对职工工资的相关信息进行管理。职工工资相关信息包括职工工号,职工姓名,月份,每月工资和年度总工资等,具体要求如下:
1、建立该系统的存储结构
2、录入职工某个月的工资
3、查找某个职工某个月的工资
4、修改某个职工某个月的工资
5、删除每个职工的工资相关信息
6、统计某个职工年度总工资
7、对职工的月工资或年度总工资进行排名
源码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct worker
{
char no[12]; //职工工号
char name[40]; //姓名
int month[12]; //月份
float gz[12]; //月工资
float total; //年度总工资
struct worker *next; //指向下一节点的指针
} Worker;
//相关的函数声明
Worker *CreateList(Worker *L, int n); //根据输入的职工人数,批量创建节点
void DeleteList(Worker *L, char n[]); //删除节点
void LIstSearch(Worker *L, char n[]); //查找职工工资信息
void InsertInfo(Worker *L); //插入职工工资信息
void SearchMonthSalary(Worker *L, char n[], int m); //查找某个职工的某个月的工资
void Input(Worker *p, int i); //为节点的数据域赋值
void Print(Worker *L); //输出整个链表的数据
void Modify(Worker *L, char n[], int m, float s); //修改某个职工的工资
void menu(); //工资管理系统的菜单
void Save(Worker *L); //将职工的工资信息保存至文件
void Bubble_sort(Worker *L); //冒泡排序实现对链表节点的排序
void StatiTotal(Worker *L,char n[]); //统计某个职工年度总工资
//根据输入的职工人数,批量创建节点
Worker *CreateList(Worker *L, int n) //n为输入的职工人数
{
int i;
for (i = 0; i < n; i++)
{
Worker *p; //将新生成的节点插入到链表中
p = NULL;
p = (Worker *)malloc(sizeof(Worker));
Input(p, i); //为节点的数据域赋值
p->next = L->next;
L->next = p;
}
return L;
}
void DeleteList(Worker *L, char n[]) //按姓名删除职工信息
{
int i;
Worker *p = L->next, *pre = L; //定义p指针指向头节点的指向,定义pre指向头节点,pre始终指向p的前驱节点
if (p == NULL)
printf("数据为空,无法删除!");
else
{
while (strcmp(p->name, n) != 0)
{
pre = p;
p = pre->next;
if (p == NULL)
{
printf("没有找到相关信息,无法删除\n");
return;
}
}
pre->next = p->next;
free(p);
printf("删除成功");
}
}
//向链表中插入职工工资信息
void InsertInfo(Worker *L)
{
int j, k;
Worker *p = NULL;
p = (Worker *)malloc(sizeof(Worker)); //生成一个新节点p
p->total = 0;
printf("请输入要插入的职工的职工工号:");
scanf("%s", &p->no);
printf("请输入要插入的职工的姓名:");
scanf("%s", &p->name);
for (k = 0, j = 1; j <= 12, k < 12; j++, k++)
p->month[k] = j;
printf("请输入要插入的职工的月工资(用空格隔开):");
for (j = 0; j < 12; j++)
{
scanf("%f", &p->gz[j]); //输入每个月的工资
p->total += p->gz[j]; //计算总工资
}
p->next = L->next;
L->next = p;
printf("插入成功!");
}
void LIstSearch(Worker *L, char n[])
{
int i;
Worker *p = L->next;
while (p != NULL)
{
if (strcmp(p->name, n) == 0)
{
printf("\n该职工的工资的信息如下:\n");
printf("职工编号:");
printf("%s\t\n", p->no);
printf("姓名:");
printf("%s\n", p->name);
printf("该职工12个月的月工资如下:");
for (i = 0; i < 12; i++)
printf("%.2f ", p->gz[i]);
printf("\n该职工的年度总工资为:");
printf("%.2f", p->total);
printf("\n\n");
printf("\n");
p = p->next;
}
else
p = p->next;
}
}
void SearchMonthSalary(Worker *L, char n[], int m)
{
int i;
Worker *p = L->next;
while (p != NULL)
{
if (strcmp(p->name, n) == 0)
{
switch (m)
{
case 1:
printf("该职工%d月的工资为%.2f", p->month[0], p->gz[0]);
break;
case 2:
printf("该职工%d月的工资为%.2f", p->month[1], p->gz[1]);
break;
case 3:
printf("该职工%d月的工资为%.2f", p->month[2], p->gz[2]);
break;
case 4:
printf("该职工%d月的工资为%.2f", p->month[3], p->gz[3]);
break;
case 5:
printf("该职工%d月的工资为%.2f", p->month[4], p->gz[4]);
break;
case 6:
printf("该职工%d月的工资为%.2f", p->month[5], p->gz[5]);
break;
case 7:
printf("该职工%d月的工资为%.2f", p->month[6], p->gz[6]);
break;
case 8:
printf("该职工%d月的工资为%.2f", p->month[7], p->gz[7]);
break;
case 9:
printf("该职工%d月的工资为%.2f", p->month[8], p->gz[8]);
break;
case 10:
printf("该职工%d月的工资为%.2f", p->month[9], p->gz[9]);
break;
case 11:
printf("该职工%d月的工资为%.2f", p->month[10], p->gz[10]);
break;
case 12:
printf("该职工%d月的工资为%.2f", p->month[11], p->gz[11]);
break;
default:
break;
}
p = p->next;
}
else
p = p->next;
}
}
void Bubble_sort(Worker *L)//冒泡排序实现对链表节点的排序
{
Worker *p,*q,*tail,*l;
tail = NULL;
while((L->next->next) != tail)
{
p = L;
q = L->next;
while(q->next != tail)
{
if((q->total) > (q->next->total))
{
p->next = q->next;
q->next = q->next->next;
p->next->next = q;
q = p->next;
}
q = q->next;
p = p->next;
}
tail = q;
}
printf("排序完成!年度总工资从小到大结果如下:\n");
l=L->next;
while(l!=NULL)
{
if(l->next!=NULL)
{
printf("%s->",l->name);
l=l->next;
}
else
{
printf("%s",l->name);
l=l->next;
}
}
}
void StatiTotal(Worker *L,char n[]) //统计某个职工年度总工资
{
int i;
Worker *p=L->next;
while(p!=NULL)
{
p->total=0;
if(strcmp(p->name,n)==0)
{
for(i=0;i<12;i++)
p->total+=p->gz[i];
printf("%s的年度总工资为%.2f",n,p->total);
break;
}
else
p=p->next;
}
}
void Modify(Worker *L, char n[], int m, float s)
{
int i,j;
Worker *p = L->next;
while (p != NULL)
{
if (strcmp(p->name, n) == 0)
{
switch (m)
{
case 1:
p->gz[0] = s;
break;
case 2:
p->gz[1] = s;
break;
case 3:
p->gz[2] = s;
break;
case 4:
p->gz[3] = s;
break;
case 5:
p->gz[4] = s;
break;
case 6:
p->gz[5] = s;
break;
case 7:
p->gz[6] = s;
break;
case 8:
p->gz[7] = s;
break;
case 9:
p->gz[8] = s;
break;
case 10:
p->gz[9] = s;
break;
case 11:
p->gz[10] = s;
break;
case 12:
p->gz[11] = s;
break;
default:
break;
}
p->total=0;
for(j=0;j<12;j++)
p->total+=p->gz[j];
p=p->next;
}
else
p = p->next;
}
printf("修改成功!");
}
void Input(Worker *p, int i)
{
int j, k;
p->total = 0;
printf("请输入第%d名职工的职工工号:", i + 1);
scanf("%s", &p->no);
printf("请输入第%d名职工的姓名:", i + 1);
scanf("%s", &p->name);
for (k = 0, j = 1; j <= 12, k < 12; j++, k++)
p->month[k] = j;
printf("请输入第%d名职工的月工资(用空格隔开):", i + 1);
for (j = 0; j < 12; j++)
{
scanf("%f", &p->gz[j]); //输入每个月的工资
p->total += p->gz[j]; //计算总工资
}
}
void Print(Worker *L) //打印所有职工工资信息
{
int i;
Worker *p = L->next;
while (p != NULL)
{
printf("职工编号:");
printf("%s\t\n", p->no);
printf("姓名:");
printf("%s\n", p->name);
printf("该职工12个月的月工资如下:");
for (i = 0; i < 12; i++)
printf("%.2f ", p->gz[i]);
printf("\n该职工的年度总工资为:");
printf("%.2f", p->total);
printf("\n\n");
p = p->next;
}
}
void Save(Worker *L)
{
int i;
Worker *p = L->next;
FILE *fp = fopen("WorkerSalaryInfo.txt", "w");
while (p != NULL)
{
fprintf(fp, "职工编号:");
fprintf(fp, "%s\t\n", p->no);
fprintf(fp, "姓名:");
fprintf(fp, "%s\n", p->name);
fprintf(fp, "该职工12个月的月工资如下:");
for (i = 0; i < 12; i++)
fprintf(fp, "%.2f ", p->gz[i]);
fprintf(fp, "\n该职工的年度总工资为:");
fprintf(fp, "%.2f", p->total);
fprintf(fp, "\n\n");
p = p->next;
}
fclose(fp);
printf("保存成功,已保存至当前目录下的‘WorkerSalaryInfo.txt'文件中");
}
void menu()
{
printf("\t\t\t\t\t \n");
printf("\t\t\t\t\t▔▔▔▔▔▔▔欢迎进入工资管理系统▔▔▔▔▔▔▔\n");
printf("\t\t\t\t\t1.录入职工每个月的工资信息 \n");
printf("\t\t\t\t\t2.按姓名查找某个员工各月的工资 \n");
printf("\t\t\t\t\t3.按姓名查找某个职工的某个月的工资 \n");
printf("\t\t\t\t\t4.修改某个职工某个月的工资 \n");
printf("\t\t\t\t\t5.删除某个职工的相关信息 \n");
printf("\t\t\t\t\t6.插入职工工资信息 \n");
printf("\t\t\t\t\t7.统计某个职工年度总工资 \n");
printf("\t\t\t\t\t8.对职工的年度总工资进行从小到大排名 \n");
printf("\t\t\t\t\t9.输出所有职工工资信息 \n");
printf("\t\t\t\t\t10、将所有职工的工资信息保存至文件 \n");
printf("\t\t\t\t\t0.退出 \n");
printf("\t\t\t\t\t▁▁▁▁▁▁▁▁▁▁谢谢使用▁▁▁▁▁▁▁▁▁\n");
}
int main()
{
int item, n, m; //item用于接收输入的命令,n用于接收输入的职工人数
float s;
char nam[10];
Worker *L = (Worker*)malloc(sizeof(Worker));
L->next=NULL;
do
{
system("cls");
menu();
printf("\n请输入相应的数字,进行相应的操作:\n");
scanf("%d", &item);
switch (item)
{
case 1:
printf("请输入您要录入的职工人数:");
scanf("%d", &n);
L = CreateList(L, n);
getchar();
printf("\n请按任意键返回主菜单\n");
getchar();
break;
case 2:
printf("请输入您要查找的职工姓名:");
scanf("%s", &nam);
LIstSearch(L, nam);
getchar();
printf("\n请按任意键返回主菜单\n");
getchar();
break;
case 3:
printf("请输入您要查找的职工的姓名:");
scanf("%s", &nam);
printf("请输入您要查找该职工第几个月的工资(1到12):");
scanf("%d", &m);
SearchMonthSalary(L, nam, m);
getchar();
printf("\n请按任意键返回主菜单\n");
getchar();
break;
case 4:
printf("请输入您要修改的职工姓名:");
scanf("%s", &nam);
printf("\n请输入您要修改的月份:");
scanf("%d", &m);
printf("\n请输入您修改后的数据:");
scanf("%f", &s);
Modify(L, nam, m, s);
getchar();
printf("\n请按任意键返回主菜单\n");
getchar();
break;
case 5:
printf("请输入您要删除的职工姓名:");
scanf("%s", &nam);
DeleteList(L, nam);
getchar();
printf("\n请按任意键返回主菜单\n");
getchar();
break;
case 6:
InsertInfo(L);
getchar();
printf("\n请按任意键返回主菜单\n");
getchar();
break;
case 7:
printf("请输入您要查询的职工姓名:");
scanf("%s",&nam);
StatiTotal(L,nam);
getchar();
printf("\n请按任意键返回主菜单\n");
getchar();
break;
case 8:
Bubble_sort(L);
getchar();
printf("\n请按任意键返回主菜单\n");
getchar();
break;
case 9:
printf("全部职工的信息如下:\n\n");
Print(L);
getchar();
printf("\n请按任意键返回主菜单\n");
getchar();
break;
case 10:
Save(L);
getchar();
printf("\n请按任意键返回主菜单\n");
getchar();
break;
case 0:
printf("谢谢您使用工资管理系统,即将退出工资管理系统.....");
exit(0);
break;
}
printf("\n\n\n\n");
} while (item);
return 0;
}
部分运行结果截图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。