|
我不知道结果是不是正确的 今天翻磁盘时找到的
当成是练习吧
/*
=======================================================
内容和要求:用菜单实现
1、工资信息:序号、月份、姓名、基本工资、资金、房费、水电费、公积金、实发工资。将
以上信息建立20个学生的信息链表,写入文件。
2、显示如下信息:1、录入工资信息
2、查询 按基本工资查询
按实发工资查询
3、修改 插入指定人员信息
删除调出人员信息
画出程序流程图,编写主函数及子函数,并分析运行结果,调试运行程序成功,撰写课程设
计说明书。
=======================================================
源代码
我的她要。。啊。。
诸位同人。。。都是好心人啊。
谁能忍心。。让俺。。做一辈子光棍啊。。谢谢。。各位了。。
QQ:31504934
alwsse@163.COM
*/
- #include <stdio.h>
- #include <string.h>
- struct Employer_info{
- int No;
- int month;
- char name[50];
- int base_salary;
- int money; //资金什么写
- int house_money; //房费什么写
- int water_money; //
- int sum; //公积金,这个也翻译一下 ,我没有金山词霸
- int real_salary;
- };
- struct list_item{
- struct Employer_info *data;
- struct list_item* next;
- };
- //把employer复制到到链表元素中 并返回这个元素
- struct list_item * add_new_listitem(struct Employer_info*employer)
- {
- if(!employer)
- return NULL;
- struct list_item *p =(struct list_item *) malloc (sizeof(struct list_item ));
- if(!p)
- return NULL;
- p->data=(struct Employer_info *)malloc(sizeof(struct Employer_info));
- if(!p->data){
- free(p);
- return NULL;
- }
- memcpy(p->data,&employer,sizeof(struct Employer_info));
- p->next = NULL;
- return p;
- }
- //------------------------------------------------------------------------------
- // 文件操作
- //------------------------------------------------------------------------------
- //保存到文件
- //写一条记录
- //Employment_info 雇员信息
- static void write_one_rec(FILE* fp,struct Employer_info *employ)
- {
- if(fp&&employ)
- fprintf(fp,"序号=%d\t姓名="%s"\t月份=%d\t基本工资=%d\t奖金=%d\t\
- 房费=%d\t水电费=%d\t公积金=%d\t实发工资=%d\n",
- employ->No,employ->name,employ->month,employ->base_salary,
- employ->money,employ->house_money,employ->water_money,
- employ->sum,employ->real_salary);
- }
- //从文件中读入一个雇员信息
- //返回 0 读入成功
- // 1 可能是因为已到文件尾 或是
- //-1 或是因为文件错误
- static int read_one_rec(FILE* fp,struct Employer_info* employ,int No)
- {
- char strbuf[500],*pos=NULL;
- if(!fp||!employ)
- return -1;
- if(feof(fp))
- return 1;
-
- do{
- memset(strbuf,0,sizeof(strbuf));
- fgets(strbuf,499,fp);
- }while(strbuf[0]=='#'); //#号开始是注释
- /*char *pos=strstr(strbuf,"序号=");
- if(!pos)
- return -1;*/
- employ->No=No;
- pos=strstr(strbuf,"姓名="");
- if(!pos)
- return -1;
- char *pos2=strchr(pos,'"');
- if(!pos2
- ||pos2-pos>49)
- return -1;
- memset(employ->name,0,sizeof(employ->name));
- memcpy(employ->name,pos,pos2-pos);
- pos=strstr(strbuf,"月份=");
- if(!pos)
- return -1;
- employ->month=atoi(pos);
- pos=strstr(strbuf,"基本工资=");
- if(!pos)
- return -1;
- employ->base_salary=atoi(pos);
- pos=strstr(strbuf,"奖金=");
- if(!pos)
- return -1;
- employ->money=atoi(pos);
- pos=strstr(strbuf,"房费=");
- if(!pos)
- return -1;
- employ->house_money=atoi(pos);
- pos=strstr(strbuf,"水电费=");
- if(!pos)
- return -1;
- employ->water_money=atoi(pos);
- pos=strstr(strbuf,"公积金=");
- if(!pos)
- return -1;
- employ->sum=atoi(pos);
- pos=strstr(strbuf,"实发工资=");
- if(!pos)
- return -1;
- employ->real_salary=atoi(pos);
- return 0;
- }
- struct list_item* load_list_from_file(char*filename,int *No)
- {
- struct Employer_info employ;
- struct list_item* phead =NULL ,*pnext = NULL;
- FILE *fp = NULL;
- int res = 0;
- int Seq = 0;
- if(No)
- *No=0;
- if(!filename)
- return NULL;
- fp= fopen(filename,"r");
- if(!fp)
- return NULL;
- do{
- memset(&employ,0,sizeof (struct Employer_info));
- res = read_one_rec(fp,&employ,Seq++);
- if(res)
- break;
- if(!phead){
- phead = add_new_listitem(&employ);
- pnext =phead;
- }else{
- pnext->next = add_new_listitem(&employ);
- pnext = pnext->next;
- }
- }while(1);
- if(No)
- *No=Seq;
- if(res == -1 ){
- printf("file format error\n");
- }
- return phead;
- }
- void save_list_to_file(struct list_item *item,char*filename)
- {
- FILE *fp = NULL;
- if(!filename)
- return ;
- fp = fopen(filename,"w");
- if(!fp)
- return ;
- while(item){
- write_one_rec(fp,item->data);
- item=item->next;
- }
- fclose(fp);
- }
- //------------------------------------------------------------------------------
- // 记录操作
- //------------------------------------------------------------------------------
- //用户录入新记录或是修改记录
- //如果是修改记录那么No==NULL 不然不可为空
- //0 成功 -1失败
- int add_new_rec(struct Employer_info* employ,int* No)
- {
- char answer;
- if(!employ)
- return -1;
- memset(employ,0,sizeof(employ));
- answer=1;
- do{
- printf("请输入姓名,名字不可为空,也不可以含引号和等号,最长49个字符:\bn");
- fgets(employ->name,49,stdin);
- fflush(NULL);
- if((!employ->name[0])||strchr(employ->name,'"')||strchr(employ->name,'=')){
- printf("输入非法,请重输\n");
- answer=0;
- }
- }while(!answer);
- printf("请输入月份、基本工资、奖金:\n");
- scanf("%d%d%d",employ->month,employ->base_salary,employ->money);
- printf("请输入房费、水电费、公积金、实发工资:\n");
- scanf("%d%d%d%d",employ->house_money,employ->water_money,employ->sum,employ->real_salary);
- printf("姓名="%s"\n月份=%d\t基本工资=%d\t奖金=%d\n\
- 房费=%d\t水电费=%d\t公积金=%d\t实发工资=%d\n
- 以上信息正确吗:如果是那么请输入y,敲其它键退回菜单\n",
- employ->name,employ->month,employ->base_salary,
- employ->money,employ->house_money,employ->water_money,
- employ->sum,employ->real_salary);
- scanf("%c",&answer);
- if(answer=='y'||answer=='Y'){
- printf("保存....\n");
- if(No)
- employ->No=++(*No);
- return 0;
- }
- printf("信息没有保存\n");
- return -1;
- }
- //查询信息
- //query_type 查询方式 0 基本工资,非0 实发工资
- //query_num 金额
- void query_info(struct list_item*list)
- {
- int query_type=0, query_num=0;
- struct Employer_info* employ=NULL;
- if(!list){
- printf("没有雇员记录\n");
- return ;
- }
- printf("请输入查询类型(0根据基本工资 其它 根据实发工资)和查询金额:\n");
- scanf("%d%d",&query_type,&query_num);
- while(list){
- if(!list->data)
- break ;
- if(query_type==0&&query_num==list->data->base_salary)
- break;
- else if(query_num==list->data->real_salary)
- break;
- list=list->next;
- }
- if(!list )
- return ;
-
- employ = list->data;
- printf("序号=%d\t姓名="%s"\n月份=%d\t基本工资=%d\t奖金=%d\n\
- 房费=%d\t水电费=%d\t公积金=%d\t实发工资=%d\n
- 敲其它键退回菜单\n",
- employ->No,employ->name,employ->month,employ->base_salary,
- employ->money,employ->house_money,employ->water_money,
- employ->sum,employ->real_salary);
- getch();
- }
- //通过名字定位记录
- struct Employer_info* find_rec_by_name(struct list_item*list)
- {
- char name[50];
- if(!list){
- printf("没有雇员记录\n");
- return NULL;
- }
- do{
- memset(name,0,sizeof(name));
- printf("请输入要操作的雇员名:\n");
- fgets(name,49,stdin);
- fflush(NULL);
- if((!name[0])||strchr(name,'"')||strchr(name,'=')){
- printf("输入非法,请重输\n");
- }
- else
- break;
- }while(1);
- while(list){
- if(!list->data)
- break ;
- if(strcmp(list->data->name,name))
- break;
- list=list->next;
- }
- if(!list ||!list->data){
- printf("没有找到对应记录\n");
- return NULL;
- }
- return list->data;
- }
- //list 雇员信息表
- //oper_type 操作类型 1 添加 2查询 3修改 4删除
- //No 序号
- void employer_oper(struct list_item **list,int oper_type,int *No)
- {
- struct Employer_info employer,*p_employer=NULL;
- struct list_item *plist=NULL;
- int ret;
- switch(oper_type) {
- case 1:
- memset(&employer,0,sizeof(struct Employer_info));
- ret=add_new_rec(&employer,No);
- if(ret)
- break;
- if(!*list){
- *list = add_new_listitem(&employer);
- if(!*list)
- *No--;
- }
- else{
- plist = *list;
- while(plist->next)
- plist=plist->next;
- plist->next = add_new_listitem(&employer);
- if(!plist->next)
- *No--;
- }
- break;
- case 2:
- query_info(*list);
- break;
- case 3:
- p_employer = find_rec_by_name(*list);
- if(p_employer){
- add_new_rec(p_employer,NULL);
- }
- break;
- case 4:
- p_employer = find_rec_by_name(*list);
- if(p_employer){
- plist = *list;
- while(plist->next->data!=p_employer)
- plist=plist->next;
- plist->next = plist->next->next;
- free(plist->next);
- while(plist){
- plist->data->No--;
- plist=plist->next;
- }
- *No--;
- }
- break;
- // case 4:
- // break;
- default:
- break;
- }
- }
- main(int argc,char**argv)
- {
- struct list_item* list = NULL;
- int oper_type =0;
- int No=0;
- if(argc!=2){
- printf ("参数错误:调用方法为:\n\t程序名 数据文件名\n");
- exit(0);
- }
- list = load_list_from_file(argv[1],&No);
- while(oper_type!=5){
- employer_oper(&list,oper_type,No);
- printf("请输入1-5选择操作:\n\
- 1、录入工资信息 \n\
- 2、查询 按基本工资查询 \n\
- 按实发工资查询 \n\
- 3、修改 插入指定人员信息 \n\
- 4. 删除调出人员信息 \n\
- 5. 退出\n");
- scanf("%d",&oper_type);
- }
- save_list_to_file(list,argv[1]);
- }
复制代码 |
|