C语言用c写一个可以验证账号,密码和修改密码的程序
2个回答
展开全部
#include <string.h>
struct e
{
char a[10];
char b[10];
}z;
int main()
{ int t=0;
char s[10],d[10];
FILE *p;
void as();
if ((p=fopen("m.txt","r+"))==NULL)
{
p=fopen("m.txt","w+");
t=1;
}
if(t==1)
{
printf("当前没有任何用户\n");
printf("请新建用户名: ");
scanf("%s",s);
printf("为用户设置密码: ");
scanf("%s",d);
strcpy(z.a,s);
strcpy(z.b,d);
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
}
if(t==0)
{
printf("请输入用户名: ");
scanf("%s",s);
fscanf(p,"%s %s",z.a,z.b);
fclose(p);
if (!strcmp(z.a,s))
{
printf("请输入密码:");
scanf("%s",d);getchar();
if(!strcmp(z.b,d))
{ char i;
printf("是否要修改密码?(输入y修改,n退出!)");
scanf("%c",&i);
if(i=='y')
{
printf("请输入修改密码:");
scanf("%s",z.b);
p=fopen("m.txt","w+");
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
printf("修改成功!");
}
}
else printf("密码错误!");
}
else printf("用户名错误");
fclose(p);
}
}
struct e
{
char a[10];
char b[10];
}z;
int main()
{ int t=0;
char s[10],d[10];
FILE *p;
void as();
if ((p=fopen("m.txt","r+"))==NULL)
{
p=fopen("m.txt","w+");
t=1;
}
if(t==1)
{
printf("当前没有任何用户\n");
printf("请新建用户名: ");
scanf("%s",s);
printf("为用户设置密码: ");
scanf("%s",d);
strcpy(z.a,s);
strcpy(z.b,d);
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
}
if(t==0)
{
printf("请输入用户名: ");
scanf("%s",s);
fscanf(p,"%s %s",z.a,z.b);
fclose(p);
if (!strcmp(z.a,s))
{
printf("请输入密码:");
scanf("%s",d);getchar();
if(!strcmp(z.b,d))
{ char i;
printf("是否要修改密码?(输入y修改,n退出!)");
scanf("%c",&i);
if(i=='y')
{
printf("请输入修改密码:");
scanf("%s",z.b);
p=fopen("m.txt","w+");
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
printf("修改成功!");
}
}
else printf("密码错误!");
}
else printf("用户名错误");
fclose(p);
}
}
展开全部
#include <stdio.h>
#include <string.h>
#define MAX_ACCOUNTS 32
#define MAX_NAME_LEN 32
#define MAX_PWD_LEN 32
#define REGIST 1
#define LOGIN 2
#define MODIFY 3
#define DELETE 4
typedef struct account
{
int flag;
char name[MAX_NAME_LEN];
char password[MAX_PWD_LEN];
}ACCOUNT_T;
ACCOUNT_T g_user[MAX_ACCOUNTS] = {0};
void input(ACCOUNT_T *input)
{
printf("请输入不超过%d位的用户名\n", MAX_NAME_LEN-1);
scanf("%s", input->name);
printf("请输入不超过%d位的密码\n", MAX_NAME_LEN-1);
scanf("%s", input->password);
}
ACCOUNT_T *find()
{
int i = 0;
ACCOUNT_T tmp = {0};
input(&tmp);
for (i = 0; i < MAX_ACCOUNTS; i++)
{
if (1 == g_user[i].flag
&& 0 == strcmp(g_user[i].name, tmp.name)
&& 0 == strcmp(g_user[i].password, tmp.password))
{
return &g_user[i];
}
}
return NULL;
}
ACCOUNT_T *new()
{
int i = 0;
ACCOUNT_T *p = NULL;
for (i = 0; i < MAX_ACCOUNTS; i++)
{
if (0 == g_user[i].flag)
{
p = &g_user[i];
break;
}
}
if (p)
{
input(p);
p->flag = 1;
printf("注册成功\n");
}
else
{
printf("注册失败 最多%d个用户\n", MAX_ACCOUNTS);
}
return p;
}
ACCOUNT_T *modify()
{
ACCOUNT_T *p = NULL;
p = find();
if (p)
{
printf("请输入新的不超过%d位的密码\n", MAX_NAME_LEN-1);
scanf("%s", p->password);
}
else
{
printf("找不到该用户\n");
}
return p;
}
void del()
{
ACCOUNT_T *p = NULL;
p = find();
if (p)
{
memset(p, 0, sizeof(ACCOUNT_T));
printf("删除完毕\n");
}
else
{
printf("找不到该用户\n");
}
return;
}
ACCOUNT_T *login()
{
ACCOUNT_T *p = NULL;
p = find();
if (p)
{
printf("允许登入\n");
}
else
{
printf("不允许登入\n");
}
return p;
}
int main(int argc, char *argv[])
{
int action = 0;
while(1)
{
printf("请输入操作码: %d.注册 %d.登入 %d.修改密码 %d注销\n"
, REGIST, LOGIN, MODIFY, DELETE);
scanf("%d", &action);
switch(action)
{
case REGIST:
new();
break;
case LOGIN:
login();
break;
case MODIFY:
modify();
break;
case DELETE:
del();
break;
default:
printf("不支持的操作%d\n", action);
break;
}
}
return 0;
}
题外话,你真抠,这么多就5分,要不是我太闲了,不会有人帮你写的。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询