如何用C语言编写密码程序

如何用C语言编写密码程序,第1张

1、用一个字符数组来存密码

再用一个字符数组接收你的输入,然后用strcmp

来比较,如果返回0则密码是正确的

2、例程:

#include "stdio.h"

#include "string.h"

int main()

{

       char mima[100]="YuanShi888"

       char input[100]={0}

       printf("请输入密码:")

        gets(input)

   含仿岩    if(strcmp(mima,input)==0)

        大灶    printf("恭喜你,密码正确!\n")

       else

            printf("对不起,密码输入错误谈御!\n")

  

}

1、枣薯数据范围小的密码可以使用穷举法。这里从00到99,当作字符来处理,需要两重循环。

2、例程:

char a,b,c='1',d='2' //这里变量c,d表森岩岁示密码的第一位和第二位。

char key[2]

for(a='0'a<='9'a++){

        for(b='0'b<='9'b++){

                key[0]=a

                key[1]=b        //这就是一个密码

            此睁    if(key[0]==c && key[1]==d) printf("密码是:%s",key)//找到密码并输出

        }

}

首先得说明我这个可是自己原创手打的,但是没去仿真了,程序可能有错误掘裤,你自己修改下吧

#include<reg52.h>

typedef unsigned char uchar

typedef unsigned int uint

sbit key1=P0^0

sbit key2=P0^1

sbit key3=P0^2

sbit key4=P0^3

sbit wela=P2^0//位锁判首简芹明存端

#define SMG P1

sbit LED=P3^0//低电平亮

uchar code table[]={0x8d,0x86}//共阳数码管 P,E

uchar chushi_mima[]={2,1,3}

uchar shuru_mima[3]

uchar index//控制输入密码的位数

uchar flag_3s=0//3s标志位

uchar keydown//确定按键变量

#define times 15//去抖时间15Ms

uchar key1_count,key2_count,key3_count,key4_count

void init()

{

wela=0

SMG=0xff

TMOD=0x01

TH0=(65536-1000)/256

TL0=(65536-1000)%256

ET0=1

EA=1

TR0=1

LED=1;

}

void main()

{

init()

while(1)

{

switch(keydown)

{

if(index>2)index=0

case 1:

shuru_mima[index]=0

index++

break

case 2:

shuru_mima[index]=1

index++

break

case 3:

shuru_mima[index]=2

index++

break

case 4:

shuru_mima[index]=3

index++

break

}

flag_3s=0

for(i=0i<3i++)

{

if(shuru_mima[i]==chushi_mima[i])

{

LED=0

wela=1

SMG=table[0]

if(flag_3s)

{

flag_3s=0

wela=0

}

}

else

{

LED=1

wela=1

SMG=table[1]

if(flag_3s)

{

flag_3s=0

wela=0

}

}

}

}

}

void timer0() interrupt 1

{

uchar count

TH0=(65536-1000)/256

TL0=(65536-1000)%256

if(++count>=600)

{

count=0

flag_3s=1

}

/*********1ms中断扫描按键(包含去抖程序)********/

if(!key1&&key1_count!=0)

{

key1_count--

if(key1_count==0)

{

keydown=1

}

}

else if(!key1) key1_count=times

// key2,key3,key4你自己写吧

}


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

原文地址: https://outofmemory.cn/yw/12245056.html

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

发表评论

登录后才能评论

评论列表(0条)

保存