C语言— —课程设计职工信息管理系统
项目要求
以菜单方式工作管理每个职工的个人信息。
(1)总共有职工10名,每个职工是一个记录,包括的信息有: 职工号、姓名、性别、出生年月、学历、职务、部门、工资、住址、电话并且可以对职工信息进行录入、删除、修改、浏览等操作。
(2)可以按姓名进行查询。
(3)可以按工资排序并浏览。
函数之间调用说明:
一,设定结构体
代码如下:
struct worker
{
int number;
char name[15];
char sex[15];
int birth;
char edu[15];
char post[15];
char department[15];
int salary;
char address[15];
}
字符型name代表职工姓名,字符型sex代表职工性别,字符型edu代表职工学历,字符型post代表职工职位,字符型department代表职工部门,字符型address代表职工住址,整型number代表职工编号,整型birth代表职工年龄,整型salary代表职工工资,结构体worker存储了不同职工信息。
二,定义主函数
代码如下:
int main()
{
char c;
do
{
printf("\n\n\n");
printf(" §§职工信息管理系统§§ \n");
printf("\n"); printf("*****************************************************************\n");
printf("* § *\n");
printf("* 1.录入职工信息"); printf(" § 2.显示职工信息 *\n");
printf("* § *\n");
printf("* 3.修改职工信息"); printf(" § 4.删除职工信息 *\n");
printf("* § *\n");
printf("* 5.查询职工信息"); printf(" § 6.职工信息排序 *\n");
printf("* *\n");
printf("* 0.退出管理系统 *\n");
printf("*****************************************************************\n");
printf("\n 请输入:");
c = getchar();
switch (c)
{
case '1': {input(); getchar(); break; }
case '2': {display(); getchar(); break; }
case '3': {modify(); getchar(); break; }
case '4': {dele(); getchar(); break; }
case '5': {query(); getchar(); break; }
case '6': {order(); getchar(); break; }
case '0':
{
printf("\n感谢使用,本程序由Mi-ronin编写而成,版权所有侵权必究,感谢支持,再见。 ");
exit(0);
}
default:
{
printf("\n请输入有效数字:");
c = getchar();
}
}
} while (1);
return 0;
}
三,主菜单
代码如下:
int main()
{
char c;
do
{
printf("\n\n\n");
printf(" §§职工信息管理系统§§ \n");
printf("\n"); printf("*****************************************************************\n");
printf("* § *\n");
printf("* 1.录入职工信息"); printf(" § 2.显示职工信息 *\n");
printf("* § *\n");
printf("* 3.修改职工信息"); printf(" § 4.删除职工信息 *\n");
printf("* § *\n");
printf("* 5.查询职工信息"); printf(" § 6.职工信息排序 *\n");
printf("* *\n");
printf("* 0.退出管理系统 *\n");
printf("*****************************************************************\n");
printf("\n 请输入:");
c = getchar();
switch (c)
{
case '1': {input(); getchar(); break; }
case '2': {display(); getchar(); break; }
case '3': {modify(); getchar(); break; }
case '4': {dele(); getchar(); break; }
case '5': {query(); getchar(); break; }
case '6': {order(); getchar(); break; }
case '0':
{
printf("\n感谢使用,本程序由Mi-ronin编写而成,版权所有侵权必究,感谢支持,再见。 ");
exit(0);
}
default:
{
printf("\n请输入有效数字:");
c = getchar();
}
}
} while (1);
return 0;
}
四,录入职工信息
代码如下:
void input()
{
int i;
for (i = 0; i < M; i++)
{
printf("\n输入第%d个职工信息", i + 1);
printf("\n输入职工号:");
scanf_s("%d", &worker[i].number);
printf("\n输入职工姓名:");
scanf_s("%s", &worker[i].name);
printf("\n输入职工性别:");
scanf_s("%s", &worker[i].sex);
printf("\n输入职工年龄:");
scanf_s("%d", &worker[i].birth);
printf("\n输入职工学历:");
scanf_s("%s", &worker[i].edu);
printf("\n输入职工职务:");
scanf_s("%s", &worker[i].post);
printf("\n输入职工部门:");
scanf_s("%s", &worker[i].department);
printf("\n输入职工工资:");
scanf_s("%d", &worker[i].salary);
printf("\n输入职工住址:");
scanf_s("%s", &worker[i].address);
}
writeToFile();
}
五,文件操作
文件操作包括写文件和读文件:
//将职工数据写到文件中
void writeToFile()
{
int i;
FILE* fp;
fopen_s(&fp, "worker", "wb");
if (!fp)
{
printf("cannot open the file\n");
return;
}
for (i = 0; i < 10; i++)
{
if (fwrite(&worker[i], sizeof(struct worker), 1, fp) != 1)
{
printf("file write error\n");
return;
}
}
fclose(fp);
}
//从文件中读取职工数据并存入数组stu[M]
int readFromFile()
{
int i = 0;
FILE* fp;
fopen_s(&fp, "worker", "rb");
if (!fp)
{
printf("cannot open the file\n");
exit(0);
}
else
{
do {
fread(&worker[i], sizeof(struct worker), 1, fp);
i++;
} while (feof(fp) == 0);
}
fclose(fp);
return(i - 1);
}
六,职工信息显示
代码如下:
void display()
{
int i;
int m = readFromFile();
printf("\n公司职工信息:\n");
printf("\n职工号\t姓名\t性别\t年龄\t学历\t职务\t部门\t工资\t住址\n");
for (i = 0; i < M; i++)
printf("\n %d\t %s\t %s\t %d\t %s\t %s\t %s\t %d\t %s\n", worker[i].number, worker[i].name, worker[i].sex, worker[i].birth, worker[i].edu, worker[i].post, worker[i].department, worker[i].salary, worker[i].address);
printf("\n");
}
七,职工信息删除
代码如下:
void dele()
{
int m = readFromFile();
int i, j, t, n, f, Num;
display();
printf("请输入要删除的职工的职工号:");
scanf_s("%d", &Num);
for (f = 1, i = 0; f && i < m; i++)
{
if (worker[i].number == Num)
{
printf("\n已找到此人,原始记录为:\n");
printf("\n职工号\t姓名\t性别\t年龄\t学历\t职务\t部门\t工资\t住址\n"); printf("\n %d\t %s\t %s\t %d\t %s\t %s\t %s\t %d\t %s\n", worker[i].number, worker[i].name, worker[i].sex, worker[i].birth, worker[i].edu, worker[i].post, worker[i].department, worker[i].salary, worker[i].address);
printf("\n确认删除请按 1,取消删除请按 0: ");
scanf_s("%d", &n);
if (n == 1) /*如果删除,则其他的信息都向前移一行*/
{
for (j = i; j < m - 1; j++)
worker[j] = worker[j + 1]; /*当找到第j个职工时,for语句后j自加了1,所以下面的应该把改后的 信息赋值给第j-1个人*/
f = 0;
}
else f = 2;
}
}
system("cls");
switch (f) {
case 0: m = m - 1;
printf("\n删除成功!\n");
writeToFile(); /*调用保存函数*/
display(); /*调用浏览函数*/
break;
case 1: printf("\n没有该员工!\n");
display(); /*调用浏览函数*/
break;
case 2: printf("\n取消删除!\n");
break;
}
//display(); /*调用浏览函数*/
printf("\n继续删除请按1,返回主菜单请按0:");
scanf_s("%d", &t);
system("cls");
switch (t)
{
case 1: dele();
break;
case 0: system("cls");
break;
default: break;
}
system("cls");
}
八,职工信息修改
代码如下
//信息修改
void modify()
{
int m = readFromFile();
int number;
char name[15];
char sex[15];
int birth;
char edu[15];
char post[15];
char department[15];
int salary;
int tel[15];
char address[15];
int b = 1, c, i, n, t, k = 0; /*导入文件内的信息*/
printf("\n");
printf("请输入要修改的职工的职工号: ");
scanf_s("%d", &number);
system("cls");
for (i = 0; i < m; i++)
{
if (worker[i].number == number)
{
k = 1;
printf("\n已找到该职工号员工,其记录为:\n");
printf("\n职工号\t姓名\t性别\t年龄\t学历\t职务\t部门\t工资\t住址\n");
printf("\n %d\t %s\t %s\t %d\t %s\t %s\t %s\t %d\t %s\n", worker[i].number, worker[i].name, worker[i].sex, worker[i].birth, worker[i].edu, worker[i].post, worker[i].department, worker[i].salary, worker[i].address);
printf("\n确认修改请按1,取消修改请按0:");
scanf_s("%d", &n);
if (n == 1)
{
printf("\n需要进行修改的选项\n 1.职工号 2.姓名 3.性别 4.年龄 5.学历 6.职务 7.部门 8.工资 9.住址\n");
printf("请输入你想修改的那一项序号: ");
scanf_s("%d", &c);
do {
switch (c)
{
case 1: printf("职工号改为:");
scanf_s("%d", &number);
worker[i].number = number;
break;
case 2: printf("姓名改为:");
scanf_s("%s", name);
strcpy_s(worker[i].name, name);
break;
case 3: printf("性别改为:");
scanf_s("%s", &sex);
strcpy_s(worker[i].sex, sex);
break;
case 4: printf("年龄改为:");
scanf_s("%d", &birth);
worker[i].birth = birth;
break;
case 5: printf("学历改为:");
scanf_s("%s", edu);
strcpy_s(worker[i].edu, edu);
break;
case 6: printf("职务改为:");
scanf_s("%s", &post);
strcpy_s(worker[i].post, post);
break;
case 7: printf("部门改为:");
scanf_s("%s", department);
strcpy_s(worker[i].department, department);
break;
case 8: printf("工资改为:");
scanf_s("%d", &salary);
worker[i].salary = salary;
break;
case 9:printf("住址改为:");
scanf_s("%s", &address);
strcpy_s(worker[i].address, address);
break;
}
printf("\n确认修改 请按1,重新输入 请按2: ");
scanf_s("%d", &b);
if (b == 1)
{
system("cls");
writeToFile();//储存修改后的数据
printf("\n");
display();//浏览修改后的数据
}
} while (b == 2);
}
}
else if (i == (m - 1)) i++;
}
if (k == 0) {
printf("\n输入有误!\n");
getchar();
getchar();
}
else {
printf("\n继续修改请按 1,退出修改请按 0: ");
scanf_s("%d", &t);
}
system("cls");
switch (t)
{
case 1: display();
modify();//返回修改函数
break;
case 0: break;
default:break;
}
system("cls");
}
九,职工信息查询
//信息查询
void query()
{
char name1[15];
int i, t, n = 0;
int m = readFromFile();
printf("\n请输入要查找的姓名: ");
scanf_s("%s", name1);
for (i = 0; i < m; i++)
if (strcmp(name1, worker[i].name) == 0)
{
if (n == 0)
{
printf("\n已找到以下信息:\n");
printf("\n职工号\t姓名\t电话号\t性别\t年龄\t学历\t职务\t部门\t工资\t住址\n");
}
n++; printf("\n %d\t %s\t %s\t %d\t %s\t %s\t %s\t %d\t %s\n", worker[i].number, worker[i].name, worker[i].sex, worker[i].birth, worker[i].edu, worker[i].post, worker[i].department, worker[i].salary, worker[i].address);
}
if (n == 0)
{
printf("\n没有该员工!\n");
getchar(); getchar();
}
else
{
printf("\n查询到有 %d 个员工符合要求。\n", n);
printf("\n");
printf("继续查询请按 1,,返回主菜单请按 0 :");
scanf_s("%d", &t);
switch (t)
{
case 0: system("cls");
break;
case 1: break;
default:break;
}
}
}
代码运行检测及结果:
1.信息录入:
2.信息修改:
3.信息删除:
4.信息查询:
5.信息排序: