c语言实现职工管理系统

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

简单的职工管理系统主要实现以下功能:
(1) 新增一名职工:将新增职工对象按姓名以字典方式职工管理文件中.
(2) 删除一名职工:从职工管理文件中删除一名职工对象。

(3)查询:从职工管理文件中查询符合某些条件的职工。

(4)修改:检索某个职工对象,对其某些属性进行修改。

(5)排序:按某种需要对职工对象文件进行排序。

要求:职工信息包括姓名、性别、出生年月、工作年月、学历、职务、住址、电话等信息。

实现提示:
(1)由键盘输入职工对象,以文件方式保存。

程序执行时先将文件读入内存。

(2)对职工对象中的"姓名"按字典顺序进行排序。

(3)对排序后的职工对象进行增、删、查询、修改、排序等操作。

源代码:
#include〈stdio。

h〉
#include<stdlib.h〉
#include<string。

h>
#include <locale.h〉
#define I 100
static char tempstr[20]; /*用于临时存储输入信息的数组*/
static struct staff

int num;
char name[20];
char sex;
long byear;//birth year 出生年月
long iyear;//imploy year 工作年月
char education[20];
char duty[20];
char address[20];
char phone[20];
struct staff *next;
}*head=NULL,*thisn,*newn,sta[I]={0},temp; /*temp用于交换的中间量*/
void cin(void); /*输入信息*/
int identify(int num);/*对职工号是否重复进行验证*/
void cout(void);/*输出信息*/
void sort(void);/*查询排序*/
void del(void); /*删除职工信息*/
int record(void);/*将文件中的数据录入结构体数组中*/
void iyear(int j); /*按工作年月排序*/
void education(int j); /*按学历排序*/
void modify(void); /*修改职工信息*/
void descending(void);/*学历降序输出*/
void ascending(void);/*学历升序输出*/
void printmsg(int i);/*输出信息*/
void seek(void);//查询员工信息
int main(void)

int flag=1;
while(flag)

system(”cls”); /*清屏*/
printf(”欢迎登陆职工信息管理系统!\n”);
printf(" 【1】输入职工信息【2】输出职工信息\n”);printf(”【3】信息查询排序【4】删除职工信息\n");printf(" 【5】修改职工信息【6】查询职工信息\n”);printf("请输入以上数字,选择相应的功能:");
switch (getchar())

case ’1':cin();break;/*调用输入信息的函数*/
case ’2’:cout();break;/*调用输出信息的函数*/
case '3':sort();break; /*调用排序的函数*/
case '4’:del();break; /*调用删除职工信息函数*/
case '5':modify();break; /*调用修改职工信息的函数*/
case '6':seek();break; //查询员工信息
default:flag=0;break; /*跳出循环体,结束程序*/


return 0;
}
void cin(void) /*输入信息的函数*/

FILE *fp;
int receive; /*接收返回值*/
if((fp=fopen("information.txt","a+"))==NULL)/*为读写打开一个文本文件*/

printf(”文件打开错误\n");
exit(1); /*关闭所有文件,终止正在执行的程序,待用户检查出错误,修改后再运行*/
}
newn=(struct staff *)malloc(sizeof(struct staff));
if(head==NULL)/*连接链表*/
{
head=newn;
thisn=newn;
thisn—>next=NULL;

else
{
thisn=head;
while(thisn-〉next!=NULL)
thisn=thisn-〉next;
thisn—>next=newn;
thisn=newn;

getchar(); /*接收缓冲区中选择菜单时多余的回车*/ fputc(’\n’,fp);/*每次记录信息前先换行*/
printf("请输入职工号:");
gets(tempstr);
thisn-〉num=atoi(tempstr); /*atoi()将字符串转换成整型数*/
receive=identify(thisn—>num);
while(receive)
{
printf("请输入职工号:”);
gets(tempstr);thisn->num=atoi(tempstr);
receive=identify(thisn->num);

fputs(tempstr,fp);
fputs(" ",fp); /*信息排版的需要*/
printf("请输入姓名:");
gets(thisn->name);
fputs(thisn->name,fp);
fputs(””,fp);
printf(”请输入性别(填F或M):”);
thisn—〉sex=getchar();getchar();
fputc(thisn-〉sex,fp);
fputs(””,fp);
printf("请输入出生年月(如199001):”);
gets(tempstr);thisn—>byear=atoi(tempstr);
fputs(tempstr,fp);
fputs(" ”,fp);
printf(”请输入工作年月(如199001):");
gets(tempstr);thisn—>iyear=atoi(tempstr);
fputs(tempstr,fp);
fputs(" ",fp);
printf(”请输入学历(博士、硕士、本科、大专、高中、中专、初中、小学、其他):");gets(thisn-〉education);
fputs(thisn—〉education,fp);
fputs(””,fp);
printf(”请输入职务:”);
gets(thisn->duty);
fputs(thisn-〉duty,fp);
fputs(" ",fp);
printf(”请输入地址:");
gets(thisn-〉address);
fputs(thisn—>address,fp);
fputs(" ",fp);
printf("请输入电话号码:”);
gets(thisn-〉phone);
fputs(thisn-〉phone,fp);
thisn—〉next=NULL;
fclose(fp);
system(”cls");/*清屏*/
}
int identify(int num)/*对职工号是否重复进行验证*/

FILE *fp;
int i;
if((fp=fopen("information。

txt",”r"))==NULL)

printf(”文件打开错误”);
exit(1);

for(i=0;i〈I;i++)/*将文件中的信息按行写入结构体数组中*/

fscanf(fp,”%d %s %c %ld %ld %s %s %s %s\n”,&sta[i].num,&sta[i].name,&sta[i]。

sex,&sta[i]。

byear, &sta[i].iyear,&sta[i].education,&sta[i]。

duty,
&sta[i]。

address,&sta[i].phone);
if(sta[i]。

num==num)
{
system("cls”);
printf(”账号已存在【1】重新输入【2】退出系统\n”);
printf("请输入以上数字,选择相应的功能: ");
switch(getchar())
{
case ’1':getchar();return(1); /*输入不成功,返回值作为循环条件*/
case '2’:exit(1); /*退出系统*/


if(sta[i]。

num==0) /*动态限制循环输出的次数*/ break;
}
fclose(fp);
return (0);/*输入成功,返回值作为循环条件*/
void cout(void)/*输出信息*/
{
FILE *fp;
int s,i,j;
system(”cls”);
if((fp=fopen(”information.txt”,"r”))==NULL)
{
printf(”文件打开错误”);
exit(1);
}
for(i=0;i〈I;i++) /*将文件中的信息按行写入结构体数组中并输出*/ {
fscanf(fp,”%d %s %c %ld %ld %s %s %s %s\n”,
&sta[i]。

num,&sta[i]。

name,&sta[i].sex,&sta[i].byear, &sta[i]。

iyear,&sta[i].education,&sta [i]。

duty,
&sta[i].address,&sta[i].phone);
if(sta[i].num==0) /*动态限制循环输出的次数*/
break;
}
s=record();
setlocale (LC_COLLATE, "”);
for (j=0;j<s—1;j++)
for(i=0;i<s—j-1;i++)
{
if(strcoll(sta[i]。

name,sta[i+1]。

name)>0)
{
temp=sta[i];
sta[i]=sta[i+1];
sta[i+1]=temp;

}
printf(”工号姓名性别出生年月工作年月学历职务地址电话\n”);
printf(”-—---—-———---——---———--——-—-—-——-—-——-——-——-—----—-——-——-——-——-————---——--—--\n”);
for(i=0;i<s;i++)
printmsg(i);
fclose(fp);
getchar();/*接收回车*/
printf("\n\n\n\n\n回到主界面请按r 退出系统请按其他键\n”);
switch (getchar())
{
case ’r':getchar();system(”cls”);break;/*getchar()接收回车*/
default:exit(0);
}
}
void sort(void) /*查询排序*/

int j;
getchar(); /*接收回车*/
j=record();
printf(" 【1】按工作年月排序【2】按学历排序\n"); printf(”请输入以上数字,选择相应的功能:”);
switch (getchar())
{
case '1’:iyear(j);break;
case '2':education(j);break;
}
getchar(); /*接收回车*/
getchar();/*接收回车*/
system("cls”);
printf(”【1】回到主界面【2】退出系统\n”);
printf("请输入以上数字,选择相应的功能:”);
switch(getchar())

case '1’:getchar();system("cls”);break;
default:exit(1);

}
void iyear(int j)/*按工作年月排序*/
{
int i,k;/*i,k用于循环体中以及用于结构体下标*/
system("cls");
for(k=0;k〈j—1;k++)
for(i=0;i<=j-k-1;i++)

if(sta[i].iyear〈sta[i+1].iyear)

temp=sta[i];
sta[i]=sta[i+1];
sta[i+1]=temp;


printf(”工号姓名性别出生年月工作年月学历职务地址电话\n");
printf(”——--—----——-——————-—---—--—---—---——--———-----—----—-—-—-—-————---——---—---——\n”);
for(i=0;i<=j—1;i++)
printf("%—5d %—10s %—4c %-4ld %—4ld %-6s %—6s %—20s %—s\n”,
sta[i].num,sta[i]。

name,sta[i]。

sex,sta[i]。

byear, sta[i]。

iyear,sta[i]。

education,sta [i].duty,
sta[i]。

address,sta[i].phone);
printf(”【1】返回上一级【2】退出系统\n”);
printf("请输入以上数字,选择相应的功能:");
switch (getchar())

case '1':system(”cls”);break;
case '2':exit(1);break;

}
void education(int j) /*按学历排序*/
{
FILE *fp;
int i;
if((fp=fopen("information。

txt”,"r”))==NULL)
{
printf("文件打开错误");
exit(1);

for(i=0;i〈I;i++)/*将文件中的信息按行写入结构体数组中*/

if(sta[i].num==0)/*动态限制循环输出的次数*/ break;
fscanf(fp,”%d %s %c %ld %ld %s %s %s %s\n”,&sta[i]。

num,&sta[i].name,&sta[i]。

sex,&sta[i]。

byear,&sta[i]。

iyear,&sta[i].education,&sta[i]。

duty,
&sta[i].address,&sta[i]。

phone);
}
fclose(fp);
getchar();
printf(”【1】学历降序排列【2】学历升序排列\n”);
printf(”请输入以上数字,选择相应的功能:”);
switch(getchar())
{
case ’1':descending();break;
case ’2’:ascending();break;
}
printf(”\n\n按任意键返回上一级”);
}
void del(void)/*删除职工信息*/
{
FILE *fp;
int num,i,j,k;/*num用于输入要删除的职工号,i用于循环体以及数组下标,j 记录职工人数*/
printf(”请输入要删除的职工号:”);
scanf(”%d",&num);
getchar();/*接收回车*/
system(”cls”);
if((fp=fopen(”information。

txt","r”))==NULL)

printf(”文件打开错误”);
exit(1);
}
for(i=0;i<I;i++) /*将文件中的信息按行写入结构体数组中并输出*/
{
fscanf(fp,”%d %s %c %ld %ld %s %s %s %s\n",
&sta[i]。

num,&sta[i]。

name,&sta[i]。

sex,&sta[i]。

byear,&sta[i]。

iyear,&sta[i].education,
&sta[i]。

duty,
&sta[i].address,&sta[i].phone);
if(sta[i]。

num==0)
break;
if(sta[i].num==num)
j=i;
}
fclose(fp);
if((fp=fopen("information.txt”,”w"))==NULL) /*重新向文件输入所有信息*/ {
printf("文件打开错误”);
exit(1);

for(k=j;k〈record();k++)
sta[k]=sta[k+1];
for(i=0;i<record();i++)

sprintf(tempstr,"%d”,sta[i].num);/*将整型数转化成字符串*/ fputs(tempstr,fp);
fputs(””,fp);
fputs(sta[i]。

name,fp);
fputs(" ",fp);
fputc(sta[i]。

sex,fp);
fputs(" ”,fp);
sprintf(tempstr,”%ld",sta[i]。

byear);
fputs(tempstr,fp);
fputs(" ”,fp);
sprintf(tempstr,"%ld",sta[i]。

iyear);
fputs(tempstr,fp);
fputs(" ”,fp);
fputs(sta[i]。

education,fp);
fputs(”",fp);
fputs(sta[i].duty,fp);
fputs(””,fp);
fputs(sta[i].address,fp);
fputs(" ",fp);
fputs(sta[i]。

phone,fp);
fputc('\n',fp);
}
fclose(fp);

int record(void)/*将文件中的数据录入结构体数组中*/
{
FILE *fp;
int i,j; /*j用于记录职工的人数*/
system("cls”);
if((fp=fopen(”information.txt”,"r"))==NULL)

printf(”文件打开错误”);
exit(1);
}
for(i=0,j=0;i<I;i++,j++)/*将文件中的信息按行写入结构体数组中*/

fscanf(fp,"%d %s %c %ld %ld %s %s %s %s\n”, &sta[i].num,&sta[i].name,&sta[i]。

sex,&sta[i]。

byear, &sta[i].iyear,&sta[i].education,&sta[i].duty,
&sta[i]。

address,&sta[i].phone);
if(sta[i]。

num==0)/*动态限制循环输出的次数*/
break;

fclose(fp);
return j;
}
void modify(void) /*修改职工信息*/

FILE *fp;
int i,num,j; /*num是要修改的职工号,j用于计算职工人数*/
getchar(); /*接收回车*/
system(”cls”);
printf("请输入要修改的职工号码: ”);
scanf("%d”,&num);
getchar();
if((fp=fopen("information。

txt”,”r”))==NULL)
{
printf(”文件打开错误");
exit(1);

for(i=0,j=0;i<I;i++,j++) /*将文件中的信息按行写入结构体数组中并输出*/

fscanf(fp,"%d %s %c %ld %ld %s %s %s %s\n”, &sta[i].num,&sta[i].name,&sta[i].sex,&sta[i].byear, &sta[i].iyear,&sta[i].education,&sta [i]。

duty,
&sta[i].address,&sta[i].phone);

for(i=0;i<I;i++)
{ if(sta[i].num==num)

printf(”【1】修改姓名【2】修改学历\n");
printf(" 【3】修改职务【4】修改住址\n");
printf(”【5】修改电话\n”);
printf(”请输入以上数字,选择相应的功能:”);
switch (getchar())

case '1':getchar();
printf(”请输入新姓名:”);
gets(sta[i]。

name);
printf("修改成功!按任意键返回主界面");
getchar();
break;
case ’2’:getchar();
printf(”请输入新学历(博士、硕士、本科、大专、高中、中专、初中、小学、其他):");
gets(sta[i]。

education);
printf("修改成功!按任意键返回主界面”);
getchar();
break;
case '3':getchar();
printf(”请输入新职务:”);
gets(sta[i]。

duty);
printf(”修改成功!按任意键返回主界面”);
getchar();break;
case ’4':getchar();
printf("请输入新地址:”);
gets(sta[i]。

address);
printf(”修改成功!按任意键返回主界面");
getchar();break;
case '5’:getchar();
printf("请输入新电话号码:”);
gets(sta[i]。

phone);
printf(”修改成功!按任意键返回主界面");
getchar();break;
default:break;
}
}
}
fclose(fp);
if((fp=fopen("information。

txt","w"))==NULL)/*重新向文件输入所有信息*/

printf(”文件打开错误");
exit(1);
}
for(i=0;i<j;i++)
{
sprintf(tempstr,”%d",sta[i]。

num);/*将整型数转化成字符串*/ fputs(tempstr,fp);
fputs(”",fp);
fputs(sta[i].name,fp);
fputs(" ”,fp);
fputc(sta[i]。

sex,fp);
fputs(" ",fp);
sprintf(tempstr,"%ld",sta[i]。

byear);
fputs(tempstr,fp);
fputs(””,fp);
sprintf(tempstr,”%ld”,sta[i]。

iyear);
fputs(tempstr,fp);
fputs(”",fp);
fputs(sta[i]。

education,fp);
fputs(" ",fp);
fputs(sta[i]。

duty,fp);
fputs(”",fp);
fputs(sta[i]。

address,fp);
fputs(" ”,fp);
fputs(sta[i].phone,fp);
fputc('\n’,fp);

fclose(fp);
}
void descending(void) /*按降序排列每个职工的学历*/

int i;
system(”cls”);
printf(”工号姓名性别出生年月工作年月学历职务地址电话\n”);
printf("—-—-———-—-—----—-—————-———-—-—--—-—-———-----——----———-—-———--———--—-—--————--\n”);
for(i=0;i<I;i++)
{
if(strcmp(sta[i]。

education,”博士”)==0)

printmsg(i);
}
}
for(i=0;i〈I;i++)

if(sta[i]。

num==0)
break;
if(strcmp(sta[i].education,"硕士")==0)
printmsg(i);

for(i=0;i<I;i++)

if(sta[i].num==0)
break;
if(strcmp(sta[i].education,"本科")==0)
printmsg(i);
}
for(i=0;i〈I;i++)

if(sta[i]。

num==0)
break;
if(strcmp(sta[i]。

education,"大专”)==0)
printmsg(i);

for(i=0;i<I;i++)

if(sta[i]。

num==0)
break;
if(strcmp(sta[i].education,"高中")==0)
printmsg(i);
}
for(i=0;i<I;i++)

if(sta[i].num==0)
break;
if(strcmp(sta[i].education,"中专")==0)
printmsg(i);
}
for(i=0;i<I;i++)

if(sta[i].num==0)
break;
if(strcmp(sta[i].education,"初中”)==0)
printmsg(i);

for(i=0;i<I;i++)

if(sta[i].num==0)
break;
if(strcmp(sta[i].education,”小学”)==0)
printmsg(i);

for(i=0;i〈I;i++)
{
if(sta[i]。

num==0)
break;
if(strcmp(sta[i].education,”其他”)==0)
printmsg(i);


void ascending(void)/*按学历升序排列*/

int i;
system("cls”);
printf("工号姓名性别出生年月工作年月学历职务地址电话\n");
printf(”—---————-—-——-——-——-—--—————-----—-————-———--——-————--—--—-------—————--—-——-\n”);
for(i=0;i〈I;i++)

if(sta[i]。

num==0)
break;
if(strcmp(sta[i].education,"小学”)==0)
printmsg(i);

for(i=0;i〈I;i++)

if(sta[i]。

num==0)
break;
if(strcmp(sta[i]。

education,"初中")==0)
printmsg(i);
}
for(i=0;i<I;i++)
{
if(sta[i].num==0)
break;
if(strcmp(sta[i]。

education,"中专”)==0)
printmsg(i);
}
for(i=0;i<I;i++)

if(sta[i]。

num==0)
break;
if(strcmp(sta[i].education,"高中")==0)
printmsg(i);
}
for(i=0;i<I;i++)
{
if(sta[i].num==0)
break;
if(strcmp(sta[i].education,”大专”)==0)
printmsg(i);

for(i=0;i〈I;i++)

if(sta[i]。

num==0)
break;
if(strcmp(sta[i].education,"本科")==0)
printmsg(i);

for(i=0;i〈I;i++)

if(sta[i]。

num==0)
break;
if(strcmp(sta[i].education,"硕士”)==0)
printmsg(i);
}
for(i=0;i〈I;i++)

if(strcmp(sta[i]。

education,"博士")==0)
{
printmsg(i);
}

for(i=0;i〈I;i++)

if(sta[i].num==0)
break;
if(strcmp(sta[i]。

education,"其他")==0)
printmsg(i);
}
}
void printmsg(int i)/*输出信息*/

printf("%-5d %—10s %—4c %-4ld %-4ld %-6s %-6s %-20s%—s\n",
sta[i].num,sta[i]。

name,sta[i]。

sex,sta[i].byear,sta[i]。

iyear,sta[i]。

education,sta [i].duty,
sta[i].address,sta[i].phone);

void seek() //查询职工信息

FILE *fp;
int i,num,j,c;/*num是要修改的职工号,j用于计算职工人数*/
char nam[20];
system("cls");
if((fp=fopen(”information.txt”,”r”))==NULL)
printf(”文件打开错误");
exit(1);
}
for(i=0,j=0;i<I;i++,j++)/*将文件中的信息按行写入结构体数组中并输出*/
{
fscanf(fp,"%d %s %c %ld %ld %s %s %s %s\n”,
&sta[i]。

num,&sta[i]。

name,&sta[i]。

sex,&sta[i].byear, &sta[i].iyear,&sta[i].education,&sta [i]。

duty,
&sta[i]。

address,&sta[i]。

phone);
}
printf("1.按编号查找2。

按姓名查找\n”);
scanf("%d”,&c);
if(c==1)
{printf(”请输入要查询的职工编号:”);
scanf("%d",&num);
printf("工号姓名性别出生年月工作年月学历职务地址电话\n");
printf("---—-----—-----———--———-—------——--—————--—--———-----——-—-—-—-——--————---—--—\n");
for(i=0;i<I;i++)
{ if(sta[i]。

num==num)
printmsg(i);


else{
printf(”请输入要查询的职工姓名: ");
scanf(”%s”,nam);
printf(”工号姓名性别出生年月工作年月学历职务地址电话\n");
printf("--——---————-———-—--——----——--——-———--—----—————---—————-—---—-———-——----——--—\n”);
for(i=0;i<I;i++)
{ if(strcmp(sta[i]。

name,nam)==0)
printmsg(i);
}

getchar();
printf("\n\n\n\n\n回到主界面请按r 退出系统请按其他键\n");
switch (getchar())
{
case ’r’:getchar();system("cls”);break;/*getchar()接收回车*/
default:exit(0);
}
}。

相关文档
最新文档