如何用C语言编写密码程序

如何用C语言编写密码程序,第1张

1、用一个字符数组来存密码

再用一个字符数组接收你的输入,然后用strcmp

来比较,如果返回0则密码是正确的

2、例程:

#include "stdio.h"

#include "string.h"

int main()

{

       char mima[100]="YuanShi888"

       char input[100]={0}

       printf("请输入密码:")

        gets(input)

   含仿岩    if(strcmp(mima,input)==0)

        大灶    printf("恭喜你,密码正确!\n")

       else

            printf("对不起,密码输入错误谈御!\n")

  

}

clude "string.h"

//考虑到用数据库文件保存注册信息的话要使用access创建文件并且还要配置数据源,所以我的方法是采用将注册信息保存到文件

//下面是完整的程序:

//登陆检测函数

int login(char *name,char *password)

{

char info[10000]

char *p=info

FILE *file=fopen("user","r")

int size

if(file)

{

size=fread(info,1,10000,file)

while(size!=(int)p-(int)info)

{

if(!strcmp(p,name)&&!strcmp(p+strlen(p)+1,password))

{

fclose(file)

return 1

}

p+=strlen(p)+1

p+=strlen(p)+1

}

}

fclose(file)

return 0

}

//添加注册信息入文件

void save(char *name,char *password)

{

FILE *file=fopen("user","a")

fwrite(name,1,strlen(name)+1,file)

fwrite(password,1,strlen(password)+1,file)

fclose(file)

}

#define PASSWORD "12345" //这里指定你要允许通过的密码,比如12345,将引号里的数字改为你想要的即可

int main()

{

char password[100]

char name[100],c[100],password1[100]

tag1: printf("press 1 to register, or 2 to login\n")//输入1为注册,输入2为登陆

while(1)

{

gets(c)

if('1'==c[0])

{

printf("please enter your name\n")//输入迅仿姓名

gets(name)

tag2: printf("please enter your password\n")//输入密码

gets(password)

printf("please enter your password again\n")

gets(password1)

if(strcmp(password,password1))

{

//做肆两次密码不一致,重输

printf("the password you entered is different from the first one,please try again!\n")

goto tag2

}

printf("register is completed!\n")//注册成功

//下面实现将注册信息加入文件保存

save(name,password)

goto tag1

}

else if('2'==c[0])

{

tag3: printf("please enter your name:\n")

gets(name)

printf("please enter your password:\n")

gets(password)

if(login(name,password))//如果验证通过,则

{

printf("login successfully!\亩胡纤n")

//这里添加成功登陆后要执行的代码

}

else

{

printf("your name or password doesn't exist!\n")//否则重输

goto tag3

}

}

else

{

printf("invalid input!press 1 to register, or 2 to login\n")//输入非法,重输

goto tag1

}

}

return 0

}

饿,写了我两个小时啊,大哥,分一定要给我啊~~~~~~


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12523735.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-26
下一篇 2023-05-26

发表评论

登录后才能评论

评论列表(0条)

保存