C语言编写用户登录程序

C语言编写用户登录程序,第1张

艾达的小刀

#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 <stdlib.h>

#include <stdio.h>

#include <string.h>

bool search(char id[], char pass[]) {

FILE *fp

char tid[10], tpass[10]

fp = fopen("c:\\data", "r")

while (!feof(fp)) {

fscanf(fp, "%s%s", tid, tpass)

if (strcmp(tid, id)==0 &&strcmp(tpass, pass)==0) {

fclose(fp)

return true

}

}

fclose(fp)

return false

}

bool login() {

char id[10], pass[10]

printf("Login\nPress the id: ")

scanf("%s", id)

printf("Press the password: ")

// 可以自行将password处理成*号, 如果不皮宽会可以发信给我

scanf("%s", pass)

printf("-----------------------")

if (search(id, pass))

return true

else

return false

}

void _add(char id[], char pass[]) {

FILE *fp

fp=fopen("c:\\data", "a")

// 在写入文件时可以按一定的排序方式插入,可减少以后Login时的search时间

fprintf(fp, "%s %s\n", id, pass)

fclose(fp)

}

void regis() {

char id[10], pass[10], tpass[10]

printf("Register\nPress the id: ")

scanf("%s", id)

while (true) {

printf("Press the password: ")

scanf("%s", pass)

printf("Press the password again: ")

scanf("%s", tpass)

if (strcmp(pass, tpass) != 0)

printf("The passwords you pressed are not the same!\n")

else

break

}

_add(id, pass)

printf("-----------------------Register successfully!\n")

}

void init() {

FILE *fp

if ((fp=fopen("c:\\data", "r")) == NULL) { // 注意,一定要有个名叫data(没有扩展名)的合法燃敏亮文件在C盘根目录

printf("---------File is not exist\n")

system("pause")

exit(0)

}

else

fclose(fp)

}

int main(void){

int command

init() // 检查data文件在不在

while (true) {

printf("-----------------------(Login: 1 Register: 2 Exit: 3)\n")

scanf("%d", &command)

printf("-----------------------\拿郑n")

// 这里可以编写command的检测语句

if (command == 3)

break

else if (command == 1) {

if (!login())

printf("ID is not exist or password is wrong!\n")

else

printf("Login successfully!\n")

}

else

regis()

}

return 0

}

搞定了。。。我是用成功了的。。。如果有问题就发信给我。。。。


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

原文地址: http://outofmemory.cn/yw/12539000.html

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

发表评论

登录后才能评论

评论列表(0条)

保存