c语言关机恶作剧

发布于:2023-01-04 ⋅ 阅读:(191) ⋅ 点赞:(0)

《程序员的快乐》

                执着篇 ----- 既然我已经踏上这条道路,那么,任何东西都不应妨碍我沿着这条路走下去。

目录

1、“粮草先行”

Sleep():

system():

strcmp():

rand():

srand():

time():

goto语句:

2、多个字符从两端移动,向中间汇聚。

3、模拟用户登录情景,关机小程序

4、猜数字小游戏


1、“粮草先行”

Sleep():

介绍:Sleep函数可以使计算机程序(进程,任务或线程)进入休眠,使其在一段时间内处于非活动状态。

单位:毫秒         

头文件:#include <windows.h>

system():

介绍: 可以 执行系统命令,调用命令处理器来执行命令。

参数:包含要执行的系统命令的 C 字符串。

 例:"cls"---清屏、“shutdown -s -t 60” --- 定时关机。                        

头文件:#include <stdlib.h>

strcmp():

介绍:比较两个字符串,将 字符串str1与 字符串str2进行比较。

参数:str1,str2

返回值:两个字符串相等返回0。

头文件:#include <string.h>

rand():

介绍:生成随机数,0----32767。

使用技巧:例:int n = rand() % 100 + 1;n的范围就控制在1到100; 

头文件:#include <stdlib.h>

srand():

void srand (unsigned int seed);
介绍:初始化随机数生成器。
返回值类型:unsigned int
使用:例:始化为一些独特的运行值,例如时间。
srand (time(NULL))

time(NULL)强制类型转换:srand((unsigned int)time(NULL));

NULL:此参数可以是空指针,该函数仍然返回一个时间类型的值。

头文件:#include <stdlib.h>

time():

介绍:time_t 时间(time_t* 定时器);获取当前时间。

头文件:#include <time.h>

goto语句:

适用场景:一次跳出两层或多层循环。

语法:

goto 标签;
标签:

2、多个字符从两端移动,向中间汇聚。

效果描述:

首先在屏幕上打印一串字符,多个字符从两端移动,向中间汇聚。

代码实现:使用了Sleep()、system()、strcmp()

#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	//两个数组存储数据
	char arr1[] = "Study Hard!!!!";
	char arr2[] = "##############";
	int left = 0;
	int right = strlen(arr1)-1;

	while (left <= right)
	{
		arr2[left] = arr1[left];
		arr2[right] = arr1[right];
		printf("%s\n", arr2);
		Sleep(1000);
		system("cls");
		left++;
		right--;
	}
	printf("%s\n", arr2);
	return 0;
}

 结果:

3、模拟用户登录情景,关机小程序

效果描述:

模拟用户登陆,三次密码错误会触发自动关机,输入特定的指令取消关机!

代码实现:使用了system()、strcmp()、goto语句

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
	char password[20] = { 0 };
	int i = 0;
	for (i = 0; i < 3; i++)
	{
		printf("请输入密码:");
		scanf("%s", password);
		if (strcmp(password, "zhang") == 0)
		{
			printf("密码输入正确");
			break;
		}
		else
		{
			printf("密码错误!\n");
		}
	}
	if (3 == i)
	{
		
		printf("三次密码错误,用户已注销!");
		system("shutdown -s -t 60");
			again:
			printf("你的设备将在1分钟后关机,输入:我是猪 可以取消!\n");
			scanf("%s",password);
			if(strcmp(password,"我是猪") == 0)
			{
				system("shutdown -a");
				printf("已取消");
			}
			else
			{
				goto again;
			}
	}
	return 0;
}

结果:

 三次密码错误:

 

 

4、猜数字小游戏

游戏描述:

在固定范围内随机生成一个数字,我们输入一个数字进行比较,猜错提示比随机数大或者比随机数小,猜对提示游戏成功!

代码实现:rand()、srand()、time()

#include <stdlib.h>
#include <time.h>
#include <stdio.h>

void menu()
{
	printf("********************************\n");
	printf("*******     1. play      *******\n");
	printf("*******     0. exit      *******\n");
	printf("********************************\n");
}


void game()
{
	
	//1.生成随机数
	int ret = rand() % 100 + 1;
	int num = 0;
	//2.猜数字
	while (1)
	{
		printf("请猜数字:>");
		scanf("%d", &num);
		if (num == ret)
		{
			printf("恭喜你,猜对了\n");
			break;
		}
		else if (num > ret)
		{
			printf("猜大了\n");
		}
		else
		{
			printf("猜小了\n");
		}
	}
}


int main()
{
	int input = 0;
	//讲解srand函数
	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;
}

游戏测试:

 注意srand()的使用!

 小结:很多函数用到我们的程序当中非常便捷,不断更新自己的“武器库”会事半功倍哦!继续加油!!!


网站公告

今日签到

点亮在社区的每一天
去签到