main.c 文件
#include"func.h"
int main(int argc,char *argv[])
{
char a[20];
char b;
int i,num=0;
char c[500];
char key[20];
pUSR_ACCOUNT phead_account=NULL;
pUSR_ACCOUNT ptail_account=NULL;
pUSR_INFOR phead_infor=NULL;
pUSR_INFOR ptail_infor=NULL;
pUSR_ACCOUNT pcur;
FILE *fp_account,*fp_infor;
memset(c,0,sizeof(c));
memset(key,0,sizeof(key));
fp_account=fopen(argv[1],"r");
if (NULL == fp_account)
{
perror("fopen");
}
fp_infor=fopen(argv[2],"r");
if (NULL == fp_account)
{
perror("fopen");
}
while(fgets(c,sizeof(c),fp_account)!=NULL)
num++; //计算行数即用户个数
scan_fp_list(&phead_account,&ptail_account);
scan_fp_list1(&phead_infor,&ptail_infor);
start_top();
printf(" 用户名:");
gets(a);
printf("\n\n 密码:");
while(0==1)
{
out:
printf("\b\b\b\b\b\b\b\b\b\b");
}
i=0;
while((b=getch())!=‘\r‘)
{
if(b>=‘a‘&&b<=‘z‘||b>=‘A‘&&b<=‘Z‘||b>=‘0‘&&b<=‘9‘||b==‘_‘)
{
key[i]=b; //密码保存在key中等待被比较
i++;
printf("*");
}
else if(b==‘\b‘)
{
if(i>0)
{
i--;
printf("\b \b");
}
}
}
pcur=phead_account;
for(i=0;i<num;i++)
{
if(strcmp(a,pcur->usr_name)==0)
{
if(strcmp(key,pcur->usr_pwd)==0)
{
if(pcur->usr_role==1)
{mng_menu(phead_account,phead_infor,fp_account,fp_infor);}else if(pcur->usr_role==2)
{stu_menu(phead_account,phead_infor,fp_account,fp_infor);}
}else{
printf("密码错误,请重新输入");
memset(key,0,sizeof(key));
goto out;
}
}
pcur=pcur->pNext_Usr_Account;
}
if(i=num)
{
printf("\n\n没有这个账户");
}
system("pause");
return 0;
}
func.h文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
typedef struct usr_account_tag{
char usr_name[20];
char usr_pwd[20];
int usr_role;
struct usr_account_tag *pNext_Usr_Account;
}USR_ACCOUNT,*pUSR_ACCOUNT;
typedef struct usr_infor_tag{
int usr_id;
char usr_name[20];
//int usr_course_id[3];
float usr_course_score[3];
struct usr_infor_tag *pNext_Usr_Infor;
}USR_INFOR,*pUSR_INFOR;
void start_top();
void scan_fp_list(pUSR_ACCOUNT *,pUSR_ACCOUNT *);
void scan_fp_list1(pUSR_INFOR *,pUSR_INFOR *);
void mng_menu(pUSR_ACCOUNT ,pUSR_INFOR ,FILE *,FILE *);
void stu_menu(pUSR_ACCOUNT ,pUSR_INFOR,FILE *,FILE *);
void srch_acc(pUSR_ACCOUNT ,pUSR_INFOR ,FILE *,FILE *);
void del_acc(pUSR_ACCOUNT ,pUSR_INFOR ,FILE *,FILE *fp) ;
void mod_acc(pUSR_ACCOUNT ,pUSR_INFOR ,FILE *,FILE *);
void add_acc(pUSR_ACCOUNT,pUSR_INFOR ,FILE *,FILE *);
void del_stu(pUSR_ACCOUNT ,pUSR_INFOR ,FILE *,FILE *);
void mod_stu(pUSR_ACCOUNT,pUSR_INFOR ,FILE *,FILE *);
void add_stu(pUSR_ACCOUNT ,pUSR_INFOR ,FILE *,FILE *);
void srch_stu_num(pUSR_ACCOUNT ,pUSR_INFOR ,FILE *,FILE *);
void srch_stu_num1(pUSR_ACCOUNT ,pUSR_INFOR ,FILE *,FILE *);
void srch_stu_name1(pUSR_ACCOUNT ,pUSR_INFOR ,FILE *,FILE *);
void srch_stu_name(pUSR_ACCOUNT ,pUSR_INFOR ,FILE *,FILE *);
void all_stu_print(pUSR_ACCOUNT ,pUSR_INFOR ,FILE *,FILE *);
void srch_stu(pUSR_ACCOUNT ,pUSR_INFOR ,FILE *,FILE *);
func.c文件
#include"func.h"
void scan_fp_list(pUSR_ACCOUNT *pphead,pUSR_ACCOUNT *pptail) //将账号信息存入链表
{
char a[100];
FILE *fp;
pUSR_ACCOUNT pnew;
fp=fopen("usr_account.txt","r");
while(memset(a,0,sizeof(a)),fgets(a,100,fp))
{
pnew=(pUSR_ACCOUNT)malloc(sizeof(USR_ACCOUNT));
memset(pnew,0,sizeof(USR_ACCOUNT));
sscanf(a,"%s%s%d",pnew->usr_name,pnew->usr_pwd,&pnew->usr_role);
if(*pphead==NULL)
{
*pphead=pnew;
*pptail=pnew;
}else{
pnew->pNext_Usr_Account=*pphead;
*pphead=pnew;
}
}
fclose(fp);
}
void scan_fp_list1(pUSR_INFOR *pphead,pUSR_INFOR *pptail) //将学生成绩信息存入链表
{
char a[100];
FILE *fp;
pUSR_INFOR pnew;
fp=fopen("usr_infor.txt","r");
while(memset(a,0,sizeof(a)),fgets(a,100,fp))
{
pnew=(pUSR_INFOR)malloc(sizeof(USR_INFOR));
memset(pnew,0,sizeof(USR_INFOR));
sscanf(a,"%d%s%f%f%f",&pnew->usr_id,&pnew->usr_name,&pnew->usr_course_score[0],&pnew->usr_course_score[1],&pnew->usr_course_score[2]);
if(*pphead==NULL)
{
*pphead=pnew;
*pptail=pnew;
}else{
pnew->pNext_Usr_Infor=*pphead;
*pphead=pnew;
}
}
fclose(fp);
}
void start_top() //打印页面开头
{
printf("*******************************************************************************\n");
printf("********************学 生 信 息 管 理 系 统**************\n");
printf("*******************************************************************************\n");
}
void mng_menu(pUSR_ACCOUNT phead_account,pUSR_INFOR phead_infor,FILE *fp,FILE *fp1) //管理一级界面 //管理员界面
{
int i;
system("cls");
start_top();
printf("\n\n\n\t\t\t\t1.查看学生信息\n");
printf("\t\t\t\t2.添加学生信息\n");
printf("\t\t\t\t3.修改学生信息\n");
printf("\t\t\t\t4.删除学生信息\n");
printf("\t\t\t\t5.增加账号信息\n");
printf("\t\t\t\t6.更改账号信息\n");
printf("\t\t\t\t7.删除账号信息\n");
printf("\t\t\t\t8.查看账号信息\n");
printf("\t\t\t\t9.退出系统\n");
printf("\n\n请输入您的选择:");
scanf("%d",&i);
switch (i)
{
case 1:srch_stu(phead_account,phead_infor,fp,fp1);break;
case 2:add_stu(phead_account,phead_infor,fp,fp1);break;
case 3:mod_stu(phead_account,phead_infor,fp,fp1);break;
case 4:del_stu(phead_account,phead_infor,fp,fp1);break;
case 5:add_acc(phead_account,phead_infor,fp,fp1);break;
case 6:mod_acc(phead_account,phead_infor,fp,fp1);break;
case 7:del_acc(phead_account,phead_infor,fp,fp1);break;
case 8:srch_acc(phead_account,phead_infor,fp,fp1);break;
case 9:;break; //退出系统
}
system("cls");
}
void stu_menu(pUSR_ACCOUNT phead_account,pUSR_INFOR phead_infor,FILE *fp,FILE *fp1) //学生界面
{
int i;
system("cls");
start_top();
printf("\n\n\n\t\t\t\t1.通过姓名查找\n");
printf("\t\t\t\t2.通过学号查找\n");
printf("\t\t\t\t3.返回\n");
printf("请输入您的选择:");
scanf("%d",&i);
switch(i)
{
case 1:srch_stu_name1(phead_account,phead_infor,fp,fp1);break;
case 2:srch_stu_num1(phead_account,phead_infor,fp,fp1);break;
case 3:mng_menu(phead_account,phead_infor,fp,fp1); //返回登陆界面
}
}
void srch_stu(pUSR_ACCOUNT phead_account,pUSR_INFOR phead_infor,FILE *fp,FILE *fp1) //管理2级进入界面
{
int i;
system("cls");
start_top();
printf("\n\n\n\t\t\t\t0.查看所有学生\n");
printf("\t\t\t\t1.通过姓名查找\n");
printf("\t\t\t\t2.通过学号查找\n");
printf("\t\t\t\t3.返回\n");
printf("请输入您的选择:");
scanf("%d",&i);
switch (i)
{
case 0:all_stu_print(phead_account,phead_infor,fp,fp1);break;
case 1:srch_stu_name(phead_account,phead_infor,fp,fp1);break;
case 2:srch_stu_num(phead_account,phead_infor,fp,fp1);break;
case 3:mng_menu(phead_account,phead_infor,fp,fp1);
}
}
void all_stu_print(pUSR_ACCOUNT phead_account,pUSR_INFOR phead_infor,FILE *fp,FILE *fp1) //管理查看所有学生成绩
{
pUSR_INFOR p;
system("cls");
start_top();
printf("\n\n\t\t\t\t 所有学生信息\n\n");
printf("\t\t学号\t姓名\t\t数学\t语文\t英语\n");
p=phead_infor;
while(p)
{
printf("\t\t%d\t%15s\t%5.2f\t%5.2f\t%5.2f\t\n",p->usr_id,p->usr_name,p->usr_course_score[0],p->usr_course_score[1],p->usr_course_score[2]);
p=p->pNext_Usr_Infor;
}
printf("按任意键退出\n");
fflush(stdin);
getchar();
srch_stu(phead_account,phead_infor,fp,fp1);
}
void srch_stu_name(pUSR_ACCOUNT phead_account,pUSR_INFOR phead_infor,FILE *fp,FILE *fp1) //管理根据姓名查学生成绩
{
char a[20];
int i=0;
pUSR_INFOR p;
memset(a,0,sizeof(a));
system("cls");
start_top();
printf("\n\n请输入学生姓名:");
scanf("%s",a);
p=phead_infor;
while(p)
{
if(strcmp(p->usr_name,a)==0)
{
printf("\t\t学号\t姓名\t\t数学\t语文\t英语\n");
printf("\t\t%d\t%15s\t%5.2f\t%5.2f\t%5.2f\t\n",p->usr_id,p->usr_name,p->usr_course_score[0],p->usr_course_score[1],p->usr_course_score[2]);
i++;
}
p=p->pNext_Usr_Infor;
}
if(NULL==p&&0==i)
printf("\n无此学生信息\n");
printf("按任意建返回上一层\n");
fflush(stdin);
getchar();
srch_stu(phead_account,phead_infor,fp,fp1);
}
void srch_stu_name1(pUSR_ACCOUNT phead_account,pUSR_INFOR phead_infor,FILE *fp,FILE *fp1) //学生根据姓名查成绩
{
char a[20];
int i=0;
pUSR_INFOR p;
memset(a,0,sizeof(a));
system("cls");
start_top();
printf("\n\n请输入学生姓名:");
scanf("%s",a);
p=phead_infor;
while(p)
{
if(strcmp(p->usr_name,a)==0)
{
printf("\t\t学号\t姓名\t\t数学\t语文\t英语\n");
printf("\t\t%d\t%15s\t%5.2f\t%5.2f\t%5.2f\t\n",p->usr_id,p->usr_name,p->usr_course_score[0],p->usr_course_score[1],p->usr_course_score[2]);
i++;
}
p=p->pNext_Usr_Infor;
}
if(NULL==p&&0==i)
printf("\n无此学生信息\n");
printf("按任意建返回上一层\n");
fflush(stdin);
getchar();
stu_menu(phead_account,phead_infor,fp,fp1);
}
void srch_stu_num1(pUSR_ACCOUNT phead_account,pUSR_INFOR phead_infor,FILE *fp,FILE *fp1) //学生通过学号查成绩
{
int i=0;
int a=0;
pUSR_INFOR p;
system("cls");
start_top();
printf("\n\n请输入学生学号:");
scanf("%d",&a);
p=phead_infor;
while(p)
{
if(p->usr_id==a)
{
printf("\t\t学号\t姓名\t\t数学\t语文\t英语\n");
printf("\t\t%d\t%15s\t%5.2f\t%5.2f\t%5.2f\t\n",p->usr_id,p->usr_name,p->usr_course_score[0],p->usr_course_score[1],p->usr_course_score[2]);
i++;
}
p=p->pNext_Usr_Infor;
}
if(NULL==p&&0==i)
printf("\n无此学生信息\n");
printf("按任意建返回上一层\n");
fflush(stdin);
getchar();
stu_menu(phead_account,phead_infor,fp,fp1);
}
void srch_stu_num(pUSR_ACCOUNT phead_account,pUSR_INFOR phead_infor,FILE *fp,FILE *fp1) //管理通过学号查询学生成绩
{
int i=0;
int a=0;
pUSR_INFOR p;
system("cls");
start_top();
printf("\n\n请输入学生学号:");
scanf("%d",&a);
p=phead_infor;
while(p)
{
if(p->usr_id==a)
{
printf("\t\t学号\t姓名\t\t数学\t语文\t英语\n");
printf("\t\t%d\t%15s\t%5.2f\t%5.2f\t%5.2f\t\n",p->usr_id,p->usr_name,p->usr_course_score[0],p->usr_course_score[1],p->usr_course_score[2]);
i++;
}
p=p->pNext_Usr_Infor;
}
if(NULL==p&&0==i)
printf("\n无此学生信息\n");
printf("按任意建返回上一层\n");
fflush(stdin);
getchar();
srch_stu(phead_account,phead_infor,fp,fp1);
}
void add_stu(pUSR_ACCOUNT pphead1,pUSR_INFOR pphead,FILE *fp,FILE *fp1) //增加学生信息
{
int i;
char a[20]={‘0‘};
float b,c,d;
pUSR_INFOR p;
pUSR_INFOR pnew=(pUSR_INFOR)malloc(sizeof(USR_INFOR));
memset(pnew,0,sizeof(USR_INFOR));
out:
system("cls");
start_top();
printf("\n\n请输入要添加的学生信息:");
printf("\t\t学号\t姓名\t\t数学\t语文\t英语\n");
scanf("%d%s%f%f%f",&i,a,&b,&c,&d);
p=pphead;
while(p)
{
if(i==p->usr_id)
{
printf("\n学号重复,按任意键重新输入");
getchar();
memset(a,0,sizeof(a));
goto out;
}
p=p->pNext_Usr_Infor;
}
pnew->usr_id=i;
strcpy(pnew->usr_name,a);
pnew->usr_course_score[0]=b;
pnew->usr_course_score[1]=c;
pnew->usr_course_score[2]=d;
pnew->pNext_Usr_Infor=pphead;
pphead=pnew;
fp1=fopen("usr_infor.txt","w+");
p=pphead;
while(p)
{
fprintf(fp1,"\t%d\t%s\t%6.2f\t%6.2f\t%6.2f\n",p->usr_id,p->usr_name,p->usr_course_score[0],p->usr_course_score[1],p->usr_course_score[2]);
p=p->pNext_Usr_Infor;
}
fclose(fp1);
printf("添加学生信息成功\n");
printf("按任意键返回上层");
fflush(stdin);
getchar();
mng_menu(pphead1,pphead,fp,fp1) ;
}
void mod_stu(pUSR_ACCOUNT pphead1,pUSR_INFOR pphead,FILE *fp,FILE *fp1) //管理修改学生信息
{
pUSR_INFOR p;
int a;
char k[20]={‘0‘};
float b,c,d;
ou:
system("cls");
start_top();
printf("请输入要修改学生的学号:\n");
scanf("%d",&a);
while(p)
{
if(a==p->usr_id)
{
goto out;
}
p=p->pNext_Usr_Infor;
if(NULL==p)
{
printf("没有该学生,请按任意键重新输入");
fflush(stdin);
getchar();
goto ou;
}
}
out:
printf("\n\n请输入要修改的学生信息:");
printf("\t\t姓名\t\t数学\t语文\t英语\n");
scanf("%s%f%f%f",k,&b,&c,&d);
strcpy(p->usr_name,k);
p->usr_course_score[0]=b;
p->usr_course_score[1]=c;
p->usr_course_score[2]=d;
p=pphead;
fp1=fopen("usr_infor.txt","w+");
while(p)
{
fprintf(fp1,"\t%d\t%s\t%6.2f\t%6.2f\t%6.2f\n",p->usr_id,p->usr_name,p->usr_course_score[0],p->usr_course_score[1],p->usr_course_score[2]);
p=p->pNext_Usr_Infor;
}
fclose(fp1);
printf("修改学生信息成功");
printf("按任意键返回上一级");
fflush(stdin);
getchar();
mng_menu(pphead1,pphead,fp,fp1) ;
}
void del_stu(pUSR_ACCOUNT pphead1,pUSR_INFOR pphead,FILE *fp,FILE *fp1) //管理删除学生信息
{
pUSR_INFOR p,ppre;
int a;
ou:
system("cls");
start_top();
printf("请输入要删除学生的学号:");
scanf("%d",&a);
p=pphead;
ppre=pphead;
while(p)
{
if(a==p->usr_id)
{
goto out;
}
ppre=p;
p=p->pNext_Usr_Infor;
if(NULL==p)
{
printf("没有该学生,请按任意键重新输入");
getchar();
goto ou;
}
}
out:
if(p==ppre)
{
pphead=p->pNext_Usr_Infor;
free(p);
}else{
ppre->pNext_Usr_Infor=p->pNext_Usr_Infor;
free(p);
}
fp1=fopen("usr_infor.txt","w+");
p=pphead;
while(p)
{
fprintf(fp1,"\t%d\t%s\t%6.2f\t%6.2f\t%6.2f\n",p->usr_id,p->usr_name,p->usr_course_score[0],p->usr_course_score[1],p->usr_course_score[2]);
p=p->pNext_Usr_Infor;
}
fclose(fp1);
printf("已删除该学生信息\n");
printf("按任意键返回上一级");
fflush(stdin);
getchar();
mng_menu(pphead1,pphead,fp,fp1) ;
}
void add_acc(pUSR_ACCOUNT pphead1,pUSR_INFOR pphead,FILE *fp,FILE *fp1) //添加账号信息
{
char a[20]={‘0‘};
char b[20]={‘0‘};
int c;
pUSR_ACCOUNT p;
pUSR_ACCOUNT pnew=(pUSR_ACCOUNT)malloc(sizeof(USR_ACCOUNT));
memset(pnew,0,sizeof(USR_ACCOUNT));
out:
system("cls");
start_top();
printf("请输入要添加的账号信息\n");
printf("\t\t姓名\t\t密码\t权限\n");
scanf("\t%s\t\t%s\t%d",a,b,&c);
p=pphead1;
while(p)
{
if(strcmp(a,p->usr_name)==0)
{
printf("\n账号重复,按任意键重新输入");
getchar();
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
goto out;
}
p=p->pNext_Usr_Account;
}
strcpy(pnew->usr_name,a);
strcpy(pnew->usr_pwd,b);
pnew->usr_role=c;
pnew->pNext_Usr_Account=pphead1;
pphead1=pnew;
p=pphead1;
fp=fopen("usr_account.txt","w+");
while(p)
{
fprintf(fp,"\t%s\t%s\t%d\t\n",p->usr_name,p->usr_pwd,p->usr_role);
p=p->pNext_Usr_Account;
}
fclose(fp);
printf("添加账号成功\n");
printf("输入任意键返回上一级");
fflush(stdin);
getchar();
mng_menu(pphead1,pphead,fp,fp1) ;
}
void mod_acc(pUSR_ACCOUNT pphead1,pUSR_INFOR pphead,FILE *fp,FILE *fp1) //更改账号信息
{
char a[20]={‘0‘};
char b[20]={‘0‘};
int c;
pUSR_ACCOUNT p;
ou:
system("cls");
start_top();
printf("请输入要更改的账户名");
scanf("%s",a);
p=pphead1;
while(p)
{
if(strcmp(a,p->usr_name)==0)
{
goto out;
}
p=p->pNext_Usr_Account;
if(p==NULL)
{
printf("没有该账号,请按任意键重新输入");
getchar();
memset(a,0,sizeof(a));
goto ou;
}
}
out:
printf("请输入要更改的信息\n");
printf("\t密码\t\t权限");
scanf("\t%s\t\t%d",b,&c);
strcpy(p->usr_pwd,b);
p->usr_role=c;
p=pphead1;
fp=fopen("usr_account.txt","w+");
while(p)
{
fprintf(fp,"\t%s\t%s\t%d\t\n",p->usr_name,p->usr_pwd,p->usr_role);
p=p->pNext_Usr_Account;
}
fclose(fp);
printf("账号修改成功,按任意键返回上一级");
fflush(stdin);
getchar();
mng_menu(pphead1,pphead,fp,fp1) ;
}
void del_acc(pUSR_ACCOUNT pphead1,pUSR_INFOR pphead,FILE *fp,FILE *fp1) //删除账号信息
{
pUSR_ACCOUNT p,ppre;
char a[20]={‘a‘};
ou:
system("cls");
start_top();
printf("请输入要删除的账户名");
scanf("%s",a);
p=pphead1;
ppre=pphead1;
while(p)
{
if(strcmp(p->usr_name,a)==0)
{
goto out;
}
ppre=p;
p=p->pNext_Usr_Account;
if(NULL==p)
{
printf("没有该账号,请按任意键重新输入");
fflush(stdin);
getchar();
goto ou;
}
}
out:
if(p==ppre)
{
pphead1=p->pNext_Usr_Account;
free(p);
}else{
ppre->pNext_Usr_Account=p->pNext_Usr_Account;
free(p);
}
fp=fopen("usr_account.txt","w+");
p=pphead1;
while(p)
{
fprintf(fp,"\t%s\t%s\t%d\t\n",p->usr_name,p->usr_pwd,p->usr_role);
p=p->pNext_Usr_Account;
}
fclose(fp);
printf("已删除该账号信息\n");
printf("按任意键返回上一级");
fflush(stdin);
getchar();
mng_menu(pphead1,pphead,fp,fp1) ;
}
void srch_acc(pUSR_ACCOUNT pphead1,pUSR_INFOR pphead,FILE *fp,FILE *fp1) //管理查看所有账号信息
{
pUSR_ACCOUNT p;
system("cls");
start_top();
printf("\n\n\t\t\t\t 所有账号信息\n\n");
printf("\t\t姓名\t密码\t权限\n");
p=pphead1;
while(p)
{
printf("\t\t%15s\t%15s\t%d\t\n",p->usr_name,p->usr_pwd,p->usr_role);
p=p->pNext_Usr_Account;
}
printf("按任意键退出\n");
fflush(stdin);
getchar();
mng_menu(pphead1,pphead,fp,fp1);
}
usr_account.txt文件
wusheng 2009 1
yangkai 12306 1
qinger 5201314 1
dajiushen 201009 2
zhaosi 99428 2
falaowang 99628 2
rendahua 123 2
usr_infor.txt文件
1001 wusheng 100.00 98.00 99.00
1002 dajiushen 87.00 91.00 83.00
1003 yangkai 70.00 75.00 81.00
1004 zhaosi 33.00 24.00 26.00
1005 falaowang 45.00 56.00 71.00
1006 qinger 91.00 81.00 86.00
1007 rendahua 11.00 9.00 14.00