一:设计一个职工信息管理系统,是指能够实现以下功能:
1.职工信息录入功能
2.职工信息浏览功能
3.职工信息查询(或排序)
4.职工信息删除功能
5.职工信息修改功能
二:需求说明:
1.职工信息包括职工号,姓名,性别,出生年月日,学历,职务,工资,住址,电话等,并且要求职工号不能重复。
2.录入的职工信息要求用文件形式保存,并可以对文件进行浏览、查询、修改、删除等基本操作;
3.职工信息的显示要求有一定的规范格式;
4.对职工信息应能够分别按工资及按学历两种方式进行,要求能返回所有符合条件的职工信息;
5.对职工信息的修改应逐个地进行,一个职工的信息修改不影响其他的职工记录;
6.所涉及的系统应以菜单方式工作,应为用户提供清晰的使用提示。
三:详细设计:
1.数据结构
(1)性别:enum Sex{ male, female }; //枚举类型的变量叫Sex,内含有两个元素也称枚举元素在这里是pencil和female,分别表示铅笔和钢笔。
enum Sex sex;
(2)学历:enum Education{ high, junior, college, master, doctor}; 枚举型变量
enum Education education;
(3)日期:
struct Date
{
int year;
int month;
int day;
};
(4)职工信息:
struct Info
{
char num[5]; /* 职工号 */
char name[8]; /* 姓名 */
enum Sex sex; /* 性别 */
struct Date birthday; /* 出生年月 */
enum Education education; /* 学历 */
char duty[8]; /* 职务 */
double wage; /* 工资 */
char addr[12]; /* 地址 */
char phone[8]; /* 电话 */
};
2.各个功能模块的处理流程
对应于总体设计时的系统功能模块图,各个功能模块的处理流程如下:
(1)信息输入模块
打开职工信息文件;
while ( 继续输入 )
{ 读入职工信息;
将读入信息添加到职工信息文件中;
提示是否继续输入;
}
关闭职工信息文件;
(2)信息浏览模块
打开职工信息文件;
while ( 不到文件结束 )
{
从文件中顺序读出一个职工的信息;
按指定格式输出该职工的信息;
}
关闭职工信息文件;
(3)信息查询模块
while ( 继续查询 )
{
if( 按编号查询 )
{
读入查询的职工编号;
打开职工信息文件;
while( 不到文件结束 )
{
顺序读出一个职工的信息;
if( 该职工信息的编号符合条件 )
输出该职工的信息;
}
关闭职工信息文件;
提示共有几条符合条件的记录;
}
else if( 按姓名查询 )
{
读入查询的职工姓名;
打开职工信息文件;
while( 不到文件结束 )
{
顺序读出一个职工的信息;
if( 该职工信息的姓名符合条件 )
输出该职工的信息;
}
关闭职工信息文件;
提示共有几条符合条件的记录;
}
else
提示输入不合适;
提示是否继续查询;
}
(4)信息修改模块
while( 继续修改 )
{
打开职工信息文件;
打开临时文件;
提示并读入待修改的职工的编号;
while( 不到文件结束 )
{
顺序读出一个职工的信息;
if( 该职工信息的编号符合条件 )
将职工的信息进行修改;
将职工的信息写入临时文件中;
}
关闭原信息文件;
关闭临时文件;
删除原信息文件;
将临时文件的名字改为原信息文件的名字;
}
(5)信息删除模块
while( 继续删除 )
{
提示并读入待删除的职工号;
打开职工信息文件;
查找是否有符合条件的职工记录;
if( 有符合条件的记录 )
{
创建一个新的临时文件
while( 原信息文件中记录未读完 )
{
读出原信息文件中的下一条记录;
if( 此条记录不是待删除记录 )
写入临时文件中去;
}
关闭原信息文件;
关闭临时文件;
删除原信息文件;
将临时文件的名字改为原信息文件的名字;
}
}
else
提示没有符合条件的记录存在;
}
(6)菜单选择模块:
给出信息提示;
清屏;
绘制菜单(包含输入、显示、查询、修改、删除、退出);
提示菜单选择并读入到变量;
返回变量的值
程序如下:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <conio.h> #include <windows.h> enum Sex{male, female}; /* 性别 */ enum Education{high, junior, college, master, doctor}; /* 学历 */ struct Date /* 日期 */ { int year; int month; int day; }; struct Info { char num[10]; /* 职工号 */ char name[15]; /* 姓名 */ enum Sex sex; /* 性别 */ struct Date birthday; /* 出生年月*/ enum Education education; /* 学历 */ char duty[15]; /* 职务 */ double wage; /* 工资 */ char addr[30]; /* 地址 */ char phone[15]; /* 电话 */ }; char menu() /* 菜单选择函数 */ { char n; /* n记录选择的菜单 */ system("cls"); /*清屏*/ puts("\t\t Welcome to employee management system "); puts("\t\t*********************MENU********************\n"); puts("\t\t\t\t1.Append inform\n"); puts("\t\t\t\t2.Display inform\n"); puts("\t\t\t\t3.Search inform\n"); puts("\t\t\t\t4.Modify inform\n"); puts("\t\t\t\t5.Delete inform\n"); puts("\t\t\t\t6.Exit\n"); puts("\t\t*********************************************\n"); printf("Choice your number(1-6):[ ]\b\b"); while(1) { n=getchar(); getchar(); if(n<'1'||n>'6') printf("Input error,please input again(1-6):[ ]\b\b"); else break; } return n; } void append() /* 信息添加函数 */ { struct Info info; FILE * fp; char ch; char temp[10]; if((fp=fopen("inform.txt","ab")) == NULL) { printf("\tCan not open the inform file!"); getch(); exit(1); } do { printf("\tnum:"); gets(info.num); printf("\tname:"); gets(info.name); printf("\tsex(male or female):"); gets(temp); if(!strcmp(temp,"female")) info.sex=female; else info.sex=male; printf("\tbirthday(yyyy/mm/dd):"); scanf("%d/%d/%d",&info.birthday.year,&info.birthday.month,&info.birthday.day); getchar(); printf("\teducation:"); gets(temp); if(!strcmp(temp,"doctor")) info.education=doctor; else if(!strcmp(temp,"master")) info.education=master; else if(!strcmp(temp,"college")) info.education=college; else if(!strcmp(temp,"junior")) info.education=junior; else info.education=high; printf("\tduty:"); gets(info.duty); printf("\twage:"); gets(temp); info.wage=atof(temp); printf("\taddress:"); gets(info.addr); printf("\tphone:"); gets(info.phone); fwrite(&info, sizeof(info), 1, fp); printf("\tAny more?(Y/N):[ ]\b\b"); ch=getchar(); getchar(); }while(ch=='Y'||ch == 'y'); fclose(fp); } void print1() { printf("%-8s%-10s%-8s%-12s%-15s%-8s%-15s%-15s%-10s\n","nun", "name","sex","birthday","education","duty","wage","address","phone"); } void print2(struct Info info) { printf("%-8s%-10s",info.num,info.name); if(info.sex==male) printf("%-8s","male"); else printf("%-8s","female"); printf("%-4d/%-2d/%-4d",info.birthday.year,info.birthday.month,info.birthday.day); if(info.education==high) printf("%-15s","high"); else if(info.education==junior) printf("%-15s","junior"); else if(info.education==college) printf("%-15s","college"); else if(info.education==master) printf("%-15s","master"); else printf("%-15s","doctor"); printf("%-8s%-15.2lf",info.duty,info.wage); printf("%-15s%-10s\n",info.addr,info.phone); } void display() /* 职工信息显示函数 */ { struct Info info; FILE * fp; int total = 0; if((fp=fopen("inform.txt","rb")) == NULL) { printf("\tCan not open the inform file!"); getch(); exit(1); } while(fread(&info, sizeof(info), 1, fp) == 1) { total++; if(total==1) print1(); print2(info); if((total != 0) && (total%5 == 0)) { printf("\n\n\tPress any key to continue......"); getch(); puts("\n\n"); print1(); } } fclose(fp); printf("\n\n\tThere are %d record in all!",total); getch(); } void search() /* 信息查询函数 */ { struct Info info; FILE * fp; int flag; /* flag为1按编号查询,flag为2按姓名查询 */ int total=0; /*记录符合条件的记录的个数 */ char ch[10]; char f; if((fp=fopen("inform.txt","rb")) == NULL) { printf("\tCan not open the inform file!"); getch(); exit(1); } do { rewind(fp); printf("\n\nSearch by(1:num 2:name):[ ]\b\b"); while(1) { scanf("%d",&flag); getchar(); if(flag<1||flag>2) printf("Input error,please input again(1:num 2:name):[ ]\b\b"); else break; } if(flag==1) /* 按编号进行查询 */ { printf("Please input the num you want to search:"); gets(ch); total=0; /* 符合条件的记录数 */ while(fread(&info, sizeof(info), 1, fp) == 1) if(strcmp(ch,info.num)==0) { total++; if(total == 1) print1(); print2(info); } } else /* 按姓名进行查询 */ { printf("Please input the name you want to search:"); gets(ch); total=0; /* 符合条件的记录数 */ while(fread(&info, sizeof(info), 1, fp) == 1) if(strcmp(ch,info.name)==0) { total++; if(total == 1) print1(); print2(info); } } printf("\n\n\tThere are %d record included!\n",total); printf("Search any more?(Y/N):[ ]\b\b"); f=getchar(); getchar(); }while(f=='Y'||f=='y'); fclose(fp); } void modify() /* 信息修改函数*/ { struct Info info; FILE * fp1,*fp2; int flag; char ch[10]; char f; char temp[10]; do { if((fp1=fopen("inform.txt","rb")) == NULL) { printf("\tCan not open the inform file!"); getch(); exit(1); } if((fp2=fopen("temp.txt","wb")) == NULL) { printf("\tCan not creat the temp file!"); getch(); exit(1); } printf("Please input the num you want to modify:"); gets(ch); flag=0; while(fread(&info, sizeof(info), 1, fp1) == 1) { if(strcmp(ch,info.num)==0) { print1(); print2(info); printf("\n\nPlease input the new information:\n"); printf("\tnum:"); gets(info.num); printf("\tname:"); gets(info.name); printf("\tsex(male or female):"); gets(temp); if(!strcmp(temp,"female")) info.sex=female; else info.sex=male; printf("\tbirthday(yyyy/mm/dd):"); scanf("%d/%d/%d",&info.birthday.year,&info.birthday.month,&info.birthday.day); getchar(); printf("\teducation:"); gets(temp); if(!strcmp(temp,"doctor")) info.education=doctor; else if(!strcmp(temp,"master")) info.education=master; else if(!strcmp(temp,"college")) info.education=college; else if(!strcmp(temp,"junior")) info.education=junior; else info.education=high; printf("\tduty:"); gets(info.duty); printf("\twage:"); gets(temp); info.wage=atof(temp); printf("\taddress:"); gets(info.addr); printf("\tphone:"); gets(info.phone); flag=1; break; } fwrite(&info, sizeof(info), 1, fp2); } fclose(fp1); fclose(fp2); if(flag==1) { printf("Modify sucess!\n"); remove("inform.txt"); rename("temp.txt","inform.txt"); } else printf("Can not find this record!\n"); printf("Modify any more?(Y/N):[ ]\b\b"); f=getchar(); getchar(); }while(f=='Y'||f=='y'); } void Delete() /* 信息删除函数 */ { struct Info info; FILE * fp1,*fp2; int flag; char ch[10]; char f; char temp[10]; do { if((fp1=fopen("inform.txt","rb")) == NULL) { printf("\tCan not open the inform file!"); getch(); exit(1); } if((fp2=fopen("temp.txt","wb")) == NULL) { printf("\tCan not creat the temp file!"); getch(); exit(1); } printf("Please input the num you want to delete:"); gets(ch); flag=0; while(fread(&info, sizeof(info), 1, fp1) == 1) { if(strcmp(ch,info.num)==0) { print1(); print2(info); flag=1; break; } else fwrite(&info, sizeof(info), 1, fp2); } fclose(fp1); fclose(fp2); if(flag==1) { printf("Delete sucess!\n"); remove("inform.txt"); rename("temp.txt","inform.txt"); } else printf("Can not find this record!\n"); printf("Delete any more?(Y/N):[]\b\b"); f=getchar(); getchar(); }while(f=='Y'||f=='y'); } int main() { while(1) switch(menu()) { case '1':append();break; case '2':display();break; case '3':search();break; case '4':modify();break; case '5':Delete();break; case '6':exit(0);break; } }