第1个回答 2015-12-11
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
bool search(char id[], char pass[]) {
*fp;
char tid[10], tpass[10];
fp = fopen("c:\\data", "r");
while (!feof(fp)) {
fscanf(fp, "%s%s", tid, tpass);
if (
(tid, id)==0 &&
(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[]) {
*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 (
(pass, tpass) != 0)
printf("The passwords you pressed are not the same!\n");
else
break;
}
_add(id, pass);
printf("-----------------------Register successfully!\n");
}
void init() {
*fp;
if ((fp=fopen("c:\\data", "r")) ==
) { // 注意,一定要有个名叫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;
}
第2个回答 2011-12-22
#include "stdafx.h"
#include "string.h"
#define n 20
void zhuce();
void denglu();
char yhm[n],mm[n];
int main(int argc, char* argv[])
{
int i;
printf("-----------\n1.注册\n2.登陆\n3.继续\n0.退出\n");
scanf("%d",&i);
switch(i)
{case 0: break;
case 1 : zhuce();break;
case 2: denglu();break;
}
return 0;
}
void zhuce( )
{char temp1[n],temp2[n],temp3[n],yhmtmp[n];
printf("输入用户名\n");
fflush(stdin);//清空缓存
gets(yhmtmp);
printf("输入密码\n");
fflush(stdin);
gets(temp1);
printf("输入密码确认\n");
fflush(stdin);
gets(temp2);
if(!strcmp(temp1,temp2))
{strcpy(mm,temp1);
printf("注册成功\n");
}
else
{printf("输入密码确认\n");
gets(temp3);
if(!strcmp(temp1,temp3))
{strcpy(mm,temp1);
printf("注册成功\n");
}
else
printf("注册失败\n");
}
}
void denglu( )
{
char s1[n],s2[n];
printf("输入用户名\n");
fflush(stdin);
gets(s1);
printf("输入密码\n");
fflush(stdin);
gets(s2);
if((strcmp(s1,yhm))&&(strcmp(s2,mm)))
printf("登陆成功\n");
}
还没实现在一次运行中及注册又可以登陆,所以你要登陆的话第二次运行此程序
追问运行不了啊
追答?可以的
本回答被提问者和网友采纳
第3个回答 2011-12-25
#include "stdafx.h"
#include "string.h"
#define n 20
void zhuce();
void denglu();
char yhm[n],mm[n];
int main(int argc, char* argv[])
{
int i;
printf("-----------\n1.注册\n2.登陆\n3.继续\n0.退出\n");
scanf("%d",&i);
switch(i)
{case 0: break;
case 1 : zhuce();break;
case 2: denglu();break;
}
return 0;
}
void zhuce( )
{char temp1[n],temp2[n],temp3[n],yhmtmp[n];
printf("输入用户名\n");
fflush(stdin);//清空缓存
gets(yhmtmp);
printf("输入密码\n");
fflush(stdin);
gets(temp1);
printf("输入密码确认\n");
fflush(stdin);
gets(temp2);
if(!strcmp(temp1,temp2))
{strcpy(mm,temp1);
printf("注册成功\n");
}
else
{printf("输入密码确认\n");
gets(temp3);
if(!strcmp(temp1,temp3))
{strcpy(mm,temp1);
printf("注册成功\n");
}
else
printf("注册失败\n");
}
}
void denglu( )
{
char s1[n],s2[n];
printf("输入用户名\n");
fflush(stdin);
gets(s1);
printf("输入密码\n");
fflush(stdin);
gets(s2);
if((strcmp(s1,yhm))&&(strcmp(s2,mm)))
printf("登陆成功\n");
}