如何用C语言编写程序,实现输入密码有三次机会,正确后显示一个菱形,急!!!!! 5
展开全部
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "windows.h"
void Print(int m,bool flag) //打印星号函数
{
char *s=flag?"*":" ";
for (int i=0;i<m;i++)
printf(s);
}
int num=40;
int main()
{
char password[]="123456";
char inputword[20];
int n,i,j,XHNumber;
int Count=4,index=3;;
while(1)
{
printf("please input password: ");
scanf("%s",inputword);
if(!strcmp(password,inputword))
{
system("cls");
printf("The password is right!\n\n\n");
for(i=0;i<2*5+1;i++) //控制行
{
XHNumber=2*(i+1)-1;
if(i>=(2*5+2)/2) //判断是否到了下半
{
XHNumber=XHNumber-Count;
Count+=4;
num+=1;
}
else num-=1;
Print(num,false); //打印空格
Print(XHNumber,true); //打印星号
printf("\n");
}
break;
}
else
{
index--;
printf("The password is wrong! ");
if(index==2)
{
printf("You have two chance to input password!\n");
}
else if(index==1)
{
printf("You have one chance to input password!\n");
}
else
{
printf(" You have input wrong password 3 times, \nthe system will exit after 3 seconds!\n");
printf("3..\n");
Sleep(1000);
printf("2..\n");
Sleep(1000);
printf("1..\n");
Sleep(1000);
break;
exit(0);
}
}
}
return 0;
}
#include "stdlib.h"
#include "string.h"
#include "windows.h"
void Print(int m,bool flag) //打印星号函数
{
char *s=flag?"*":" ";
for (int i=0;i<m;i++)
printf(s);
}
int num=40;
int main()
{
char password[]="123456";
char inputword[20];
int n,i,j,XHNumber;
int Count=4,index=3;;
while(1)
{
printf("please input password: ");
scanf("%s",inputword);
if(!strcmp(password,inputword))
{
system("cls");
printf("The password is right!\n\n\n");
for(i=0;i<2*5+1;i++) //控制行
{
XHNumber=2*(i+1)-1;
if(i>=(2*5+2)/2) //判断是否到了下半
{
XHNumber=XHNumber-Count;
Count+=4;
num+=1;
}
else num-=1;
Print(num,false); //打印空格
Print(XHNumber,true); //打印星号
printf("\n");
}
break;
}
else
{
index--;
printf("The password is wrong! ");
if(index==2)
{
printf("You have two chance to input password!\n");
}
else if(index==1)
{
printf("You have one chance to input password!\n");
}
else
{
printf(" You have input wrong password 3 times, \nthe system will exit after 3 seconds!\n");
printf("3..\n");
Sleep(1000);
printf("2..\n");
Sleep(1000);
printf("1..\n");
Sleep(1000);
break;
exit(0);
}
}
}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询