刚编的代码 运行后第一次输入正确密码结果是密码错误 哪里不对? 请教大神们
#include "stdio.h" #include "malloc.h"
#include "stdlib.h"
#include "string.h"
#define N 3
typedef struct stu
{
char no[11];
char name[15];
char zhanghu[15];
char miaoshu[15];
char mima[15];
float score[N];
float sum;
float average;
int order;
struct stu *next;
}STUDENT;
int menu_select()
{
char s[3];
int c;
printf("\n ***************主菜单**************\n");
printf(" 1. 输入用户信息\n");
printf(" 2. 显示所有记录\n");
printf(" 3. 对所有记录进行排序\n");
printf(" 4. 按账号位置查找记录并显示\n");
printf(" 5. 插入记录\n");
printf(" 6. 删除记录\n");
printf(" 7. 将所有记录保存到文件\n");
printf(" 8. 从文件中读入所有记录\n");
printf(" 9. 退出\n");
printf(" ***********************************\n\n");
do
{
printf(" 请选择操作(1-9):");
scanf("%s",s);
c=atoi(s);
}while(c<0||c>9);
return(c);
}
STUDENT *create()
{
float s;
STUDENT *h=NULL,*info;
for(;;)
{
info=(STUDENT *)malloc(sizeof(STUDENT));
if(!info)
{
printf("\n内存分配失败");
return NULL;
}
printf("\n请按如下提示输入相关信息.\n\n");
printf("输入编号(输入'#'结束):");
scanf("%s",info->no);
if(info->no[0]=='#') break;
printf("输入帐号位置:");
scanf("%s",info->name);
printf("输入账号描述:");
scanf("%s",info->miaoshu);
printf("输入账号名:");
scanf("%s",info->zhanghu);
printf("输入密码:");
scanf("%s",info->mima);
s=0;
info->sum=s;
info->average=(float)s/N;
info->order=0;
info->next=h;
h=info;
}
return(h);
}
/*显示模块*/
void print(STUDENT *h)
{
int i=0;
STUDENT *p;
p=h;
if(p==NULL)
{
printf("\n 很遗憾,空表中没有任何记录可供显示!\n");
}
else
{
printf("********************************** 用户信息 ****************************************\n");
printf(" 序号 编号 帐号位置 账号描述 账号名 密码\n");
printf("-----------------------------------------------------------------------------------\n");
while(p!=NULL)
{
i++;
printf(" %-4d %-8s %-8s %-8s %-8s %-8s \n", i, p->no, p->name, p->miaoshu, p->zhanghu, p->mima);
p=p->next;
}
printf("***********************************************************************************\n\n");
}
}
STUDENT *sort(STUDENT *h)
{
int i=0;
STUDENT *p,*q,*t,*h1;
h1=h->next;
h->next=NULL;
while(h1!=NULL)
{
t=h1;
h1=h1->next;
p=h;
q=h;
while(p!=NULL&&t->sum<p->sum)
{
q=p;
p=p->next;
}
if(p==q)
{
t->next=p;
h=t;
}
else
{
t->next=p;
q->next=t;
}
}
p=h;
while(p!=NULL)
{
i++;
p->order=i;
p=p->next;
}
printf("按记录时间从上到下排序成功!!!\n");
return (h);
}
void search(STUDENT *h)
{
STUDENT *p;
char s[15];
printf("请输入您要查找的帐号位置:");
scanf("%s",s);
p=h;
while(p!=NULL&&strcmp(p->name,s))
p=p->next;
if(p==NULL)
printf("\n您要查找的是%s,很遗憾,查无此帐号信息!\n",s);
else
{
printf("*********************************** Found *****************************************\n");
printf(" 编号 帐号位置 账号描述 账号名 密码\n");
printf("-----------------------------------------------------------------------------------\n");
printf("%7s %-8s %-8s %-8s %-8s \n", p->no, p->name, p->miaoshu, p->zhanghu, p->mima);
printf("***********************************************************************************\n");
}
}
STUDENT *insert(STUDENT *h)
{
STUDENT *info;
int n=0;
float s1;
printf("\n请添加新记录!\n");
info=(STUDENT *)malloc(sizeof(STUDENT));
if(!info)
{
printf("\n内存分配失败!");
return NULL;
}
printf("\n请按如下提示输入相关信息.\n\n");
printf("输入编号(输入'#'结束):");
scanf("%s",info->no);
printf("输入帐号位置:");
scanf("%s",info->name);
printf("输入账号描述:");
scanf("%s",info->miaoshu);
printf("输入账号名:");
scanf("%s",info->zhanghu);
printf("输入密码:");
scanf("%s",info->mima);
s1=0;
info->sum=s1;
info->average=(float)s1/N;
info->order=0;
info->next=NULL;
info->next=h;
h=info;
printf("\n ----已经添加帐号位置为: %s 到记录中----\n",info->name);
return(h);
}
STUDENT *delete1(STUDENT *h)
{
char k[5];
STUDENT *p,*q;
char s[11];
printf("请输入要删除用户的编号:");
scanf("%s",s);
q=p=h;
while(p!=NULL&&strcmp(p->no,s))
{
q=p;
p=p->next;
}
if(p==NULL)
printf("\n很遗憾,记录中没有您要删除的编号为%s的用户!\n",s);
else
{
printf("*********************************** Found *****************************************\n");
printf(" 编号 帐号位置 账号描述 账号名 密码\n");
printf("-----------------------------------------------------------------------------------\n");
printf("%7s %-8s %-8s %-8s %-8s \n", p->no, p->name, p->miaoshu, p->zhanghu, p->mima);
printf("***********************************************************************************\n");
do{
printf("您确实要删除此记录吗?(y/n):");
scanf("%s",k);
}while(k[0]!='y'&&k[0]!='n');
if(k[0]!='n')
{
if(p==h)
h=p->next;
else
q->next=p->next;
free(p);
printf("\n 已经成功删除编号为 %s 的用户的记录!\n",s);
}
}
return(h);
}
void save(STUDENT *h)
{
FILE *fp;
STUDENT *p;
char outfile[20];
printf("请输入导出文件名,例如:d:\\xds\\score.txt:\n");
scanf("%s",outfile);
if((fp=fopen(outfile,"wb"))==NULL)
{
printf("不能打开文件,文件保存失败!\n");
}
else
{
p=h;
while(p!=NULL)
{
fwrite(p,sizeof(STUDENT),1,fp);
p=p->next;
}
fclose(fp);
printf("-----所有记录已经成功保存至文件%s中!-----\n",outfile);
}
}
STUDENT *load()
{
STUDENT *p,*q,*h=NULL;
FILE *fp;
char infile[20];
printf("请输入导入文件名,例如:d:\\xds\\score.txt:\n");
scanf("%s",infile);
if((fp=fopen(infile,"rb"))==NULL)
{
printf("文件打开失败!\n");
return h;
}
p=(STUDENT *)malloc(sizeof(STUDENT));
if(!p)
{
printf("内存分配失败!\n");
return h;
}
h=p;
while(!feof(fp))
{
if(1!=fread(p,sizeof(STUDENT),1,fp))
break;
p->next=(STUDENT *)malloc(sizeof(STUDENT));
if(!p->next)
{
printf("内存分配失败!\n");
return h;
}
q=p;
p=p->next;
}
q->next=NULL;
fclose(fp);
printf("已经成功从文件%s导入数据!!!\n",infile);
return h;
}
int main()
{
char s2[30]="123456";
char mi[30]={0};
STUDENT *head = NULL;
system("color A");
printf(" ********************欢迎使用隐私信息管理系统**********************\n\n请输入口令密码(共3次机会):\n");
getchar();
gets(mi);
if(strcmp(s2,mi)==0)
{
printf("\n");
printf("口令正确\n\n");
for(;;)
{
switch(menu_select())
{
case 1: head=create();break;
case 2: print(head);break;
case 3: head=sort(head);break;
case 4: search(head);break;
case 5: head=insert(head);break;
case 6: head=delete1(head);break;
case 7: save(head);break;
case 8: head=load();break;
case 9: exit(0);
}
}
}
else
printf("密码错误,请重新输入(还有2次机会):\n");
gets(mi);
if(strcmp(s2,mi)==0)
{printf("\n");
printf("口令正确\n\n ");
for(;;)
{
switch(menu_select())
{
case 1: head=create();break;
case 2: print(head);break;
case 3: head=sort(head);break;
case 4: search(head);break;
case 5: head=insert(head);break;
case 6: head=delete1(head);break;
case 7: save(head);break;
case 8: head=load();break;
case 9: exit(0);
}
}
}
else
printf("密码错误,请重新输入(还有1次机会):\n");
gets(mi);
if(strcmp(s2,mi)==0)
{printf("\n");
printf("口令正确\n\n ");
for(;;)
{
switch(menu_select())
{
case 1: head=create();break;
case 2: print(head);break;
case 3: head=sort(head);break;
case 4: search(head);break;
case 5: head=insert(head);break;
case 6: head=delete1(head);break;
case 7: save(head);break;
case 8: head=load();break;
case 9: exit(0);
}
}
}
else
printf("您是非法用户!\n");
getchar();
return 0;
}