一分钟内关机
system函数
专门执行系统命令的,但要引用头文件stdlib.h
例如执行系统关机命令
system("shutdown -s -t 60");
strcmp函数
两个字符串的比较不能用==,而应该使用strcmp(string compara),同样要引用头文件string.h
strcmp如果判断两个字符串内容相同,则返回0
strcmp(input, "字符串内容");
#############################################################################
打开cmd命令窗口
输入shutdown -s -t 60
注释:-s表示关机,-t表示设置时间内关机单位为秒
回车系统会出现一分钟内关机
此时输入shutdown -a取消关机
代码如下
#include<stdlib.h>
#include<string.h>
int main()
{
char input[20] = { 0 };
system("shutdown -s -t 60");//专门执行系统命令的,但要引用头文件stdlib.h
again:
printf("请注意,你的电脑将在1分钟内关机,请输入:“\n我是猪”,可取消关机\n");
scanf("%s",input);
//判断input中放的是不是“我是猪”
//两个字符串的比较不能用==,而应该使用strcmp(string compara),同样要引用头文件string.h
//strcmp如果判断两个字符串内容相同,则返回0
if (strcmp(input, "我是猪") == 0)
{
system("shutdown -a");
printf("\n行行行,我知道了....\n");
}
else
{
printf("\n小逼崽子,你是真没见过黑客嗷,敢不敢跟我比划比划?\n");
goto again;
}
return 0;
}
把goto语句改为while循环:
int main()
{
char input[20] = { 0 };
system("shutdown -s -t 60");//专门执行系统命令的,但要引用头文件stdlib.h
while (1)
{
printf("\n请注意,你的电脑将在1分钟内关机,请输入:“我是猪”,可取消关机\n");
scanf("%s", input);
//判断input中放的是不是“我是猪”
//两个字符串的比较不能用==,而应该使用strcmp(string compara),同样要引用头文件string.h
//strcmp如果判断两个字符串内容相同,则返回0
if (strcmp(input, "我是猪") == 0)
{
system("shutdown -a");
printf("\n你对自己有很清晰的认知。\n");
break;
}
}
return 0;
}
猜数字游戏
游戏要求:
1.电脑自动生成1~100的随机数
2.玩家猜数字,猜数字的过程中,根据猜测数据的大小给出大了或小了的反馈,直到猜对,游戏结束
rand函数
会生成0~RAND_MAX(32767)之间的伪随机数,对一个叫种子的基准来生成的
需引用头文件#include<stdlib.h>
printf("%d\n",rand());
之所以每次运行程序生成的随机数列是一样的,是因为rand函数生成随机数的默认种子是1,如果要生成不同的数,就要让种子变化。
srand函数
rand(1)
rand(2)
rand(3)
程序在调用rand函数时先调用的是srand函数,srand的参数seed来生成随机种子,只要种子在变化,每次生成的随机数就在变化,srand函数不需要频繁调用,一次程序调用一次就可以了。
time函数
要包含头文件time.h
时间戳
计算机当前时间和计算机的起始时间之间的差值(秒)(1970.1.1)。
以变量时间戳为种子序列传给srand函数,time函数返回的就是时间戳,
srand((unsigned int)time(NULL))
NULL是空指针
time函数的返回值为time_t,但我们需要的是unsigned int类型的,所以我们需要强转一下(unsigned int)time
int main()
{
srand((unsigned int)time(NULL));
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
return 0;
}
设置随机数的范围
如果我们要生成0~99之间的随机数
rand%100;//余数在0~99
如果我们要生成1~100之间的随机数
rand%100+1;//余数为1~100
如果我们要生成100~200之间的随机数
100+rand()%(200-100+1);//余数1~100,加100就是100~200
如果我们要生成a~b之间的随机数
a+rand()%(b-a+1);
//猜数字游戏(有次数)
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
void game()
{
//1.生成随机数
int r = 1 + rand() % 10;
//2.猜数字
int guess = 0;
int count = 3;
while (count > 0)
{
printf("\n——您还有%d次机会——\n", count);
printf("请输入您所猜的数字是:\n");
scanf("%d", &guess);
if (guess > r)
printf("————————猜大了————————\n");
else if (guess < r)
printf("————————猜小了————————\n");
else
{
printf("恭喜你!!猜对了,答案就为%d\n", r);
break;
}
count--;
}
if (count == 0)
{
printf("次数用完了~,正确答案是%d\n", r);
}
}
void menu()
{
printf("_____________________\n");
printf("|*******************|\n");
printf("|****猜数字游戏******|\n");
printf("|-------------------|\n");
printf("|****开始游戏(1)*****|\n");
printf("|****退出游戏(0)*****|\n");
printf("|*******************|\n");
printf("|___________________|\n");
printf(" \n");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do//使用do while循环,不管三七二十一,先打印菜单
{
menu();
printf("*******请选择********\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("******游戏已退出*******\n");
break;
default:
printf("******请正确输入*******\n");
break;
}
} while (input);
return 0;
}