Employee information management system (C language)

Table of contents

1 Introduction

2. Realize the function

3. Flowchart 

4. Source code

5. Summary


1 Introduction

        It has been quite a long time since I learned C language. Writing a small project, the function realization may not be perfect, so it is a review and record!

2. Realize the function

0. Enter employee information                     
1. Sort by salary level
2. Modify employee information
3. Display employee information
4. Delete employee information
5. Query employee information
6. Exit the system

The employee number is randomly generated

3. Flowchart 

4. Source code

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define MAXSIZE 100  //符号常量 用来申请100个单元的存储空间
#define N 2          //创建数组个数,可更改
int length=0;        //全局变量,用来表示当前存储记录的条数
struct employee 
{
	int No;               //职工号
	char name[20];        //姓名
	char sex[10];         //性别
	char age[10];         //年龄
	char degree[10];      //学历 
	char salary[20];      //工资 
	char addr[35];        //住址 
	char tel[20];         //电话 
}em[N];          

int select(struct employee ly[])
{
  int i,index=-1;
  char name[20];
  for(i=0;i<length;i++)
   if(strcmp(ly[i].name,name)==0)       //strcmp()比较字符串的大小,如果返回值为0则表示2个字符串相等
     {
       index=i;
       break;
     }
  return index;
}

//录入职工信息
void add(struct employee ly[])
{
    int i,number;
	printf("请输入录入信息的职工人数:");
	scanf("%d",&number);
	for(i=0;i<number;i++)
	{
		srand(time(NULL));
		ly[i].No=rand()%1000+2014000;   //随机数范围2014000~2014999
		if(ly[i].No!=ly[i-1].No)
		printf("\n");
		printf("随机生成的职工号:%d\n",ly[i].No);
	if(MAXSIZE==length) 
	return;                  //如果表已经存满,则不能添加记录
    printf("请输入姓名:");
    scanf("%s",ly[length].name);
    printf("请输入性别:");
    scanf("%s",ly[length].sex);
    printf("请输入年龄:");
    scanf("%s",ly[length].age);
    printf("请输入学历:");
    scanf("%s",ly[length].degree);
    printf("请输入工资:");
    scanf("%s",ly[length].salary);
    printf("请输入住址:");
    scanf("%s",ly[length].addr);
    printf("请输入电话:");
    scanf("%s",ly[length].tel);
    length++;                 // 添加一个记录,实际长度就+1
	}
}

//信息排序
void rank(struct employee ly[])    //按工资由大到小排序
{
	int i,j;
	for(i=0;i<N-1;i++)
	{
		for(j=0;j<N-1-i;j++)
		{
			if(ly[j].salary<ly[j+1].salary)
			{
				em[i]=ly[j];
				ly[j]=ly[j+1];
				ly[j+1]=em[i];      
			}
		}
	}
	for(i=0;i<N;i++)
	{
		 printf("\n %d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
		 ly[i].No,ly[i].name,
		 ly[i].sex,ly[i].age,ly[i].degree,
		 ly[i].salary,ly[i].addr,ly[i].tel);
	}
}

//显示职工信息
void show(struct employee ly[])
{  
    int i;
    if(length==0)  
    {
        printf("空表!");
    return ;
  }
  printf(" \n 职工号\t\t姓名\t性别\t年龄\t学历\t工资\t住址\t\t电话\n");
  for(i=0;i<length;i++)
    printf("\n %d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
    ly[i].No,ly[i].name,ly[i].sex,ly[i].age,ly[i].degree,
    ly[i].salary,ly[i].addr,ly[i].tel);
}

//删除职工信息
void deleteByname(struct employee ly[])
{
    int i;
    char name[20];
    printf("请输入你要删除记录的姓名:");
    scanf("%s",name);
    if(i==-1) return;
    for(i=0;i<length-1;i++)
     ly[i]=ly[i+1];
     length--;          //删除成功后,实际长度-1
}

//修改职工信息
void update(struct employee ly[])
{
    int i;
    char name[20];
    printf("请输入你要修改已记录的姓名:");
    scanf("%s",name);
    if(i==-1) return;
    for(i=0;i<length-1;i++)
    ly[i]=ly[i+1];
    length--;
    printf("请输入新的职工号:");
    scanf("%d",&ly[length].No);
    printf("请输入新的姓名:");
    scanf("%s",ly[length].name);
    printf("请输入新的性别:");
    scanf("%s",ly[length].sex);
    printf("请输入新的年龄:");
    scanf("%s",ly[length].age);
    printf("请输入新的学历:");
    scanf("%s",ly[length].degree);
    printf("请输入新的工资:");
    scanf("%s",ly[length].salary);
    printf("请输入新的住址:");
    scanf("%s",ly[length].addr);
    printf("请输入新的电话:");
    scanf("%s",ly[length].tel);
    length++;
}

//查询职工信息
int chaxun(struct employee ly[])
{
	int i,j=0;
	int num;
	char find[30];	  
	printf("\n提示:输入\t 0--退出\t1--按学历查询\t 2--按姓名查询\t 3--按电话查询\n");
	printf("请选择:");
	scanf("%d",&num);
	for(i=0;i<length;i++)
	{
		if(num==1)      //按学历查询
	    {
		     printf("\n请输入你要查询的学历:");
		     scanf("%s",find);
			if(strcmp(ly[i].degree,find)==0)
			{
			    j=1;
				printf("\n 职工号\t\t姓名\t性别\t年龄\t学历\t工资\t住址\t\t电话\n");
			 	printf("\n %d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
				ly[i].No,ly[i].name,
				ly[i].sex,ly[i].age,ly[i].degree,
				ly[i].salary,ly[i].addr,ly[i].tel);
		    }    
	    }
	    else if(num==2)      //按职工姓名查询
	    {
		     printf("\n输入你要查询的职工姓名:");
		     scanf("%s",find);
			if(strcmp(ly[i].name,find)==0) 
			{
				j=1;
				printf("\n 职工号\t\t姓名\t性别\t年龄\t学历\t工资\t住址\t\t电话\n");
				printf("\n %d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
				ly[i].No,ly[i].name,
				ly[i].sex,ly[i].age,ly[i].degree,
				ly[i].salary,ly[i].addr,ly[i].tel);
			}
	     }
	    else if(num==3)      //按职工电话查询
	    {
		    printf("\n输入你要查询的电话:");
		    scanf("%s",find);
			if(strcmp(ly[i].tel,find)==0) 
			{
				j=1;
				printf("\n 职工号\t\t姓名\t性别\t年龄\t学历\t工资\t住址\t\t电话\n");
				printf("\n %d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
				ly[i].No,ly[i].name,
				ly[i].sex,ly[i].age,ly[i].degree,
				ly[i].salary,ly[i].addr,ly[i].tel);
			}
	    }
	    else if (num==0)
		return 0;
	}
	return 0;
}

//将职工信息写到文件中
void save(int n)      //保存n个记录
{ 
	FILE *fp;
    int i;
    if((fp=fopen("employee.txt","wb"))==NULL) //以只写方式为输出打开一个二进制文件
    { 
	    printf("\n不能打开文件\n");
	    exit(0);
    }

   for(i=0;i<n;i++)
   {
    if(fwrite(&em[i],sizeof(struct employee),1,fp)!=1)
    { 
        printf("文件写入错误\n");
    }    
   }
    fclose(fp);
}

int load() //加载信息
{ 
	FILE *fp;
    int i;
    if((fp=fopen("employee.txt","rb"))==NULL)
    {
    	printf("\n不能打开文件\n");
    	exit(0);
    }
    for(i=0;feof(fp);i++)
    {
    	fread(&em[i],sizeof(struct employee),1,fp);
    	printf("\n %d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t\n",
        em[i].No,em[i].name,em[i].sex,em[i].age,em[i].degree,
        em[i].salary,em[i].addr,em[i].tel);
    }
      fclose(fp);
      return(i-1);
}

//开始页面
void start()
{
	printf("\n                                时间:");
	system("date/t");
	printf("\t\t\t\t==============================================\n ");
	printf("\t\t\t\t                            \t\t  \n ");
	printf("\t\t\t\t                            \t\t  \n ");
	printf("\t\t\t\t\t|   欢迎访问职工管理系统!   |\t\t\t  \n ");
	printf("\t\t\t\t\t|                           |\t\t\t  \n ");
	printf("\t\t\t\t\t|   --------------------    |\t\t\t  \n ");
	printf("\t\t\t\t                            \t\t  \n ");
	printf("\t\t\t\t                            \t\t  \n ");
	printf("\t\t\t\t==============================================\n ");
	printf("\n");
	printf("\t\t\t\n                                        \t\t请按回车键进入系统......\n");
	getchar();
	system("cls");   //清屏
}

//退出页面
void end()
{
	system("cls");
	printf("\t\t\t\t==============================================\n ");
	printf("\t\t\t\t                            \t\t  \n ");
	printf("\t\t\t\t                            \t\t  \n ");
	printf("\t\t\t\t\t|   感谢访问,欢迎下次光临! |\t\t\t  \n ");
	printf("\t\t\t\t\t|                           |\t\t\t  \n ");
	printf("\t\t\t\t\t|   --------------------    |\t\t\t  \n ");
	printf("\t\t\t\t                            \t\t  \n ");
	printf("\t\t\t\t                            \t\t  \n ");
	printf("\t\t\t\t==============================================\n ");
	printf("\n");
	printf("\t\t\t\n                                        \t\t请按任意键退出系统......\n");
	printf("\n                                时间:");
	system("date/t");
}

//主菜单
void menum()
{
	printf("\t\t\t=======================职工信息管理系统=======================\n ");
	printf("\t\t\t==\t\t\t0.录入职工信息\t\t\t    ==\n ");
	printf("\t\t\t==\t\t\t1.按工资高低排序\t\t    ==\n ");
	printf("\t\t\t==\t\t\t2.修改职工信息\t\t\t    ==\n ");
	printf("\t\t\t==\t\t\t3.显示职工信息\t\t\t    ==\n ");
	printf("\t\t\t==\t\t\t4.删除职工信息\t\t\t    ==\n ");
	printf("\t\t\t==\t\t\t5.查询职工信息\t\t\t    ==\n ");
	printf("\t\t\t==\t\t\t6.退出系统\t\t\t    ==\n");
	printf("\t\t\t==============================================================\n ");
	printf("\n");
}

//主函数
int main()
{
  int index=0;
  int result=0;
  struct employee d[MAXSIZE];   //定义了一个容量为100的数组
  start();
  menum();
  while(1)
  {
     printf("\n请输入你的操作:");
     scanf("%d",&result);
     if(result==6)
     {
     	break;
     	getchar();
     }
     switch(result)
     {
        case 0:add(d);break;
        case 1:rank(d);break;
        case 2:update(d);break;
        case 3:show(d);break;
        case 4:deleteByname(d);break;
        case 5:chaxun(d);break;
        case 6:end();break;
        default: printf("提示:输入错误\n");
     }
  }
  getchar();
  end();
  return 0;
}

5. Summary

       Programming is a good opportunity to systematically summarize the theoretical knowledge we have learned and apply it to practice. It is conducive to strengthening our ability to analyze practical problems with knowledge theory, consolidating our theoretical knowledge, and helping the formation of our logical thinking. What's more, I also exercised my patience and laid a good foundation for us to go to society. In the end, I hope that I can be down-to-earth, lay a solid foundation, and go higher and further on this road.

Guess you like

Origin blog.csdn.net/qq_62250174/article/details/128564785