人员信息管理系统(C源代码)
这是今天在朋友的坛子上转到的,觉的还不错,转过来和大家分享下.对新人朋友朋友们很有帮助. 借这个小地给朋友坛子也做下广告,呵呵
好了,进入正题吧,源码如下:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
struct ry
{
int num; //编号
char name[10]; //姓名
int pay; //工资
}ren;
int kg=0;
void adminmenu();
void usermenu();
void adminsys();
void usersys();
void data();
void find();
void print();
void help();
void backup();
void choose();
void inputpw (char *password,int len);
void user();
int finduser(char *user,char *pass);
void main()
{
choose();
}
void choose()
{
FILE *fp;
char user[10];
char pass[12];
if((fp=fopen("user.txt","r"))==NULL)
fp=fopen("user.txt","w");
fclose(fp);
if((fp=fopen("data.txt","r"))==NULL)
fp=fopen("data.txt","w");
fclose(fp);
system("cls");
printf("----------------------\n");
printf("人员信息管理系统\n");
printf("----------------------\n");
printf("登陆!\n");
printf("请输入用户名:");
fflush(stdin);
scanf("%s",user);
inputpw(pass,12);
if(strcmp(user,"admin")==0&&strcmp(pass,"admin")==0)
{
kg=1;
adminmenu();
}
else
if(finduser(user,pass))
{
kg=0;
usermenu();
}
else
{
printf("\n没有这个用户,请重新输入!\n");
if(getch())
choose();
}
}
int finduser(char *user1,char *pass1)
{
int i=0;
FILE *fp;
char user[10];
char pass[12];
fp=fopen("user.txt","r+");
fflush(stdin);
while(!feof(fp))
{
fscanf(fp,"%s%s",user,pass);
if(strcmp(user,user1)==0&&strcmp(pass,pass1)==0)
{
return 1;
break;
}
}
fclose(fp);
return 0;
}
void backup()
{
FILE *p,*q;
p=fopen("data.txt","r");
q=fopen("data.txt.bak","w");
while(!feof(p))
{
if(fscanf(p,"%d%s%d",&ren.num,ren.name,&ren.pay)==3)
{
fprintf(q,"%d %s %d",ren.num,ren.name,ren.pay);
fputc('\n',q);
}
}
fclose(p);
fclose(q);
printf("\n备份成功!\n");
if(getch())
adminsys();
}
void inputpw(char *password,int len)
{
int i=0;
char ch;
printf("\n请输入密码(长度小于等于%d位): ",len);
fflush(stdin);
for ( ch = getch(); ch!=13;ch = getch())
{
if(i>=len)
continue;
if(ch == 8)
{
if(i>0)
{
printf("\b");
password[--i]='\0';
}
continue ;
}
if( ch<32 || ch>127 )
continue;
printf("*");
password[i++]=ch;
}
password = '\0';
}
void adminmenu()
{
int i;
while(1)
{
system("cls");
printf("\t人员信息管理系统\n");
printf("1.系统管理\n");
printf("2.数据库管理\n");
printf("3.数据库查询管理\n");
printf("4.报表打印\n");
printf("5.帮助\n");
printf("\n请选择操作:");
fflush(stdin);
scanf("%d",&i);
switch(i)
{
case 1:
adminsys();
break;
case 2:
data();
break;
case 3:
find();
break;
case 4:
print();
break;
case 5:
help();
break;
default:
printf("错误,请重新输入!");
if(getch())
continue;
}
}
}
void usermenu()
{
int i;
while(1)
{
system("cls");
printf("\t人员信息管理系统\n");
printf("1.系统管理\n");
printf("2.数据库查询管理\n");
printf("3.报表打印\n");
printf("4.帮助\n");
printf("\n请选择操作:");
fflush(stdin);
scanf("%d",&i);
switch(i)
{
case 1:
usersys();
break;
case 2:
find();
break;
case 3:
print();
break;
case 4:
help();
break;
default:
printf("错误,请重新输入!");
if(getch())
continue;
}
}
}
void adminsys()
{
int i;
while(1)
{
system("cls");
printf("\t系统管理\n");
printf("1.用户管理\n");
printf("2.数据库备份\n");
printf("3.注销用户\n");
printf("4.退出系统\n");
printf("5.返回\n");
printf("\n请选择操作:");
fflush(stdin);
scanf("%d",&i);
switch(i)
{
case 1:
user();
break;
case 2:
backup();
break;
case 3:
choose();
break;
case 4:
exit(0);
break;
case 5:
adminmenu();
break;
default:
printf("错误,请重新输入!");
if(getch())
continue;
}
}
}
void usersys()
{
int i;
while(1)
{
system("cls");
printf("\t系统管理\n");
printf("1.注销用户\n");
printf("2.退出系统\n");
printf("3.返回\n");
printf("\n请选择操作:");
fflush(stdin);
scanf("%d",&i);
switch(i)
{
case 1:
choose();
break;
case 2:
exit(0);
break;
case 3:
usermenu();
break;
default:
printf("错误,请重新输入!");
if(getch())
continue;
}
}
}
void user()
{
int i,m=0;
char user[10],pass[10],temp[10];
FILE *fp,*tmpfile;
while(1)
{
system("cls");
printf("\t用户管理\n");
printf("1.增加一个用户\n");
printf("2.删除一个用户\n");
printf("3.返回\n");
printf("\n请选择操作:");
fflush(stdin);
scanf("%d",&i);
switch(i)
{
case 1:
fp=fopen("user.txt","a+");
printf("\n请输入用户名:");
scanf("%s",user);
printf("请输入密码:");
scanf("%s",pass);
fprintf(fp,"%s %s",user,pass);
fputc('\n',fp);
fclose(fp);
adminsys();
break;
case 2:
fp=fopen("user.txt","r");
tmpfile=fopen("tmp.txt", "w");
printf("请输入要删除的用户名:");
fflush(stdin);
scanf("%s",temp);
while(!feof(fp))
{
fscanf(fp,"%s%s",user,pass);
if(strcmp(temp,user)==0)
{
m=1;
continue;
}
else
if(!feof(fp))
fprintf(tmpfile,"%s %s\n",user,pass);
}
fclose(fp);
fclose(tmpfile);
system("del user.txt");
system("rename tmp.txt user.txt");
if(m==0)
printf("\n没有此用户,删除失败!");
else
printf("\n删除成功!");
if(getch())
adminsys();
break;
case 3:
adminsys();
default:
printf("错误,请重新输入!");
if(getch())
continue;
}
}
}
void data()
{
int i,m=0;
int n;
FILE *fp,*tmpfile;
while(1)
{
system("cls");
printf("\t数据库管理\n");
printf("1.输入一项纪录\n");
printf("2.删除一项记录\n");
printf("3.返回\n");
printf("\n请选择操作:");
fflush(stdin);
scanf("%d",&i);
switch(i)
{
case 1:
fp=fopen("data.txt","a+");
while(1)
{
printf("\n请输入编号:");
fflush(stdin);
scanf("%d",&ren.num);
if(ren.num>0)
break;
else
{
printf("输入错误,请重新输入!\n\n");
continue;
}
}
printf("\n请输入姓名:");
fflush(stdin);
scanf("%s",ren.name);
while(1)
{
printf("\n请输入工资:");
fflush(stdin);
scanf("%d",&ren.pay);
if(ren.pay>0)
break;
else
{
printf("输入错误,请重新输入!\n\n");
continue;
}
}
fprintf(fp,"%d %s %d",ren.num, ren.name, ren.pay);
fputc('\n',fp);
fclose(fp);
adminmenu();
break;
case 2:
fp=fopen("data.txt","r");
tmpfile=fopen("tmp.txt", "w");
while(1)
{
printf("请输入要删除的编号:");
fflush(stdin);
scanf("%d",&n);
if(n>0)
break;
else
{
printf("\n输入错误,请重新输入!\n");
continue;
}
}
while(!feof(fp))
{
fscanf(fp,"%d%s%d",&ren.num,ren.name,&ren.pay);
if(ren.num == n)
{
m=1;
continue;
}
else
if(!feof(fp))
fprintf(tmpfile,"%d %s %d\n",ren.num, ren.name, ren.pay);
if(feof(fp)&&(m==0))
{
printf("\n对不起,找不到您所要删除的编号,请重输!\n");
}
}
if(m==1)
{
printf("\n删除成功!");
}
fclose(fp);
fclose(tmpfile);
system("del data.txt");
system("rename tmp.txt data.txt");
if(getch())
adminmenu();
break;
case 3:
adminmenu();
default:
printf("错误,请重新输入!");
if(getch())
continue;
}
}
}
void find()
{
int i=0,n;
FILE *fp;
system("cls");
printf("\t数据库查询管理\n");
fp=fopen("data.txt","r");
printf("请输入要查找的编号:");
fflush(stdin);
scanf("%d",&n);
while(!feof(fp))
{
if(fscanf(fp,"%d%s%d",&ren.num, ren.name, &ren.pay)==3)
{
if(ren.num==n)
{
printf("\n编号\t姓名\t工资\n");
printf("%d\t%s\t%d\n",ren.num,ren.name,ren.pay);
i=1;
}
}
if(feof(fp)&&(i==0))
{
printf("\n对不起,找不到您所要查找的编号,请重输!\n");
}
}
fclose(fp);
if(getch())
{
if(kg==0)
usermenu();
if(kg==1)
adminmenu();
}
}
void print()
{
FILE *fp;
int i=0;
system("cls");
printf("\t报表打印\n");
printf("编号\t姓名\t工资\n");
fp=fopen("data.txt","r");
while(!feof(fp))
{
if(fscanf(fp,"%d %s %d",&ren.num, ren.name, &ren.pay)==3)
{
printf("%d\t%s\t%d\n",ren.num,ren.name,ren.pay);
i=1;
}
}
if(feof(fp)&&(i==0))
printf("\n没有任何信息!");
fclose(fp);
if(getch())
{
if(kg==0)
usermenu();
if(kg==1)
adminmenu();
}
}
void help()
{
char s;
system("cls");
fflush(stdin);
while(1)
{
printf("+++++++++++++++++++++++++++++++++++++++++\n");
printf("+ +\n");
printf("+ 帮助 +\n");
printf("+ +\n");
printf("+++++++++++++++++++++++++++++++++++++++++\n");
if(s=getch())
{
if(kg==0)
usermenu();
if(kg==1)
adminmenu();
}
}
}