温馨提示:课设要自己去操作,自己写代码,可以借鉴他人代码,学习思路和一些操作,切不可完全抄袭!!!
总体说明:设计一个职员薪资查询系统,要求能录入各职员自身及其工资情况,能读取文件信息、数据的输入、显示、排序、增删查改。
主要功能说明:
1 职员信息的录入功能:添加职员信息到链表中然后再保存到文件;
2 职员查询功能:从键盘中输入一个等级/职务/姓名,在已生成的单链表中查询等级为该等级的职员信息,并将其职员信息输出;
3 职员薪资排序功能:将职员的薪资按高到低进行排序并输出;
4 职员信息输出功能;对职员信息进行输出,将其显示在屏幕上;
5 职员信息修改功能:对职员信息进行修改和删除,然后保存到文件中;
职员信息的录入功能:
添加职员信息
workList headLinkInsert(workList head)//添加数据到表头
{
workNode *p,*s;
p=head->next;
s=(workList)malloc(sizeof(workNode));
s->data=inputOneRecord();
s->next=p;
head->next=s;
return head;
}
void addData(workList head)//添加数据
{
int n,i;
printf("Please input the number that you want to add:\n");
scanf("%d",&n);//添加数据的个数
for(i=0; i<n; i++)
{
head=headLinkInsert(head);
}
}
录入界面:
void inputMune()//录入界面
{
printf("|********************************************|\n");
printf("| 1.添加职员信息 2.返回主界面 |\n");
printf("|********************************************|\n");
}
void inputData(workList head)//1录入职员信息
{
system("cls");
inputMune();
int option;
while(1)
{
printf("Please input you prtion:\n");
scanf("%d",&option);
switch(option)
{
case 1:
addData(head);
head=writeInformation(head);//每队链表进行增删改操作时就保存一次
break;
case 2:
muneOperation(head);
return;
}
}
}
职员信息查询功能
在该系统中查询又分为职员等级查询,职员职务查询,职员姓名查询。
等级查询
workList queryPostion(workList head,char str[])//等级(职位)查找
{
workNode *p,*q;
p=head->next;
q=NULL;
while(p!=NULL)
{
if(strcmp(p->data.postion,str)==0)
{
q=p;
}
p=p->next;
}
return q;
}
void queryPostionData(workList head)//查询等级(职位)
{
char str[20];
workNode *p;
printf("Please input the postion that you want to query:\n");
scanf("%s",str);
p=queryPostion(head,str);
if(p!=NULL)
{
printHeader();
outputOneRecord(p->data);
}
if(p==NULL)
{
printf("The postion is error OR There is not the postion\n");
}
}
职务查询
workList queryPost(workList head,char str[])//职务查找
{
workNode *p,*q;
p=head->next;
q=NULL;
while(p!=NULL)
{
if(strcmp(p->data.post,str)==0)
{
q=p;
}
p=p->next;
}
return q;
}
void queryPostData(workList head)//查询职务
{
char str[20];
workNode *p;
printf("Please input the post that you want to query:\n");
scanf("%s",str);
p=queryPost(head,str);
if(p!=NULL)
{
printHeader();
outputOneRecord(p->data);
}
if(p==NULL)
{
printf("The post is error OR There is not the post\n");
}
}
名字查询
workList queryName(workList head,char str[])//名字查找
{
workNode *p,*q;
p=head->next;
q=NULL;
while(p!=NULL)
{
if(strcmp(p->data.name,str)==0)
{
q=p;
}
p=p->next;
}
return q;
}
void queryNameData(workList head)//查询名字
{
char str[20];
workNode *p;
printf("Please input the name that you want to query:\n");
scanf("%s",str);
p=queryName(head,str);
if(p!=NULL)
{
printHeader();
outputOneRecord(p->data);
}
if(p==NULL)
{
printf("The name is error OR There is not the mane\n");
}
}
查询功能界面
void queryMune()//查询界面
{
printf("|********************************************|\n");
printf("| 1.职员等级查询 2.职员职务查询 |\n");
printf("| 3.职员姓名查询 4.返回主界面 |\n");
printf("|********************************************|\n");
}
void queryData(workList head)//2查询职员信息
{
system("cls");
queryMune();
int option;
while(1)
{
printf("Please input you prtion:\n");
scanf("%d",&option);
switch(option)
{
case 1:
queryPostionData(head);
break;
case 2:
queryPostData(head);
break;
case 3:
queryNameData(head);
break;
case 4:
muneOperation(head);
return;
}
}
}
职员信息修改功能
该功能分为两小功能:1是修改职员信息,2是删除职员信息
1修改职员信息
void reviseOneData(workList head)//修改某个职员的数据,输入需要修改的职员的名字
{
char str[20];
workNode *p;
printf("Please input the name that you want to revise:\n");
scanf("%s",str);
p=queryName(head,str);
if(p!=NULL)
{
p->data=inputOneRecord();
printf("已修改!\n");
}
if(p==NULL)
{
printf("The name is error OR There is not the mane\n");
}
}
2删除职员信息
void deleteOneData(workList head)//删除某个职员的信息,输入需要修改的职员的名字
{
workNode *p,*q;
char str[20];
printf("Please input the name that you want to delete:\n");
scanf("%s",str);
p=head;
while(p->next!=NULL)
{
if(strcmp(p->next->data.name,str)==0)
{
q=p->next;
p->next=p->next->next;
free(q);
}
p=p->next;
}
printf("已删除!\n");
}
修改界面:
void reviseMune()//修改界面
{
printf("|********************************************|\n");
printf("| 1.修改职员信息 2.删除职员信息 |\n");
printf("| 3.返回主界面 |\n");
printf("|********************************************|\n");
}
void reviseData(workList head)//5.修改职员信息
{
system("cls");
reviseMune();
int option;
while(1)
{
printf("Please input you prtion:\n");
scanf("%d",&option);
switch(option)
{
case 1:
reviseOneData(head);
head=writeInformation(head);
break;
case 2:
deleteOneData(head);
head=writeInformation(head);
break;
case 3:
muneOperation(head);
return;
}
}
}
职员薪资排序功能
薪资排序功能:
workList salarySort(workList head)//排序
{
workNode *q,*p,*tail;
tail=NULL;
q=head;
while((head->next->next)!=tail)
{
p=head->next;
q=head;
while(p->next!=tail)
{
if(p->data.totalWages < p->next->data.totalWages)
{
q->next=p->next; //交换节点
p->next=p->next->next;
q->next->next=p;
p=q->next;//回退,因为节点的next值已经改变
}
p=p->next; //再前进一个节点
q=q->next;
}
tail=p;//记录最后的结点
}
return head;
}
薪资排序界面:
void sortMune()//薪资排序界面
{
printf("|********************************************|\n");
printf("| 1.职员薪资排序 2.返回主界面 |\n");
printf("|********************************************|\n");
}
void sortData(workList head)//3.职员薪资排序
{
system("cls");
sortMune();
int option;
while(1)
{
printf("Please input you prtion:\n");
scanf("%d",&option);
switch(option)
{
case 1:
salaryDataSort(head);
break;
case 2:
muneOperation(head);
return;
}
}
}
职员信息输出功能
输出界面:
void outputMune()//输出界面
{
printf("|********************************************|\n");
printf("| 1.输出职员信息 2.返回主界面 |\n");
printf("|********************************************|\n");
}
void outputData(workList head)//4.输出职员信息
{
system("cls");
outputMune();
int option;
while(1)
{
printf("Please input you prtion:\n");
scanf("%d",&option);
switch(option)
{
case 1:
showAllData(head);
break;
case 2:
muneOperation(head);
return;
}
}
}
完整代码
#include
#include
#include
#define LINE "|------------|----------------|----------------|-------------|---------|----------------|\n"
#define KEYWORD "| name | post | postion | basePay | day | totalWages |\n"
#define FORMAT "| %8s |%-15s |%-15s | %8.2f| %3d| %8.2f|\n"
#define DATA data.name,data.post,data.postion,data.basePay,data.day,data.totalWages
//单链表 数据域和指针域分开
struct worker//数据域
{
char name[20];//名字
char post[20];//职务
char postion[20] ;//等级(职位)
float basePay;//基本工资
int day;//出勤(天数)
float totalWages;//总工资=基本工资+出勤天数*50+奖金
};
typedef struct workerNode//指针域
{
struct worker data;
struct workerNode *next;
} workNode,*workList;
void muneOperation(workList head);//主界面
void mune();
float countToalWage(float basePay,int day)//计算总工资
{
float num;
float bonus;//奖金为基本工资的30%
bonus=basePay*0.3;
num=basePay+day*50+bonus;
return num;
}
struct worker inputOneRecord()//输入职工信息
{
struct worker s;
printf("Please input the name:\n");//输入名字
scanf("%s",s.name);
getchar();
printf("Please input the post:\n");//输入职务
scanf("%s",s.post);
getchar();
printf("Please input the postion:\n");//输入等级(职位)
scanf("%s",s.postion);
getchar();
printf("Please input the basePay:\n");//输入基本工资
scanf("%f",&s.basePay);
getchar();
printf("Please input the day:\n");//输入出勤天数
scanf("%d",&s.day);
getchar();
s.totalWages=countToalWage(s.basePay,s.day);//计算总工资
return s;
}
void outputOneRecord(struct worker data)//输出数据
{
printf(FORMAT,DATA);
printf(LINE);
}
void printHeader()//输出表头
{
printf(LINE);
printf(KEYWORD);
printf(LINE);
}
void exportData(workList head)//遍历
{
workNode *p;
p= head->next;
while(p)
{
outputOneRecord(p->data);
p=p->next;
}
}
void showAllData(workList head)//输出所有数据
{
printHeader();
exportData(head);
}
workList readInformation(workList head)//从文件中读取数据并建立链表
{
FILE *fp;
struct worker temp;//定义一个临时变量用来存储从文件中读取的数据
fp=fopen("c:\\课程设计\\职员薪资查询系统.txt","rb");//二进制读取文件
rewind(fp);//将文件内部的位置指针重新指向文件的开头
if(fp==NULL)
{
printf("Error:can't open the file!\n");
exit(0);
}
workNode *p,*q;
head=NULL;
head=(workNode *)malloc(sizeof(workNode));
q=head;
while(fread(&temp,sizeof(struct worker),1,fp))//尾插法建立链表并从文件中读取数据
{
p=(workNode *)malloc(sizeof(workNode));
p->data=temp;
p->next=NULL;
q->next=p;
q=p;
}
rewind(fp);
fclose(fp);
return head;
}
workList writeInformation(workList head)//保存文件
{
FILE *fp;
fp=fopen("c:\\课程设计\\职员薪资查询系统.txt","wb");
if(fp==NULL)
{
printf("Error:can't open the file!\n");
exit(0);
}
workList p;
p=head->next;
while(p!=NULL)//将链表中的数据一个一个存到文件中
{
fwrite(&(p->data),sizeof(struct worker),1,fp);
p=p->next;
}
return head;
}
void inputMune()//录入界面
{
printf("|********************************************|\n");
printf("| 1.添加职员信息 2.返回主界面 |\n");
printf("|********************************************|\n");
}
workList headLinkInsert(workList head)//添加数据到表头
{
workNode *p,*s;
p=head->next;
s=(workList)malloc(sizeof(workNode));
s->data=inputOneRecord();
s->next=p;
head->next=s;
return head;
}
void addData(workList head)//添加数据
{
int n,i;
printf("Please input the number that you want to add:\n");
scanf("%d",&n);//添加数据的个数
for(i=0; i<n; i++)
{
head=headLinkInsert(head);
}
}
void inputData(workList head)//1录入职员信息
{
system("cls");
inputMune();
int option;
while(1)
{
printf("Please input you prtion:\n");
scanf("%d",&option);
switch(option)
{
case 1:
addData(head);
head=writeInformation(head);//每队链表进行增删改操作时就保存一次
break;
case 2:
muneOperation(head);
return;
}
}
}
void queryMune()//查询界面
{
printf("|********************************************|\n");
printf("| 1.职员等级查询 2.职员职务查询 |\n");
printf("| 3.职员姓名查询 4.返回主界面 |\n");
printf("|********************************************|\n");
}
workList queryPostion(workList head,char str[])//等级(职位)查找
{
workNode *p,*q;
p=head->next;
q=NULL;
while(p!=NULL)
{
if(strcmp(p->data.postion,str)==0)
{
q=p;
}
p=p->next;
}
return q;
}
void queryPostionData(workList head)//查询等级(职位)
{
char str[20];
workNode *p;
printf("Please input the postion that you want to query:\n");
scanf("%s",str);
p=queryPostion(head,str);
if(p!=NULL)
{
printHeader();
outputOneRecord(p->data);
}
if(p==NULL)
{
printf("The postion is error OR There is not the postion\n");
}
}
workList queryPost(workList head,char str[])//职务查找
{
workNode *p,*q;
p=head->next;
q=NULL;
while(p!=NULL)
{
if(strcmp(p->data.post,str)==0)
{
q=p;
}
p=p->next;
}
return q;
}
void queryPostData(workList head)//查询等级(职位)
{
char str[20];
workNode *p;
printf("Please input the post that you want to query:\n");
scanf("%s",str);
p=queryPost(head,str);
if(p!=NULL)
{
printHeader();
outputOneRecord(p->data);
}
if(p==NULL)
{
printf("The post is error OR There is not the post\n");
}
}
workList queryName(workList head,char str[])//名字查找
{
workNode *p,*q;
p=head->next;
q=NULL;
while(p!=NULL)
{
if(strcmp(p->data.name,str)==0)
{
q=p;
}
p=p->next;
}
return q;
}
void queryNameData(workList head)//查询名字
{
char str[20];
workNode *p;
printf("Please input the name that you want to query:\n");
scanf("%s",str);
p=queryName(head,str);
if(p!=NULL)
{
printHeader();
outputOneRecord(p->data);
}
if(p==NULL)
{
printf("The name is error OR There is not the mane\n");
}
}
void queryData(workList head)//2查询职员信息
{
system("cls");
queryMune();
int option;
while(1)
{
printf("Please input you prtion:\n");
scanf("%d",&option);
switch(option)
{
case 1:
queryPostionData(head);
break;
case 2:
queryPostData(head);
break;
case 3:
queryNameData(head);
break;
case 4:
muneOperation(head);
return;
}
}
}
void sortMune()//薪资排序界面
{
printf("|********************************************|\n");
printf("| 1.职员薪资排序 2.返回主界面 |\n");
printf("|********************************************|\n");
}
workList salarySort(workList head)//排序
{
workNode *q,*p,*tail;
tail=NULL;//tail指向链表的最后节点
q=head;
while((head->next->next)!=tail)//控制循环次数
{
p=head->next;
q=head;
while(p->next!=tail)
{
if(p->data.totalWages < p->next->data.totalWages)
{
q->next=p->next; //交换节点
p->next=p->next->next;
q->next->next=p;
p=q->next;//回退,因为节点的next值已经改变
}
p=p->next;
q=q->next;
}
tail=p;//记录最后的结点
}
return head;
}
void salaryDataSort(workList head)
{
head=salarySort(head);
showAllData(head);
}
void sortData(workList head)//3.职员薪资排序
{
system("cls");
sortMune();
int option;
while(1)
{
printf("Please input you prtion:\n");
scanf("%d",&option);
switch(option)
{
case 1:
salaryDataSort(head);
break;
case 2:
muneOperation(head);
return;
}
}
}
void outputMune()//输出界面
{
printf("|********************************************|\n");
printf("| 1.输出职员信息 2.返回主界面 |\n");
printf("|********************************************|\n");
}
void outputData(workList head)//4.输出职员信息
{
system("cls");
outputMune();
int option;
while(1)
{
printf("Please input you prtion:\n");
scanf("%d",&option);
switch(option)
{
case 1:
showAllData(head);
break;
case 2:
muneOperation(head);
return;
}
}
}
void reviseMune()//修改界面
{
printf("|********************************************|\n");
printf("| 1.修改职员信息 2.删除职员信息 |\n");
printf("| 3.返回主界面 |\n");
printf("|********************************************|\n");
}
void reviseOneData(workList head)//修改某个职员的数据,输入需要修改的职员的名字
{
char str[20];
workNode *p;
printf("Please input the name that you want to revise:\n");
scanf("%s",str);
p=queryName(head,str);
if(p!=NULL)
{
p->data=inputOneRecord();
printf("已修改!\n");
}
if(p==NULL)
{
printf("The name is error OR There is not the mane\n");
}
}
void deleteOneData(workList head)//删除某个职员的信息,输入需要修改的职员的名字
{
workNode *p,*q;
char str[20];
printf("Please input the name that you want to delete:\n");
scanf("%s",str);
p=head;
while(p->next!=NULL)
{
if(strcmp(p->next->data.name,str)==0)
{
q=p->next;
p->next=p->next->next;
free(q);
}
p=p->next;
}
printf("已删除!\n");
}
void reviseData(workList head)//5.修改职员信息
{
system("cls");
reviseMune();
int option;
while(1)
{
printf("Please input you prtion:\n");
scanf("%d",&option);
switch(option)
{
case 1:
reviseOneData(head);
head=writeInformation(head);
break;
case 2:
deleteOneData(head);
head=writeInformation(head);
break;
case 3:
muneOperation(head);
return;
}
}
}
void mune()
{
printf("|********************************************|\n");
printf("| 1.录入职员信息 2.查询职员信息 |\n");
printf("| 3.职员薪资排序 4.输出职员信息 |\n");
printf("| 5.修改职员信息 6.退出查询系统 |\n");
printf("|********************************************|\n");
}
void muneOperation(workList head)//主界面
{
int option;
system("cls");
mune();
printf("Please input you option:\n");
scanf("%d",&option);
switch(option)
{
case 1:
inputData(head);
break;
case 2:
queryData(head);
break;
case 3:
sortData(head);
break;
case 4:
outputData(head);
break;
case 5:
reviseData(head);
break;
case 6:
return;
}
}
int main()
{
workList head;
head=readInformation(head);
muneOperation(head);
return 0;
}