一 ,关机程序
实现效果概述:在运行程序时,系统弹出还有60秒关机,如果输入我是猪,则取消关机,否则将继续提示关机,直到60s后,电脑关机;
1.1 goto语句
goto 语句可以实现在同一个函数内跳转标号处;
goto name ; //定义
name: //跳转点
1.2 system 函数
system 函数可以执行系统命令:(windows支持)
关机: shutdown -s -t 60
// -s 设置关机 -t 倒计时关机 60 秒数
取消关机: shutdown -a
注:需要包含头文件<stdlib.h>
1.3 strcmp 函数
两个字符串的比较不能使用 == ,而应该使用 strcmp 函数,需要包含头文件 <string.h>
(strcmp ( const char * str1, const char * str2 );
1.4 代码实现
思路:
- 关机提示
- 输入信息
- 判断输入信息的正确性 ==>正确---取消关机 错误==>继续提示
//关机程序 #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char input[20] = { 0 }; //关机 system("shutdown -s -t 60"); again: printf("请注意!你的电脑将在60秒后关机,如果输入:我是猪,则取消关机!\n"); //输入 scanf("%s", input); //判断输入信息的正确性 if (strcmp(input, "我是猪") == 0) { //取消关机 system("shutdown -a"); } else { goto again; } return 0; }
二 ,猜数字游戏
实现效果概述:电脑自动生成1-100的随机数,由玩家来猜数字,根据猜测数据的大小给出反馈--猜大了,猜小了。直到猜对!
2.1 rand
C语言提供了一个函数叫 rand,可以生成随机数,函数原型如下:
int rand (void);
但是 rand 函数 生成的随机数是伪随机数(不是真正的随机数,是通过某种算法生成的随机数,通过一个叫“种子”的基准值进行运算生成的随机数),这个随机数的范围是在0~RAND_MAX之间,这个RAND_MAX的大小是依赖编译器上实现的,但是大部分编译器上是32767。
注:需要包含头文件<stdlib.h>
2.2 srand
C语言中又提供了一个函数叫 srand ,用来初始化随机数的生成器,srand 原型如下:
void srand ( unsigned int seed);
2.3 time
在程序中我们一般是使⽤程序运行的时间作为种子的,因为时间时刻在发生变化的。
time_t time ( time_t * timer);
如果 timer 是NULL,就只返回这个时间的差值。time函数返回的这个时间差也被叫做:时间戳。 我们可以这么写:
time (NULL);
注:
1.time函数的时候需要包含头文件:time.h
2.srand 函数是不需要频繁调用的,一次运行的程序中调用一次就够了。
2.3 设置随机数的范围
1.如果我们要生成0-99之间的随机数,方法如下:
rand ( ) %100; //余数的范围是0-99
2. 1-100 之间的随机数
rand ( ) %100 + 1;
3. 100-200之间的随机数
100 + rand ( ) %(200 -100 + 1);
4. a-b之间的随机数
a + rand ( ) % (b-a+1)
2.4 代码实现:
//猜数字游戏
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
printf("***********************************\n");
printf("************ 1.play ********\n");
printf("************ 2.exit ********\n");
printf("***********************************\n");
}
void game()
{
//1.生成随机数1 - 100
int r = rand() % 100 +1 ;
//2.猜数字
int guess = 0;
while (1)
{
printf("请猜数字:>");
scanf("%d", &guess);
if (guess > r)
{
printf("猜大了\n");
}
else if (guess < r)
{
printf("猜小了\n");
}
else {
printf("恭喜你!猜对了!随机数是%d\n", r);
break;
}
}
}
int main()
{
srand((unsigned int )time(NULL));
int input = 0;
//选择是否玩
do {
//打印简易菜单
menu();
printf("请选择:>");
//是否玩
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏!\n");
break;
default:
printf("输入错误!请重新输入!\n");
break;
}
} while (input);
return 0;
}
限制次数的玩法:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
void menu()
{
printf("******************************\n");
printf("*********** 1.play ***********\n");
printf("*********** 2.exit ***********\n");
printf("******************************\n");
}//void空类型,没有参数,没有返回值;
void game()
{
//1.生成随机数;
int r = rand() % 100 + 1;
//2.猜数字;
int guess = 0;
int count = 5;
while (1)
{
printf("你还有%d次机会\n", count);
printf("请猜数字:>");
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);
break;
}
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//程序运行起来。只用调用一次就好了;
do {
//打印简易菜单
menu();
printf("请选择:>");
scanf("%d", &input);
switch (input)
{
case 1 :
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}