工资管理系统源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <windows.h>
#define MONTH_NUM 5 /* 最多的月份*/
struct worker
{
int number; /* 每个工人的工号*/
char name[15]; /* 每个工人的姓名*/
int salary[MONTH_NUM]; /* 每个工人M月的工资*/ int sum; /* 每个工人的总工资*/ float average; /* 每个工人的平均工资*/ struct worker *next; //下一个节点的地址
};
typedef struct worker STU;
char Menu(void);
int Ascending(int a, int b);
int Descending(int a, int b);
void IntSwap(int *pt1, int *pt2);
void CharSwap(char *pt1, char *pt2);
void FloatSwap(float *pt1, float *pt2);
STU *AppendNode(STU *head, const int m);
STU *DeleteNode(STU *head, int nodeNum);
STU *ModifyNode(STU *head, int nodeNum, const int m); STU *SearchNode(STU *head, int nodeNum);
STU *Appendsalary(STU *head, const int m);
void Totalsalary(STU *head, const int m);
void Printsalary(STU *head, const int m);
STU *Deletesalary(STU *head, const int m);
void Modifysalary(STU *head, const int m);
void Sortsalary(STU *head, const int m, int (*compare)(int a, int b));
void Searchsalary(STU *head, const int m);
void DeleteMemory(STU *head);
void numberSwap(STU *head);
main()
{
char ch,b;
int m,a;
STU *head = NULL;
printf("\n\n\n\n\n\n\n\n\n");
printf("``╭╮╭╮╭╮\n");
printf("``│││││└╮\n");
printf("╭┴┴———————┴┴╮~└—╯\n");
printf("││╭—————╮\n");
printf("│││哈│\n");
printf("│●●│╭╮│喽! │\n");
printf("│○╰┬┬┬╯○│o╰╯╰—————╯\n");
printf("│╰—╯│\n");
printf("╰——┬o———o┬——╯\n");
printf(" ╭╮╭╮\n");
printf(" ╰┴————┴╯\n");
Sleep(3000);
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n ≈~☆输入要记录的月份个数(m<10)☆~~`≈: "); scanf("%d", &m);
system("cls");
while (1)
{
ch = Menu();
switch (ch)
{
case'1':head = Appendsalary(head, m);
Totalsalary(head, m);
break;
case'2':Modifysalary(head, m);
Totalsalary(head, m);
printf("\nAfter modified\n");
Printsalary(head, m);
Sleep(1000);
break;
case'3':head = Deletesalary(head, m);
printf("\nAfter deleted\n");
Printsalary(head, m);
break;
case'4':do{
Searchsalary(head, m);
printf("\(≧▽≦)/ 是否要继续查询?Y or N:");
scanf(" %c",&b);
}while(b=='y'||b=='Y');
break;
case'5':Printsalary(head, m);
printf("\(≧▽≦)/ 是否要选择排列方式?Y or N:");
scanf(" %c",&b);
while(b=='y'||b=='Y')
{
printf("你想降序排列还是升序排列?(1降序2升序):");
scanf(" %d",&a);
if(a==1)
{Sortsalary(head, m, Descending);
printf("\nsorted in descending order by sum\n");
Printsalary(head, m);
}
else
{Sortsalary(head, m, Ascending);
printf("\nsorted in ascending order by sum\n");
Printsalary(head, m);
}
printf("O(∩_∩)O 是否继续选择排列方式?Y or N:");
scanf(" %c",&b);
}
break;
case'0':
system("cls");
printf("┴┬┴┬/ ̄\_/ ̄\\n");
printf("┬┴┬┴▏▏▔▔▔▔\\n");
printf("┴┬┴/\/﹨\n");
printf("┬┴∕/)╭—————╮\n"); printf("┴┬▏●▏│再│\n"); printf("┬┴▏▔█◤╭╮│见! │\n"); printf("┴◢██◣\__/o╰╯╰—————╯\n"); printf("┬█████◣/\n");
printf("┴█████████████◣\n");
printf("◢██████████████▆▄\n");
printf("◢██████████████▆▄\n");
printf("█◤◢██◣◥█████████◤\\n");
printf("◥◢████████████◤\\n");
printf("┴███████████◤﹨\n");
printf("┬││█████◤▏\n");
printf("┴││)\n");
printf("┬∕∕/▔▔▔\∕\n");
printf("*∕___/﹨∕\/\\n");
printf("┬┴┬┴┬┴\\_ ﹨/﹨\n");
printf("┴┬┴┬┴┬┴\___\﹨/▔\﹨\n");
exit(0);
DeleteMemory(head);
printf("End of program!");
break;
default:printf("Input error!");
break;
}
}
}
char Menu(void)//主菜单
{
char ch;
system("cls");
printf("\n\n\n\n ━━●●━━━━━━━━━━━管理工人的工资━━━━━━...........(╯3╰)\n");
printf(" 1.添加记录\n");
printf(" 2.更新记录\n");
printf(" 3.删除记录\n");
printf(" 4.查询记录\n");
printf(" 5.统计分析记录\n");
printf(" 0.退出\n");
printf(" ━━━━━━━━━━☆━━━━━━━━━━━━━━━━━☆━━━━━━━━━━\n");
printf("` `.~ ☆~~`≈~☆☆~~`≈~☆☆~~`≈~☆.` `.~\n");
printf("\n请输入你的选择:");
scanf(" %c", &ch); /*在%c前面加一个空格,将存于缓冲区中的回车符读入*/
system("cls");
return ch;
}
STU *Appendsalary(STU *head, const int m)//添加新记录
{
int i = 0;
char c;
do{
head = AppendNode(head, m); /*向链表末尾添加一个节点*/
printf("\n O(∩_∩)O 你想再添加一个新的记录吗(Y/N)?");
scanf(" %c",&c); /*%c前面有一个空格*/
i++;
}while (c=='Y' || c=='y');
printf("%d new nodes have been apended!\n", i);
return head;
}
STU *Deletesalary(STU *head, const int m)//删除记录
{
int i = 0, nodeNum;
char c;
do{
printf("\n ⊙﹏⊙请输入你想删除的记录编号:");
scanf("%d", &nodeNum);
head = DeleteNode(head, nodeNum); /*删除工号为nodeNum的工人信息*/ Printsalary(head, m); /*显示当前链表中的各节点信息*/
printf("Do you want to delete a node(Y/N)?");
scanf(" %c",&c); /*%c前面有一个空格*/
i++;
}while (c=='Y' || c=='y');
printf("%d nodes have been deleted!\n", i);
return head;
}
void Modifysalary(STU *head, const int m)//修改记录
{
int i = 0, nodeNum;
char c;
do{
printf("\n~~~^_^~~~ 请输入你想修改的记录编号:");
scanf("%d", &nodeNum);
head = ModifyNode(head, nodeNum, m); /*修改工号为nodeNum的节点*/ printf("Do you want to modify a node(Y/N)?");
scanf(" %c",&c); /*%c前面有一个空格*/ i++;
}while (c=='Y' || c=='y');
printf("%d nodes have been modified!\n", i);
}
void Totalsalary(STU *head, const int m)
{
STU *p = head;
int i;
while (p != NULL) /*若不是表尾,则循环*/
{
p->sum = 0;
for (i=0; i<m; i++)
{
p->sum += p->salary[i];
}
p->average = (float)p->sum / m;
p = p->next; /*让p指向下一个节点*/
}
}
void Sortsalary(STU *head, const int m, int (*compare)(int a, int b))
{
STU *pt;
int flag = 0, i;
do{
flag = 0 ;
pt = head;
/*若后一个节点的总工资比前一个节点的总工资高,则交换两个节点中的数据注意只交换节点数据,而节点顺序不变,即节点next指针内容不进行交换*/ while (pt->next != NULL)
{
if ((*compare)(pt->next->sum, pt->sum))
{
IntSwap(&pt->number, &pt->next->number);
CharSwap(pt->name, pt->next->name);
for (i=0; i<m; i++)
{
IntSwap(&pt->salary[i], &pt->next->salary[i]);
}
IntSwap(&pt->sum, &pt->next->sum);
FloatSwap(&pt->average, &pt->next->average);
flag = 1;
}
pt = pt->next;
}
}while(flag);
}
/*交换两个整型数*/
void IntSwap(int *pt1, int *pt2)
{
int temp;
temp = *pt1;
*pt1 = *pt2;
*pt2 = temp;
}
/*交换两个实型数*/
void FloatSwap(float *pt1, float *pt2)
{
float temp;
temp = *pt1;
*pt1 = *pt2;
*pt2 = temp;
}
/*交换两个字符串*/
void CharSwap(char *pt1, char *pt2)
{
char temp[15];
strcpy(temp, pt1);
strcpy(pt1, pt2);
strcpy(pt2, temp);
}
/*决定数据是否按升序排序,a<b为真,则按升序排序*/ int Ascending(int a, int b)
{
return a < b;
}
/* 决定数据是否按降序排序,a>b为真,则按降序排序*/ int Descending(int a, int b)
{
return a > b;
}
void Searchsalary(STU *head, const int m)//查询记录{
int number, i,j,k=0,x,t=0;
char str[100] = {'\0'}, temp[3];
STU *findNode;
printf("\n 请输入你想查找的记录编号:");
scanf("%d", &number);
findNode = SearchNode(head, number);
if (findNode == NULL)
{
printf("Not found!\n");
}
else
{
strcat(str, "工号");
for(j=0;j<=strlen(findNode->name);j++)
strcat(str," ");
strcat(str, "姓名");
for (i=1; i<=m; i++)
{
if(findNode->salary[i]/10) t++;
for(x=0;x<2*t;x++)
strcat(str," ");
strcat(str, "工资");
itoa(i,temp, 10);
strcat(str, temp);
}
strcat(str," 已发工资总和");
if(findNode->sum/10) k++;
for(i=0;i<k;i++)
strcat(str," ");
strcat(str," 个人平均工资");
printf("%s", str);
printf("\nNo.%3d %-8s ", findNode->number, findNode->name); for (i=0; i<m; i++)
{
printf("%-7d ", findNode->salary[i]);
}
printf("%9d %9.2f\n", findNode->sum, findNode->average);
}
}
void Printsalary(STU *head, const int m)
{
STU *p = head;
char str[100] = {'\0'}, temp[3];
int i, j = 1,k,x,n=0,t=0;
strcat(str, "工号");
for(k=0;k<=strlen(p->name);k++)
strcat(str," ");
strcat(str, "姓名");
for (i=1; i<=m; i++)
{
if(p->salary[i]/10) t++;
for(x=0;x<2*t;x++)
strcat(str," ");
strcat(str, "工资");
itoa(i,temp, 10);
strcat(str, temp);
}
strcat(str," 已发工资总和");
if(p->sum/10) n++;
for(k=0;k<n;k++)
strcat(str," ");
strcat(str," 个人平均工资");
printf("%s", str); /* 打印表头*/
while (p != NULL) /*若不是表尾,则循环打印*/
{
printf("\nNo.%3d %-8s ", p->number, p->name);
for (i=0; i<m; i++)
{
printf("%-7d ", p->salary[i]);
}
printf("%9d %9.2f\n", p->sum, p->average);
p = p->next; /*让p指向下一个节点*/
j++;
}
printf("\n");
}
STU *AppendNode(STU *head, const int m)
{
STU *p = NULL;
STU *pr = head;
int j;
p = (STU *)malloc(sizeof(STU)); /*为新添加的节点申请内存*/
if (p == NULL) /*若申请内存失败,则打印错误信息,退出程序*/ {
printf("No enough memory to alloc");
exit(0);
}
if (head == NULL) /*若原链表为空表,则将新建节点置为首节点*/ {
head = p;
}
else /*若原链表为非空,则将新建节点添加到表尾*/
{
/*若未到表尾,则继续移动指针pr,直到pr指向表尾*/
while (pr->next != NULL)
{
pr = pr->next;
}
pr->next = p; /*将新建节点添加到链表的末尾*/
}
pr = p; /*让pr指向新建节点*/
printf("Input node data......");
printf("\nInput number:");
scanf("%d", &p->number);
printf("Input name:");
scanf("%s", p->name);
for (j=0; j<m; j++)
{
printf("Input salary%d:", j+1);
scanf("%d", p->salary+j);
}
pr->next = NULL; /*将新建节点置为表尾*/
return head; /*返回添加节点后的链表的头节点指针*/ }
STU *ModifyNode(STU *head, int nodeNum, const int m)
{
int j;
STU *newNode;
newNode = SearchNode(head, nodeNum);
if (newNode == NULL)
{
printf("Not found!\n");
}
else
{
printf("Input the new node data:\n");
printf("Input name:");
scanf("%s", newNode->name);
for (j=0; j<m; j++)
{
printf("Input salary%d:", j+1);
scanf("%d", newNode->salary+j);
}
}
return head;
}
STU *DeleteNode(STU *head, int nodeNum)
{
STU *p = head, *pr = head;
if (head == NULL) /*链表为空,没有节点,无法删除节点*/
{
printf("No Linked Table!\n");
return(head);
}
/*若没找到节点nodeNum且未到表尾,则继续找*/
while (nodeNum != p->number && p->next != NULL)
{
pr = p;
p = p->next;
}
if (nodeNum == p->number) /*若找到节点nodeNum,则删除该节点*/
{
if (p == head) /*若待删节点为首节点,则让head指向第2个节点*/
{
head = p->next;
}
else /*若待删节点非首节点,则将前一节点指针指向当前节点的下一节点*/ {
pr->next = p->next;
}
free(p); /*释放为已删除节点分配的内存*/
}
else /*没有找到待删除节点*/
{
printf("This Node has not been found!\n");
}
return head; /*返回删除节点后的链表的头节点指针*/
}
STU *SearchNode(STU *head, int nodeNum)
{
STU *p = head;
int j = 1;
while (p != NULL) /*若不是表尾,则循环*/
{
if (p->number == nodeNum) return p;
p = p->next; /*让p指向下一个节点*/
j++;
}
return NULL;
}
void DeleteMemory(STU *head)
{
STU *p = head, *pr = NULL;
while (p != NULL) /*若不是表尾,则释放节点占用的内存*/ {
pr = p; /*在pr中保存当前节点的指针*/
p = p->next; /*让p指向下一个节点*/
free(pr); /*释放pr指向的当前节点占用的内存*/
}
}。