某科技公司有N(N≤20),请你采用模块化程序设计思想,实现一个薪资录入查询系统,实现以下功能:
(1)录入每个员工的工号和薪资;
(2)采用选择排序法,按薪资由高到低输出员工信息表;
(3)采用冒泡排序法,按薪资由低到高输出员工信息表;
(4)采用顺序查找法,按工号查询某位员工的薪资;
(5)采用二分查找法,按薪资查询某位员工的工号;
(6)输出所有员工的工号、薪资以及平均薪资
追加要求:要求程序运行后先显示如下菜单,并提示用户输入选项:
1.Input ID &Salary
2.Selection Sort in desecending order by salary
3.Bubble Sort in ascending order by salary
4.Sequential Search by ID
5.Binary Search by salary
6.List record
0.Exit
Please enter your choice
(用code blocks 编写程序)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#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;
}
排序算法,示例
#include <iostream>
#include <algorithm>
#include <vector>
#include <execution>
Using namespace std;
int main()
{
std::vector<int> Vec{5,3,6,2,7,4,1,8,2,9};
std::sort(std::execution::par, Vec.begin(),Vec.end());
for(auto elm:Vec)
{
cout << elm << “ ”;
}
return 0;
}