C语言编写一个用户登陆的程序?

C语言编写一个用户登陆的程序?,第1张

代码如下:

#include<stdio.h>

#pragma warning(disable:4996)

#include<string.h>

int main()

{

int i = 0

char password[10] = { 0 }

printf("请输入密码:")

while (i <3)

{

scanf("%s", password)

printf("\n")

if (strcmp(password, "972816") == 0)

{

printf("登录成功\n")

break

}

else

{

i++

if (i != 3)

printf("再输入一次")

}

}

if (i == 3)

printf("密码错误三次退出登录界面\n")

system("pause")

return 0

扩展资料:

#include后面有两种方式,<>;和""前者先在标准库中查找,查找不到在path中查找。后者为文件路径,若直接是文件名则在项目根目录下查找。

引用方法:#include <stdio.h>

注意事项:在TC2.0中,允许不引用此头文件而直接调用其中的函数,但这种做法是不标准的。也不建议这样做。以避免出现在其他IDE中无法编译或执行的问题。

参考资料来源:百度百科—include

参考资料来源:百度百科—stdio.h

艾达的小刀

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <time.h>

/*随机码产生函数*/

void RandomCode (char Rcode[])

{

int i

srand ((unsigned int)time(NULL))

for (i = 0i <3++i)

Rcode[i] = rand()%10 + '0'

Rcode[i] = '\0'

}

/*登陆函数,判断信息是否匹配,若匹配返回1,否则返回0*/

int LandedApp (char *password[], char Rcode[])

{

char name[10] = {0}

char pword[10] = {0}

char rcode[4] = {0}

printf ("用户名 : ")

gets (name)

printf ("密码 : ")

gets (pword)

printf ("随机码 : ")

gets (rcode)

if (strcmp (name, password[0]) != 0 || strcmp (pword, password[1]) != 0 || strcmp (rcode, Rcode) != 0)

return 0

else

return 1

}

int main ()

{

char * password[2] = {"admin", "admin123"}//用户名和密码

char rc[4] = {0} //随机码

int count = 3 //可输入次数

puts ("请输入用户名,密码和随机码:")

while (count)

{

RandomCode (rc)

printf ("随机码 : %s\n", rc)

if (LandedApp(password, rc) != 0)

break

--count

if (count != 0)

puts ("错误的用户名或密码或随机码,请重新输入: ")

}

if (count != 0)

puts ("\n成功登陆!")

else

puts ("\n登录失败 !")

return 0

}

艾达的小刀

#include <stdio.h>

#include <string.h>

#include<conio.h>

typedef struct

{

char name[30]

char password[20]

}userinfo

FILE *fp

int n //用于存储用户人数

void admin()//管理员建立初始用户信息

int check_info()//返回值为1时表示检查通过,为0时表示登陆失败

int modify_pass()//密码修改,修改成功返回值为1,否则,返回值为 0

void menu() //选择菜单

int main(void)

{

char choice

while(1)

{

menu()

printf("请输入你的选择(1~3)后按回车键:")

fflush(stdin)

choice=getchar()

switch(choice)

{

case '1': admin()break

case '2':

if(check_info())

{

printf("\n你已成功登陆,是否修改密码?(y/n)")

fflush(stdin)

choice =getchar()

if(choice=='y'||choice=='Y')

if(modify_pass()) printf("密码修改成功1\n")

}

else printf("登录失败")

break

case '3': exit (0)

}

}

return 0

}

void admin()//管理员建立初始用户信息

{

int i

userinfo user[30]//最多可以有30个用户

if((fp=fopen("password.txt","wb"))==NULL)

{ printf("系统出错,退出录入\n")exit(1) }

printf("输入要建立的用户的数目(n<=30):")

scanf("%d",&n)

fflush(stdin)

for( i = 0i <ni++ )

{gets(user[i].name)gets(user[i].password)}

for( i = 0i <ni++ )

{ fwrite(&user[i],sizeof(userinfo),1,fp)}

fclose(fp)

}

int check_info()//用户登陆检查

{

int i,j,k,flag=0

char userid[30],password[20]

userinfo us

if((fp=fopen("password.txt","r+"))==NULL)

{ printf("系统出错,退出登录\n")exit(1) }

printf("请输入用户名:")

fflush(stdin)

scanf("%s",userid)

for(i=0i<ni++)

{

fread( &us,sizeof(userinfo),1,fp)

if(strcmp(userid,us.name)==0)

{

flag=1

printf("请输入密码(不要超过19位):")

j=1

while(j<=3)

{

fflush(stdin)

k=0

while( (password[k++]=getch())!='\r')

{

putchar('*')

}

password[--k]='\0'

if(strcmp(us.password,password))

{

printf("\n密码错误,请重新输入")j++

}

else break

}

if(j>3)

{ printf("3次验证结束,程序退出!\n")exit (0)}

else break

}

}

fclose(fp)

return flag

}

void menu()//菜单

{

printf("\n\n\t***********************************\n")

printf("\t\t1、管理员建立密码信息表\n")

printf("\t\t2、用户登陆\n")

printf("\t\t3、退出系统\n")

printf("\t***********************************\n")

}

int modify_pass()//密码修改

{

int i,j=1,k,flag=0

char userid[30],password[20]

userinfo us

if((fp=fopen("password.txt","r+"))==NULL)

{ printf("系统出错,退出登录\n")exit(1) }

printf("请重新输入你的用户名:")

fflush(stdin)

scanf("%s",userid)

for(i=0i<ni++)

{

fread( &us,sizeof(userinfo),1,fp)

if(strcmp(userid,us.name)==0)

{

flag=1

printf("请输入新密码(不要超过19位):")

fflush(stdin)

k=0

while( (password[k++]=getch())!='\r')

{

putchar('*')

}

password[--k]='\0'

strcpy(us.password,password)

break

}

}

if(flag)

{

fseek(fp,-sizeof(userinfo),SEEK_CUR)

fwrite(&us,sizeof(userinfo),1,fp)

}

fclose(fp)

return flag

}


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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存