提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
目录
前言
主要目的是构建一个功能完善的教师信息管理系统,能实现对教师信息进行录入、删除、查询、浏览及排序的功能,同时可以将处理好的教师信息保存到文件中,并在之后打开系统时自动读取之前保存的教师信息。主要的实现方式包括对结构体数组的增删改查,多种系统命令调用、命令验证等。
一、系统总体方案的设计
1.系统总体功能结构
2.数据结构描述
2.1结构体数组
所谓结构体数组,是指数组中的每个元素都是一个结构体。在实际应用中,C语言结构体数组常被用来表示一个拥有相同数据结构的群体,比如一个班的学生、一个车间的职工等。
结构体可以存储不同的数据类型,将他们互相联系起来。结构体数组可以连续存储多个结构体,和数组作用相似。比如想定义同一个最小外接矩形的四个坐标值,并给予这个矩形一个特征编号。当需要存储多个最小外接矩形的信息时,就需要动态申请一个结构体数组。
struct 结构体类型名{类型名 成员名;类型名 成员名;……},先声明结构体类型,再定义结构体变量名。声明结构体类型,不分配空间,定义结构体类型变量,就要分配内存空间。
2.2文件
文件是性质相同的记录的集合。顺序文件是指按记录进入文件的先后顺序存放、其逻辑顺序和物理顺序一致的文件。注意:一切存储在顺序存取存储器(如磁带)上的文件,都只能是顺序文件。
3.函数调用关系
二、模块功能分析
1.主模块功能函数
1.1主函数
这是整个系统的主函数,所有功能都是通过这个函数实现的。
int main()
{
data a;
int n,length;
length=0;
if(Login()==1)
{
Open(a,length);
do
{
UI();
scanf("%d",&n);
switch (n)
{
case 1:Entry(a,length);break;
case 2:Del(a,length);break;
case 3:Browse(a,length);break;
case 4:Save(a,length);break;
case 5:Inquire(a,length);break;
case 6:sort(a,length);break;
default:putchar('\a');
}
} while (n!=0);
}
else printf(" 三次输入密码错误,已自动退出系统\n");
printf(" ");
return 0;
}
2.项目功能模块
2.1登录模块
该模块在系统启动时运行,主要采用字符验证判断,使用一一对应的方式进行验证。
int Login(void)
{
int i=3;
int k=0;
char a[100];
char b[100]="在这里写一个你的系统的密码";
printf("\n\n\n\n\n\n");
printf(" * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" * *\n");
printf(" * 你真的非常棒学院教师信息管理系统 *\n");
printf(" * *\n");
printf(" * * * * * * * * * * * * * * * * * * * * * * *\n");
do
{
printf(" 密码:");
scanf("%s",a);
if(strcmp(a,b)==0)
{
return (1);
}
else k++;
if (k!=3) printf(" 输入错误,请重新输入(再输入%d次错误自动退出系统)\n",i-k);
} while (k!=3);
return 0;
}
2.2录入模块
本模块实现的功能是将录入的信息存入到结构体数组中,录入结束后可以选择继续录入下一名教师的信息或将已录入的教师信息通过保存模块保存到文件中,也可以选择返回主界面进行其他操作。
void Entry(data &a,int &n)
{
int m;
do
{
system("cls");
printf("\n\n\n");
printf(" * * * * * * * * * * 教师信息录入 * * * * * * * * * *\n");
printf(" 请输入要录入的教师的信息:\n");
printf(" 教师号:");
scanf("%s",a[n].num);
printf(" 姓名:");
scanf("%s",a[n].name);
printf(" 性别:");
scanf("%s",a[n].gender);
printf(" 年龄:");
scanf("%d",&a[n].age);
printf(" 学历:");
scanf("%s",a[n].education);
printf(" 职称:");
scanf("%s",a[n].title);
printf(" 工资:");
scanf("%d",&a[n].wage);
printf(" 住址:");
scanf("%s",a[n].address);
printf(" 电话:");
scanf("%s",a[n].phone);
printf("\n");
n++;
printf(" 该名教师的信息输入完毕,请选择进行以下哪项操作\n");
printf(" 1.继续录入教师信息\n");
printf(" 2.返回首页\n");
printf(" 3.保存已录入教师信息\n");
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" ");
scanf("%d",&m);
if(m==3)
{
Save(a,n);
system("cls");
printf("\n\n\n");
printf(" * * * * * * * * * * 教师信息录入 * * * * * * * * * *\n");
printf(" 保存成功,请选择进行以下哪项操作\n");
printf(" 1.继续录入教师信息\n");
printf(" 2.返回首页\n");
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" ");
scanf("%d",&m);
}
}while (m!=2);
}
2.3删除模块
模块实现的功能是能通过输入的教师号查询教师信息并输出该名教师的信息,然后由操作确认教师信息后再选择是否删除。
void Del(data &a,int &n)
{
system("cls");
printf("\n\n\n");
printf(" * * * * * * * * * * * * * * * * 教师信息删除 * * * * * * * * * * * * * * * *\n");
int i,j,k,m;
char number[50];
printf(" 请输入要删除教师信息的教师号:");
scanf("%s",number);
for (i=0;i<n;i++)
{
if(strcmp(a[i].num,number)==0)
{
printf(" 以下是这名教师的信息:\n");
printf(" 教师号 姓名 性别 年龄 学历 职称 工资 住址 电话 \n");
printf(" %-10s%-7s%-5s%-5d%-8s%-8s%-8d%-11s%-10s\n",a[i].num,a[i].name,a[i].gender,a[i].age,a[i].education,a[i].title,a[i].wage,a[i].address,a[i].phone);
j=1;
m=i;
break;
}
else
{
j=0;
m=999;
}
}
if (j==1)
{
printf(" 是否要删除该名教师的信息(请输入1或2)\n");
printf(" 1.是\n");
printf(" 2.否\n");
printf(" ");
scanf("%d",&k);
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
if(k==1)
{
for (i=m;i<n-1;i++)
{
strcpy(a[i].num,a[i+1].num);
strcpy(a[i].name,a[i+1].name);
strcpy(a[i].gender,a[i+1].gender);
a[i].age=a[i+1].age;
strcpy(a[i].education,a[i+1].education);
strcpy(a[i].title,a[i+1].title);
a[i].wage=a[i+1].wage;
strcpy(a[i].address,a[i+1].address);
strcpy(a[i].phone,a[i+1].phone);
}
n--;
printf(" 删除成功,回车以返回首页\n");
}
else
{
if (k==2)
{
printf(" 取消删除,回车以返回首页\n");
}
else
{
printf(" 指令输入错误,回车以返回首页\n");
}
}
}
else printf(" 未查询到该名教师的信息\n");
getchar();
printf(" ");
getchar();
}
2.4查询模块
本模块实现的功能是由操作者选择查询方式来分别运行Lquire_1和Lquire_2这两个函数来查询教师信息。
void Inquire(data a,int n)
{
int style;
system("cls");
printf("\n\n\n");
printf(" * * * * * * * * * * * * * * * * 教师信息查询 * * * * * * * * * * * * * * * *\n");
printf(" 请选择以哪种方式进行查询(输入1或2):\n");
printf(" 1.按教师号进行查询\n");
printf(" 2.按职称进行查询\n ");
scanf("%d",&style);
switch (style)
{
case 1:Inquire_1(a,n);break;
case 2:Inquire_2(a,n);break;
default:printf(" 输入错误指令,查询失败!\n");
}
printf("\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" 回车以返回首页\n");
getchar();
printf(" ");
getchar();
}
Lquire_1函数:
void Inquire_1(data a,int n)
{
int i;
int j=0;
char number[50];
printf(" 请输入要查询教师信息的教师号:");
scanf("%s",number);
for (i=0;i<n;i++)
{
if(strcmp(a[i].num,number)==0) j=1;
}
if(j==1)
{
printf(" 查询到以下信息:\n");
printf(" 教师号 姓名 性别 年龄 学历 职称 工资 住址 电话 \n");
for (i=0;i<n;i++)
{
if(strcmp(a[i].num,number)==0)
{
printf(" %-10s%-7s%-5s%-5d%-8s%-8s%-8d%-11s%-10s\n",a[i].num,a[i].name,a[i].gender,a[i].age,a[i].education,a[i].title,a[i].wage,a[i].address,a[i].phone);
}
}
}
else printf("\n 未查询到该名教师的信息\n");
}
Lquire_2函数:
void Inquire_2(data a,int n)
{
int i;
int j=0;
char t[50];
printf(" 请输入要查询教师信息的职称:");
scanf("%s",t);
for (i=0;i<n;i++)
{
if(strcmp(a[i].title,t)==0) j=1;
}
if(j==1)
{
printf(" 查询到以下信息:\n");
printf(" 教师号 姓名 性别 年龄 学历 职称 工资 住址 电话 \n");
for (i=0;i<n;i++)
{
if (strcmp(a[i].title,t)==0)
{
printf(" %-10s%-7s%-5s%-5d%-8s%-8s%-8d%-11s%-10s\n",a[i].num,a[i].name,a[i].gender,a[i].age,a[i].education,a[i].title,a[i].wage,a[i].address,a[i].phone);
}
}
}
if (j==0) printf("\n 未查询到该名教师的信息\n");
}
2.5排序模块
本模块实现的功能是通过操作者选择排序并分别运行sort_1和sort_2两个函数来实现对教师信息的排序。
void sort(data &a,int n)
{
int style;
system("cls");
printf("\n\n\n\n\n\n\n\n * * * * * * * * * * 教师信息排序 * * * * * * * * * *\n\n");
printf(" 请输入排序方式:\n");
printf(" 1.按年龄进行排序\n");
printf(" 2.按工资进行排序\n ");
scanf("%d",&style);
switch (style)
{
case 1:sort_1(a,n);break;
case 2:sort_2(a,n);break;
default:putchar('\a');
}
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" 排序完成,回车以返回首页\n");
getchar();
printf(" ");
getchar();
}
sort_1函数:
void sort_1(data &a,int n)
{
int i,j,style;
char temp[50];
printf(" 请输入排序顺序:\n");
printf(" 1.按从高到低的顺序进行排序\n");
printf(" 2.按从低到高的顺序进行排序\n ");
scanf("%d",&style);
if (style==1)
{
for (i=0;i<n;i++)
{
for (j=0;j<n-1;j++)
{
if (a[j].age<a[j+1].age)
{
strcpy(temp,a[j].num);
strcpy(a[j].num,a[j+1].num);
strcpy(a[j+1].num,temp);
strcpy(temp,a[j].name);
strcpy(a[j].name,a[j+1].name);
strcpy(a[j+1].name,temp);
strcpy(temp,a[j].gender);
strcpy(a[j].gender,a[j+1].gender);
strcpy(a[j+1].gender,temp);
exchange(a[j].age,a[j+1].age);
strcpy(temp,a[j].education);
strcpy(a[j].education,a[j+1].education);
strcpy(a[j+1].education,temp);
strcpy(temp,a[j].title);
strcpy(a[j].title,a[j+1].title);
strcpy(a[j+1].title,temp);
exchange(a[j].wage,a[j+1].wage);
strcpy(temp,a[j].address);
strcpy(a[j].address,a[j+1].address);
strcpy(a[j+1].address,temp);
strcpy(temp,a[j].phone);
strcpy(a[j].phone,a[j+1].phone);
strcpy(a[j+1].phone,temp);
}
}
}
}
else
{
if (style==2)
{
for (i=0;i<n;i++)
{
for (j=0;j<n-1;j++)
{
if (a[j].age>a[j+1].age)
{
strcpy(temp,a[j].num);
strcpy(a[j].num,a[j+1].num);
strcpy(a[j+1].num,temp);
strcpy(temp,a[j].name);
strcpy(a[j].name,a[j+1].name);
strcpy(a[j+1].name,temp);
strcpy(temp,a[j].gender);
strcpy(a[j].gender,a[j+1].gender);
strcpy(a[j+1].gender,temp);
exchange(a[j].age,a[j+1].age);
strcpy(temp,a[j].education);
strcpy(a[j].education,a[j+1].education);
strcpy(a[j+1].education,temp);
strcpy(temp,a[j].title);
strcpy(a[j].title,a[j+1].title);
strcpy(a[j+1].title,temp);
exchange(a[j].wage,a[j+1].wage);
strcpy(temp,a[j].address);
strcpy(a[j].address,a[j+1].address);
strcpy(a[j+1].address,temp);
strcpy(temp,a[j].phone);
strcpy(a[j].phone,a[j+1].phone);
strcpy(a[j+1].phone,temp);
}
}
}
}
}
}
sort_2函数:
void sort_2(data &a,int n)
{
int i,j,style;
char temp[50];
printf(" 请输入排序顺序:\n");
printf(" 1.按从高到低的顺序进行排序\n");
printf(" 2.按从低到高的顺序进行排序\n ");
scanf("%d",&style);
if (style==1)
{
for (i=0;i<n-1;i++)
{
for (j=0;j<n-1;j++)
{
if (a[j].wage<a[j+1].wage)
{
strcpy(temp,a[j].num);
strcpy(a[j].num,a[j+1].num);
strcpy(a[j+1].num,temp);
strcpy(temp,a[j].name);
strcpy(a[j].name,a[j+1].name);
strcpy(a[j+1].name,temp);
strcpy(temp,a[j].gender);
strcpy(a[j].gender,a[j+1].gender);
strcpy(a[j+1].gender,temp);
exchange(a[j].age,a[j+1].age);
strcpy(temp,a[j].education);
strcpy(a[j].education,a[j+1].education);
strcpy(a[j+1].education,temp);
strcpy(temp,a[j].title);
strcpy(a[j].title,a[j+1].title);
strcpy(a[j+1].title,temp);
exchange(a[j].wage,a[j+1].wage);
strcpy(temp,a[j].address);
strcpy(a[j].address,a[j+1].address);
strcpy(a[j+1].address,temp);
strcpy(temp,a[j].phone);
strcpy(a[j].phone,a[j+1].phone);
strcpy(a[j+1].phone,temp);
}
}
}
}
else
{
if (style==2)
{
for (i=0;i<n-1;i++)
{
for (j=0;j<n-1;j++)
{
if (a[j].wage>a[j+1].wage)
{
strcpy(temp,a[j].num);
strcpy(a[j].num,a[j+1].num);
strcpy(a[j+1].num,temp);
strcpy(temp,a[j].name);
strcpy(a[j].name,a[j+1].name);
strcpy(a[j+1].name,temp);
strcpy(temp,a[j].gender);
strcpy(a[j].gender,a[j+1].gender);
strcpy(a[j+1].gender,temp);
exchange(a[j].age,a[j+1].age);
strcpy(temp,a[j].education);
strcpy(a[j].education,a[j+1].education);
strcpy(a[j+1].education,temp);
strcpy(temp,a[j].title);
strcpy(a[j].title,a[j+1].title);
strcpy(a[j+1].title,temp);
exchange(a[j].wage,a[j+1].wage);
strcpy(temp,a[j].address);
strcpy(a[j].address,a[j+1].address);
strcpy(a[j+1].address,temp);
strcpy(temp,a[j].phone);
strcpy(a[j].phone,a[j+1].phone);
strcpy(a[j+1].phone,temp);
}
}
}
}
}
}
2.6读取模块
void Open(data &a,int &n)
{
FILE *fp;
if ((fp=fopen("data.dat","a+"))==NULL)
{
return;
}
n=0;
do
{
fscanf(fp,"%s %s %s %d %s %s %d %s %s\n",a[n].num,a[n].name,a[n].gender,&a[n].age,a[n].education,a[n].title,&a[n].wage,a[n].address,a[n].phone);
n++;
} while (!(a[n-1].wage==0&&a[n-1].age==0));
n--;
fclose(fp);
}
2.7保存模块
void Save(data a,int n)
{
system("cls");
printf("\n");
FILE *fp;
int i;
if ((fp=fopen("data.dat","w"))==NULL)
{
printf("无法打开此文件!");
exit(0);
}
for (i=0;i<n;i++)
{
fprintf(fp,"%s %s %s %d %s %s %d %s %s\n",a[i].num,a[i].name,a[i].gender,a[i].age,a[i].education,a[i].title,a[i].wage,a[i].address,a[i].phone);
}
fprintf(fp,"%s %s %s %d %s %s %d %s %s\n",0,0,0,0,0,0,0,0,0);
fclose(fp);
printf("\n\n\n\n\n\n\n\n * * * * * * * * * * 教师信息保存 * * * * * * * * * *\n\n");
printf(" 保存成功\n\n");
printf(" * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" 回车以返回\n");
getchar();
printf(" ");
getchar();
}
2.8浏览模块
void Browse(data a,int n)
{
int i;
system("cls");
printf("\n\n\n * * * * * * * * * * * * * * * * 教师信息浏览 * * * * * * * * * * * * * * * *\n");
printf(" 按顺序输出所有教师信息\n\n");
printf(" 教师号 姓名 性别 年龄 学历 职称 工资 住址 电话 \n");
if(n!=0)
{
for (i=0;i<n;i++)
{
printf(" %-10s%-7s%-5s%-5d%-8s%-8s%-8d%-11s%-10s\n",a[i].num,a[i].name,a[i].gender,a[i].age,a[i].education,a[i].title,a[i].wage,a[i].address,a[i].phone);
}
printf("\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" 以上为所有教师的信息,回车以返回首页\n");
}
else
{
printf("\n * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" 教师信息为空,回车以返回首页\n");
}
getchar();
printf(" ");
getchar();
}
2.9其他相关
(1)结构体定义
typedef struct node
{
char num[50];
char name[50];
char gender[50];
int age;
char education[50];
char title[50];
int wage;
char address[50];
char phone[50];
}data[MAX_SIZE];
(2)UI模块
void UI(void)
{
system("cls");
printf("\n\n\n\n");
printf(" * * * * * * * * * * * * * * * * * * * * * * *\n");
printf(" * *\n");
printf(" * 请输入0~6以使用以下功能 : *\n");
printf(" * 1.教师信息录入; *\n");
printf(" * 2.教师信息删除; *\n");
printf(" * 3.教师信息浏览; *\n");
printf(" * 4.教师信息保存; *\n");
printf(" * 5.教师信息查询; *\n");
printf(" * 6.教师信息排序; *\n");
printf(" * 0.退出信息系统。 *\n");
printf(" * *\n");
printf(" * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("\n");
printf(" ");
}
(3)交换用函数
void exchange(int &x,int &y)
{
int temp;
temp=x;
x=y;
y=temp;
}