#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
}
搞定了。。。我是用成功了的。。。如果有问题就发信给我。。。。
#include "stdafx.h"\x0d\x0a#include "string.h"\x0d\x0a#define n 20\x0d\x0a\x0d\x0avoid zhuce()\x0d\x0avoid denglu()\x0d\x0achar yhm[n],mm[n]\x0d\x0aint main(int argc, char* argv[])\x0d\x0a{\x0d\x0aint i\x0d\x0a\x0d\x0aprintf("-----------\n1.注册\n2.登岩培键陆\n3.继续\n0.退出\n")\x0d\x0ascanf("%d",&i)\x0d\x0aswitch(i)\x0d\x0a{case 0: break\x0d\x0acase 1 : zhuce()break\x0d\x0acase 2: denglu()break\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0areturn 0\x0d\x0a}\x0d\x0avoid zhuce( )\x0d\x0a{char temp1[n],temp2[n],temp3[n],yhmtmp[n]\x0d\x0a\x0d\x0aprintf("输入用户名\n")\x0d\x0afflush(stdin)//清空缓存粗巧\x0d\x0a gets(yhmtmp)\x0d\x0a\x0d\x0aprintf("输入密码\n")\x0d\x0afflush(stdin)\x0d\x0a gets(temp1)\x0d\x0aprintf("输入密码确认\n")\x0d\x0afflush(stdin)\x0d\x0agets(temp2)\x0d\x0aif(!strcmp(temp1,temp2))\x0d\x0a{strcpy(mm,temp1)\x0d\x0aprintf("注册成功\n")\x0d\x0a\x0d\x0a}\x0d\x0aelse\x0d\x0a{printf("输入密码确认\n")\x0d\x0agets(temp3)\x0d\x0aif(!strcmp(temp1,temp3))\x0d\x0a{strcpy(mm,temp1)\x0d\x0aprintf("注册成功\n")\x0d\x0a\x0d\x0a}\x0d\x0aelse\x0d\x0a\x0d\x0aprintf("注册失败\n")\x0d\x0a\x0d\x0a}\x0d\x0a\x0d\x0a}\x0d\x0avoid denglu( )\x0d\x0a{\x0d\x0achar s1[n],s2[n]\x0d\x0aprintf("输中数入用户名\n")\x0d\x0afflush(stdin)\x0d\x0agets(s1)\x0d\x0aprintf("输入密码\n")\x0d\x0afflush(stdin)\x0d\x0agets(s2)\x0d\x0aif((strcmp(s1,yhm))&&(strcmp(s2,mm)))\x0d\x0aprintf("登陆成功\n")\x0d\x0a\x0d\x0a}欢迎分享,转载请注明来源:内存溢出
评论列表(0条)