#include "stdio.h"
int* set(void);
int* set(void)
{
int i;
static a[4];
printf("請輸入要設置的4位密碼 :");
for(i=0;i<4;i++)
{
scanf("%d",&a[i]);
}
return a;
}
int main()
{
int i,*a,b[4];
a=set();
printf("請輸入4位密碼 :");
for(i=0;i<4;i++)
{
scanf("%d",&b[i]);
}
i=0;
while(i<4)
{
if(a[i]==b[i])
{
printf("%d",a[i]);
i++;
}
else
{
break;
}
}
if(i==4)
{
printf("密碼正確\n");
}
else
{
printf("密碼錯誤\n");
}
return 0;
}
㈡ 如何使用c語言編寫一個密碼程序
密碼保存在文件中,從文件中讀取密碼,但是沒做容錯和異常處理,僅供參考
#include <stdio.h>
#include <string.h>
#define PSDLEN 6
void inputPsd(char *str) /*處理輸入*/
{
int i;
for(i = 0; i < PSDLEN; i++)
{
while(1)
{
str[i] = getch();
if(str[i] == '\b') /*處理退格鍵*/
{
i--;
if(i < 0)
{
i = 0;
}
else
{
printf("\b \b");
}
continue;
}
else if(str[i] == '\r') /*處理回車鍵*/
{
continue;
}
else
{
printf("*");
break;
}
}
}
str[i] = '\0';
printf("\n");
}
int checkFirst() /*檢測是否是第一次使用*/
{
FILE *fp;
if((fp = fopen("psd.dat", "rb")) == NULL)
{
return 1;
}
fclose(fp);
return 0;
}
void firstUse() /*第一次使用 需要輸入密碼*/
{
FILE *fp;
int i;
char passwd[PSDLEN + 1];
char checkPsd[PSDLEN + 1];
if((fp = fopen("psd.dat", "wb")) == NULL)
{
printf("Creat password error!\n");
exit(1);
}
while(1)
{
printf("Please input password:");
inputPsd(passwd);
printf("\nPlease input password again:");
inputPsd(checkPsd);
if(!strcmp(passwd, checkPsd))
{
break;
}
printf("\ncheck password error! \n");
}
fwrite(passwd, sizeof(char), PSDLEN, fp);
fclose(fp);
}
void login() /*核對密碼,並登錄*/
{
FILE *fp;
int i, num = 3;
char passwd[PSDLEN + 1];
char checkPsd[PSDLEN + 1];
if((fp = fopen("psd.dat", "rb")) == NULL)
{
puts("Open psd.dat error");
exit(1);
}
fread(passwd, sizeof(char), PSDLEN, fp);
fclose(fp);
passwd[PSDLEN] = '\0';
printf("Please input password to login");
while(num)
{
printf("you have %d chances to cry:\n", num);
inputPsd(checkPsd);
if(!strcmp(passwd, checkPsd))
{
break;
}
puts("\npassword error,Please input again");
num--;
}
if(!num)
{
puts("Press any key to exit...");
getch();
exit(0);
}
else
{
puts("\n--------\nWelcome!\n--------\n");
}
}
void main()
{
if(checkFirst())
{
firstUse();
}
else
login();
getch();
}
㈢ 如何編譯一個C語言密碼程序,判斷密碼的位數(如5位),如果輸入的密碼小於5位或者大於五位都有相應提示
代碼如下:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
intmain()
{
intlen=0;
charpassword[10]={0};
printf("請輸入密碼:");
scanf("%s",password);
len=strlen(password);
if(len<5){
printf("密碼長度不足! "扮頃);
}
elseif(len>5){
printf("密碼長度過長! ");
}
else{
//默認密碼是銀輪admin
if(strcmp(password,"admin")==0){
printf("登錄成功! ");
}
else{
printf("密碼不鋒缺信正確! ");
}
}
system("pause");
return0;
}
㈣ 如何用c語言編寫英文密碼還要判斷正不正確的那種
#include <stdio.h>
#include <string.h>
int main()
{
char a[100],*b="miku";
改猜 gets(a);
if(strncmp(a,b,strlen(b))==0)
puts("yes");
核畢型 else
數者 puts("no");
return 0;
}
㈤ 請教高手如何用C語言譯密碼
讀出每個字元,然後給它加上4就可以了,如果要還原,就用每個字元減去4.
㈥ 懸賞100分 如何用c語言 寫一個密碼程序
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;
}
餓,寫了我兩個小時啊,大哥,分一定要給我啊~~~~~~
㈦ 請大神幫我用C語言編譯一有關於密碼的程序。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#include<windows.h>
typedefstruct
{
intx;
inty;
}PT_STRUCT;
typedefstruct
{
charuser[20];
charpw[20];
}USER_STRUCT;
PT_STRUCTpt;
boolLoginSuccess=false;
charCurrentUser[20]={'