职工信息管理(C语言实现)

来源:互联网 发布:淘宝退款成功却收到货 编辑:程序博客网 时间:2024/04/28 15:36
#include "string.h"#include "stdio.h"#include "stdlib.h"#include "conio.h"//  定义结构体struct Paid{  char work_num[10];  char work_name[20];  char work_part[50];  int  work_paid;  int  work_age;};//定义包含有Paid结构体的链表typedef struct PaidList{struct Paid p;struct PaidList* next;} *pList;//*******************************************************************//将所有职工信息按工资多少排序void sort(pList pl){pList cur=pl;pList temp;pList next=pl->next;temp=(pList)malloc(sizeof(struct PaidList));while(cur->next!=NULL){while(next!=NULL){if(cur->p.work_paid<next->p.work_paid){temp->p=cur->p;cur->p=next->p;next->p=temp->p;}next=next->next;}cur=cur->next;next=cur->next;}}//****************************************************************//*****************************************************************//输入职工信息void shuru(){// 定义文件类型指针FILE *fp;//声明两个pList链表pList list,next;char f;//初始化链表list=(pList)malloc(sizeof(struct PaidList));list->next=NULL;next=list;//清屏system("cls");    //打开文件fp=fopen("paid.txt","r");if(!fp){printf("不能打开文件");}//如果文件已存在,将文件中的已有数据读入链表中if(fp){while(feof(fp)==0){fscanf(fp,"%s\t%s\t%s\t%d\t%d\n",next->p.work_num ,next->p.work_name,next->p.work_part,&next->p.work_paid,&next->p.work_age);//如果文件指针未到末尾,将链表增加一个节点if(!feof(fp))                              {next->next=(pList)malloc(sizeof(struct PaidList));next=next->next;next->next=NULL;}}}fclose(fp);                              //关闭文件流if(!(fp=fopen("paid.txt","a+")))           //以读写追加模式重新打开文件流{printf("不能打开文件\n");}next->next=(pList)malloc(sizeof(struct PaidList));     //为链表增加一个节点next=next->next;                   next->next=NULL;//判断输入的工号是否已经存在,保证工号的唯一性while(1)  {while(1){pList temp=list;int flag=0;printf("\n请输入职工编号 :");scanf("%s",next->p.work_num );if(list->next==NULL)break;while(temp!=NULL){if(strcmp(temp->p.work_num,next->p.work_num)==0)flag++;temp=temp->next;}if(flag>1){printf("此工号已存在,请查证后再输入!");}elsebreak;}printf("\n请输入职工姓名 :");scanf("%s",next->p.work_name );printf("\n请输入职工部门 :");scanf("%s",next->p.work_part );printf("\n请输入职工工资 :");scanf("%d",&next->p.work_paid  );printf("\n请输入职工工龄 :");scanf("%d",&next->p.work_age  );//判断是否继续录入职工信息printf("\n继续录入职工信息?(Y/N)\n");    f=getch();if('n'==f||'N'==f)break;next->next=(pList)malloc(sizeof(struct PaidList));next=next->next;next->next=NULL;  }fclose(fp);                 //关闭文件流if(!(fp=fopen("paid.txt","w")))       //以写生成的模式重新打开文件以覆盖已有数据{printf("生成文件失败!");}//将所有职工信息按工资排序sort(list);next=list;//将所有职工信息存入磁盘文件中while(next!=NULL){fprintf(fp,"%s\t%s\t%s\t%d\t%d\n",next->p.work_num,next->p.work_name,next->p.work_part,next->p.work_paid,next->p.work_age);next=next->next;}fclose(fp);printf("\n\n数据已保存.\n");//程序暂停system("pause");}//*******************************************************************//显示所有职工信息void display(){FILE *fp;struct Paid p;system("cls");fp=fopen("paid.txt","r");if(!fp){printf("没有职工记录或打开文件失败!");}while(feof(fp)==0){fscanf(fp,"%s\t%s\t%s\t%d\t%d\n",p.work_num ,p.work_name,p.work_part,&p.work_paid,&p.work_age);printf("%s\t%s\t%s\t%d\t%d\n",p.work_num,p.work_name,p.work_part,p.work_paid,p.work_age);}printf("\n\n");system("pause");}//**********************************************************************//**********************************************************************//按照姓名查询职工信息void nameCheck(){FILE* fp;char name[20];struct Paid p;int flag=0;fp=fopen("paid.txt","r");if(!fp){printf("没有职工记录或打开文件失败!");}printf("请输入您想查询的姓名:");scanf("%s",name);printf("工号      姓名       部门       工资        工龄\n");while(!feof(fp)){fscanf(fp,"%s\t%s\t%s\t%d\t%d\n",p.work_num ,p.work_name,p.work_part,&p.work_paid,&p.work_age);if(strcmp(name,p.work_name)==0){printf("%s\t%s\t%s\t%d\t%d\n",p.work_num ,p.work_name,p.work_part,p.work_paid,p.work_age);flag++;}}if(!flag)printf("没有符合条件的职工信息!");system("pause");}//***********************************************************************//***********************************************************************//按照工号查询职工信息void numCheck(){FILE* fp;char num[10];struct Paid p;int flag=0;fp=fopen("paid.txt","r");if(!fp){printf("没有职工记录或打开文件失败!");}printf("请输入您想查询的编号:");scanf("%s",num);printf("工号      姓名       部门       工资        工龄\n");while(!feof(fp)){fscanf(fp,"%s\t%s\t%s\t%d\t%d\n",p.work_num ,p.work_name,p.work_part,&p.work_paid,&p.work_age);if(strcmp(num,p.work_num)==0){flag++;printf("%s\t%s\t%s\t%d\t%d\n",p.work_num ,p.work_name,p.work_part,p.work_paid,p.work_age);}}if(!flag)printf("没有符合条件的职工信息!\n");system("pause");}//**************************************************************************//**************************************************************************//按照部门查询职工信息void partCheck(){FILE* fp;char part[50];struct Paid p;int flag=0;fp=fopen("paid.txt","r");if(!fp){printf("没有职工记录或打开文件失败!");}printf("请输入您想查询的部门:");scanf("%s",part);printf("工号      姓名       部门       工资        工龄\n");while(!feof(fp)){fscanf(fp,"%s\t%s\t%s\t%d\t%d\n",p.work_num ,p.work_name,p.work_part,&p.work_paid,&p.work_age);if(strcmp(part,p.work_part)==0){printf("%s\t%s\t%s\t%d\t%d\n",p.work_num ,p.work_name,p.work_part,p.work_paid,p.work_age);flag++;}}if(!flag)printf("没有符合条件的职工信息!\n");system("pause");}//*****************************************************************************//*****************************************************************************//按照条件查询职工信息void check(){char f;system("cls");printf("\n\n\n\n\n\n");printf("                   1.按姓名查询\n");printf("                   2.按编号查询\n");printf("                   3.按部门查询\n");f=getch();switch(f){case '1':nameCheck();break;case '2':numCheck();break;case '3':partCheck();break;}}//**************************************************************//*********************计算所有职工的平均工资*******************void average(){FILE* fp;struct Paid p;int all=0;double total=0;double avg;system("cls");if(!(fp=fopen("paid.txt","r"))){printf("打开文件失败或没有职工信息记录");return;}while(feof(fp)==0){fscanf(fp,"%s\t%s\t%s\t%d\t%d\n",p.work_num ,p.work_name,p.work_part,&p.work_paid,&p.work_age);all++;total+=p.work_paid;}avg=total/all;printf("所有职工的平均工资是:%lf\n",avg);system("pause");}//*******************************************************************//***************************************************************//欢迎界面void welcom(){printf("\n\n\n\n\n");printf("                      欢迎使用职工信息管理系统!");printf("\n\n\n\n\n");system("pause");}//*****************************************************************//*****************************主函数*********************************int main(){welcom();system("cls");while(1){char f;system("cls");printf("\n\n\n\n\n");printf("                     1.录入职工信息\n");printf("                     2.显示全部职工信息\n");printf("                     3.按条件检索职工信息\n");printf("                     4.显示职工的平均工资\n");while(1){f=getch();if('1'!=f&&'2'!=f&&'3'!=f&&'4'!=f){system("cls");printf("\n\n\n\n\n\n");printf("                                无此选项!\n                            ");system("pause");break;}elsebreak;}switch(f){case '1':shuru();break;case '2':display();break;case '3':check();break;case '4':average();break;}}}

原创粉丝点击