當前位置:首頁 » 編程語言 » c語言設計登錄密碼程序
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言設計登錄密碼程序

發布時間: 2022-05-26 04:52:31

㈠ 如何使用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語言寫一個創建用戶名和密碼並且能修改密碼的程序

#include <stdio.h>//我自己做的,你拿去用吧!
#include <string.h>
struct e
{
char a[10];
char b[10];
}z;
int main()
{ int t=0;
char s[10],d[10];
FILE *p;
void as();
if ((p=fopen("m.txt","r+"))==NULL)
{
p=fopen("m.txt","w+");
t=1;
}
if(t==1)
{
printf("當前沒有任何用戶\n");
printf("請新建用戶名: ");
scanf("%s",s);
printf("為用戶設置密碼: ");
scanf("%s",d);
strcpy(z.a,s);
strcpy(z.b,d);
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
}
if(t==0)
{
printf("請輸入用戶名: ");
scanf("%s",s);
fscanf(p,"%s %s",z.a,z.b);
fclose(p);
if (!strcmp(z.a,s))
{
printf("請輸入密碼:");
scanf("%s",d);getchar();
if(!strcmp(z.b,d))
{ char i;
printf("是否要修改密碼?(輸入y修改,n退出!)");
scanf("%c",&i);
if(i=='y')
{
printf("請輸入修改密碼:");
scanf("%s",z.b);
p=fopen("m.txt","w+");
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
printf("修改成功!");

}

}
else printf("密碼錯誤!");
}
else printf("用戶名錯誤");
fclose(p);

}

}

㈢ C語言編寫用戶登錄程序

艾達的小刀
#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 = 0; i < 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;
}
艾達的小刀

㈣ C語言密碼程序設計

手寫的,沒運行,你自己試一下

#include"stdio.h"
#include"stdlib.h"
#include"string.h"
intmain()
{
char st[6];
printf("輸入登陸密碼: ");
gets(st);
if(strcmp(st,"123")==0)
printf("7");//響鈴
else
printf("Error ");
return(0);
}

㈤ c語言程序設計 密碼設置程序怎麼編寫

- -|
這么多問題才10分...
給你一個密文輸出的程序好了..
其他自己想.
#include <stdio.h>
main()
{
char str[9]; //密碼串長9為
inputPW(str,8); //有效密碼長為8 最後一位要放\0 結束符的!
printf("\n密碼為: %s",str);
}

inputPW(char * s,int len) //自己寫的密碼輸入的函數
{
int i;
fflush(stdin); //清輸入流 跟flushall()類似
for(i=0; ;i++)
{
s[i]=getch();
if(s[i]==13) //輸入結束 不能用=='\n'來判斷!!
//因為對於回車來說getchar()='\n'=10;而getch()=13 !='\n' 這個要知道!
break;
if(s[i]==8 && i>0) //如果用戶按退格鍵 並且要有格可退時候
{
printf("\b \b"); //顯示退一格
i=i-2; //輸入數據退2(因為for循環體會加1 所以實際就是退了1)
continue;
}
if(i==len) {i--; continue;}
printf("*");
}
s[i]='\0'; //末尾補\0 所以該密碼實際有效長度為i-1;定義有效長為len 實際定義的串長為len+1
}

㈥ C語言用c寫一個可以驗證賬號,密碼和修改密碼的程序

#include <string.h>
struct e
{
char a[10];
char b[10];
}z;
int main()
{ int t=0;
char s[10],d[10];
FILE *p;
void as();
if ((p=fopen("m.txt","r+"))==NULL)
{
p=fopen("m.txt","w+");
t=1;
}
if(t==1)
{
printf("當前沒有任何用戶\n");
printf("請新建用戶名: ");
scanf("%s",s);
printf("為用戶設置密碼: ");
scanf("%s",d);
strcpy(z.a,s);
strcpy(z.b,d);
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
}
if(t==0)
{
printf("請輸入用戶名: ");
scanf("%s",s);
fscanf(p,"%s %s",z.a,z.b);
fclose(p);
if (!strcmp(z.a,s))
{
printf("請輸入密碼:");
scanf("%s",d);getchar();
if(!strcmp(z.b,d))
{ char i;
printf("是否要修改密碼?(輸入y修改,n退出!)");
scanf("%c",&i);
if(i=='y')
{
printf("請輸入修改密碼:");
scanf("%s",z.b);
p=fopen("m.txt","w+");
fprintf(p,"%s %s",z.a,z.b);
fclose(p);
printf("修改成功!");
}
}
else printf("密碼錯誤!");
}
else printf("用戶名錯誤");
fclose(p);
}
}

㈦ c語言編寫用戶登錄程序

代碼如下:

#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(" ");

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

{

printf("登錄成功 ");

break;

}

else

{

i++;

if (i != 3)

printf("再輸入一次");

}

}

if (i == 3)

printf("密碼錯誤三次退出登錄界面 ");

system("pause");

return 0;

(7)c語言設計登錄密碼程序擴展閱讀:

#include後面有兩種方式,<>;和""前者先在標准庫中查找,查找不到在path中查找。後者為文件路徑,若直接是文件名則在項目根目錄下查找。

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

注意事項:在TC2.0中,允許不引用此頭文件而直接調用其中的函數,但這種做法是不標準的。也不建議這樣做。以避免出現在其他IDE中無法編譯或執行的問題。

㈧ 用C語言設計一個模擬程序,完成計算機登錄時的密碼驗證過程。

#include<stdio.h>
#include<string.h>

intmain(intargc,char*argv[])
{
constchar*password="123456";
charbuff[64]={0};

printf("請輸入需要校驗的密碼: ");
scanf("%s",buff);//如果要考慮空格使用fgets

if(0==strcmp(password,buff))
{
printf("輸入正確 ");
}
else
{
printf("輸入錯誤 ");
}

return0;
}

㈨ 如何用C語言編寫密碼程序

1、用一個字元數組來存密碼
再用一個字元數組接收你的輸入,然後用strcmp
來比較,如果返回0則密碼是正確的
2、常式:

#include"stdio.h"
#include"string.h"
intmain()
{
charmima[100]="YuanShi888";
charinput[100]={0};
printf("請輸入密碼:");
gets(input);
if(strcmp(mima,input)==0)
printf("恭喜你,密碼正確! ");
else
printf("對不起,密碼輸入錯誤! ");

}

㈩ C語言編寫一個用戶登陸的程序

代碼如下:

#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(" ");

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

{

printf("登錄成功 ");

break;

}

else

{

i++;

if (i != 3)

printf("再輸入一次");

}

}

if (i == 3)

printf("密碼錯誤三次退出登錄界面 ");

system("pause");

return 0;

(10)c語言設計登錄密碼程序擴展閱讀:

#include後面有兩種方式,<>;和""前者先在標准庫中查找,查找不到在path中查找。後者為文件路徑,若直接是文件名則在項目根目錄下查找。

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

注意事項:在TC2.0中,允許不引用此頭文件而直接調用其中的函數,但這種做法是不標準的。也不建議這樣做。以避免出現在其他IDE中無法編譯或執行的問題。