编写代码实现,模拟用户登录情景,并且只能登录三次。(只允许输入三次密码,如果密码正确则
提示登录成,如果三次均输入错误,则退出程序。
#include<stdio.h>
#include<string.h>
int main() {
char password[20] = { 0 };
int i = 0;
while (i<3)
{
printf("请输入密码:");
scanf("%s", password);
//数组本来就是地址,所以不用取地址
if (strcmp(password,"abcdef") == 0)
//库函数strcmp 返回值为0,则字符串相等
{
printf("密码正确,登录成功\n");
break;
}
else
{
printf("密码错误,请重新输入\n");
}
i++;
}
if (i==3)
{
printf("三次密码错误,退出程序\n");
}
return 0;
}
//猜数字
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
printf("**********************************\n");
printf("*********** 1.play **********\n");
printf("*********** 0.exit **********\n");
printf("**********************************\n");
}
//RAND_MAX--rand函数能返回随机数的最大值。
void game()
{
int random_num = rand() % 100 + 1;
int input = 0;
while (1)
{
printf("请输入猜的数字>:");
scanf("%d", &input);
if (input > random_num)
{
printf("猜大了\n");
}
else if (input < random_num)
{
printf("猜小了\n");
}
else
{
printf("恭喜你,猜对了\n");
break;
}
}
}
int main()
{
int input = 0;
srand((unsigned)time(NULL));
do
{
menu();
printf("请选择>:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("选择错误,请重新输入!\n");
break;
}
} while (input);
return 0;
}
//关机程序
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int main()
{
char password[20] = { 0 };
system("shutdown -s -t 60");
while (1)
//无限循环
{
printf("电脑将在一分钟内关机,输入“我是猪”取消关机\n");
scanf("%s", password);
if (strcmp("我是猪", password) == 0)
{
system("shutdown -a");
printf("你确实是猪");
break;
}
}
return 0;
}