课程设计,工资管理系统完整源代码

2024-12-20 10:58:42
推荐回答(1个)
回答1:

#include
#include
#include
#include

#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);

main()
{
char ch;
int m;
STU *head = NULL;

printf("输入要记录的月份(m<10):");
scanf("%d", &m);
while (1)
{
ch = Menu();
switch (ch)
{
case'1':head = Appendsalary(head, m);
Totalsalary(head, m);
break;
case'2':Printsalary(head, m);
break;
case'3':head = Deletesalary(head, m);
printf("\nAfter deleted\n");
Printsalary(head, m);
break;
case'4':Modifysalary(head, m);
Totalsalary(head, m);
printf("\nAfter modified\n");
Printsalary(head, m);
break;
case'5':Searchsalary(head, m);
break;
case'6':Sortsalary(head, m, Descending);
printf("\nsorted in descending order by sum\n");
Printsalary(head, m);
break;
case'7':Sortsalary(head, m, Ascending);
printf("\nsorted in ascending order by sum\n");
Printsalary(head, m);
break;
case'0':exit(0);
DeleteMemory(head);
printf("End of program!");
break;
default:printf("Input error!");
break;
}
}
}

char Menu(void)
{
char ch;

printf("\n管理工人的工资\n");
printf(" 1.添加记录\n");
printf(" 2.列出记录\n");
printf(" 3.删除记录\n");
printf(" 4.修改记录\n");
printf(" 5.查找记录\n");
printf(" 6.降序排列\n");
printf(" 7.升序排列\n");
printf(" 0.退出\n");
printf("请输入你的选择:");
scanf(" %c", &ch); /*在%c前面加一个空格,将存于缓冲区中的回车符读入*/
return ch;
}

STU *Appendsalary(STU *head, const int m)
{
int i = 0;
char c;

do{
head = AppendNode(head, m); /*向链表末尾添加一个节点*/
printf("你想添加一个新的记录吗(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("请输入你想删除的记录编号:");
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("请输入你想修改的记录编号:");
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 {
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 {
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);
}

/*决定数据是否按升序排序,aint 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;
STU *findNode;

printf("请输入你想查找的记录编号:");
scanf("%d", &number);
findNode = SearchNode(head, number);
if (findNode == NULL)
{
printf("Not found!\n");
}
else
{
printf("\nNo.%3d%8s", findNode->number, findNode->name);
for (i=0; 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;

strcat(str, "Number Name ");
for (i=1; i<=m; i++)
{
strcat(str, "salary");
itoa(i,temp, 10);
strcat(str, temp);
strcat(str, " ");
}
strcat(str," sum average");

printf("%s", str); /* 打印表头 */
while (p != NULL) /*若不是表尾,则循环打印*/
{
printf("\nNo.%3d%15s", p->number, p->name);
for (i=0; i {
printf("%7d", p->salary[i]);
}
printf("%9d%9.2f", 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 {
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 {
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指向的当前节点占用的内存*/
}
}
凑合着用吧,一些缺少的功能自己加上去就行了。。。