#define_CRT_SECURE_NO_WARNINGS//goto语句示例////程序运行起来倒计时60秒关机////cmd-命令行窗口////shutdown -s -t 60//倒计时60秒关机////shutdown -a//取消关机////system是一个库函数这个函数是用来执行系统命令//#includestdlib.h//#includestdio.h//#includestring.h//int main()//{// char input[20] { 0 };// system(shutdowm -s -t 60);//again:// printf(你的电脑将在1分钟后关机如果输入我是猪就取消关机\n);// scanf(%s, input);// if (strcmp(input ,我是猪) 0)////字符串的比较不能使用 ////应使用strcmp函数////如果strl 大于 str2返回 0 数字////如果strl 等于 str2返回 0////如果strl 小于 str2返回 0 数字// {// system(shudown -a);// printf(还算配合取消关机\n);// }// else// {// goto again;// }// return 0;//}//goto语句平替//#includestdlib.h//#includestdio.h//#includestring.h//int main()//{// char input[20] { 0 };// system(shutdowm -s -t 60);// while (1)// {// printf(你的电脑将在1分钟后关机如果输入我是猪就取消关机\n);// scanf(%s, input);// if (strcmp(input, 我是猪) 0)// //字符串的比较不能使用 // //应使用strcmp函数// //如果strl 大于 str2返回 0 数字// //如果strl 等于 str2返回 0// //如果strl 小于 str2返回 0 数字// {// system(shudown -a);// printf(还算配合取消关机\n);// break;// }// }//// return 0;//}//写一个猜数字游戏//游戏要求//1.电脑自动生成1-100的随机数//2.玩家猜数字猜数字的过程中根据玩家猜测数据的大小// 给出大了或小了的反馈直到猜对游戏结束//随机数⽣成// rand//C语⾔提供了⼀个函数叫 rand这函数是可以⽣成随机数的函数原型如下所⽰// int rand(void);//rand函数会返回⼀个伪随机数这个随机数的范围是在0~RAND_MAX之间这个RAND_MAX的⼤⼩是//依赖编译器上实现的但是⼤部分编译器上是32767。//rand函数的使⽤需要包含⼀个头⽂件是stdlib.h//#include stdio.h// #include stdlib.h// int main()//{// printf(%d\n, rand());// printf(%d\n, rand());// printf(%d\n, rand());// printf(%d\n, rand());// printf(%d\n, rand());// return 0;// }//⼀次运⾏中产⽣的5个数字是相对随机的但是下⼀次运⾏程序⽣成的结果和上⼀次//⼀模⼀样这就说明有点问题。//其实 rand 函数⽣成的随机数是伪随机的伪随机数不是真正的随机数是通过某种算法⽣成的随机数。真正的随机数的是⽆法预测下⼀个值是多少的。⽽ rand 函//数是对⼀个叫“种⼦”的基准值进⾏运算⽣成的随机数。//srand//C语⾔中⼜提供了⼀个函数叫 srand⽤来初始化随机数的⽣成器的srand的原型如下// void srand(unsigned int seed);//程序中在调⽤ rand 函数之前先调⽤ srand 函数通过 srand 函数的参数seed来设置rand函数⽣成随//机数的时候的种⼦只要种⼦在变化每次⽣成的随机数序列也就变化起来了。//time//在程序中我们⼀般是使⽤程序运⾏的时间作为种⼦的因为时间时刻在发⽣变化的。//在C语⾔中有⼀个函数叫 time 就可以获得这个时间time函数原型如下// time_t time(time_t * timer);//time 函数会返回当前的⽇历时间其实返回的是1970年1⽉1⽇0时0分0秒到现在程序运⾏时间之间的//差值单位是秒。返回的类型是time_t类型的time_t 类型本质上其实就是32位或者64位的整型类//型。//time函数的参数 timer 如果是⾮NULL的指针的话函数也会将这个返回的差值放在timer指向的内存//中带回去。//如果 timer 是 NULL就只返回这个时间的差值。time函数返回的这个时间差也被叫做时间戳。//time函数的时候需要包含头⽂件time.h////VS2022 上time_t类型的说明//// #ifndef _CRT_NO_TIME_T// #ifdef _USE_32BIT_TIME_T// typedef __time32_t time_t;// #else// typedef __time64_t time_t;// #endif// #endif////// typedef long __time32_t;// typedef __int64 __time64_t;// 如果只是让time函数返回时间戳我们就可以这样写//time(NULL);//调⽤time函数返回时间戳这⾥没有接收返回值//#include stdio.h// #include stdlib.h// #include time.h//// int main()// {// //使⽤time函数的返回值设置种⼦// //因为srand的参数是unsigned int类型我们将time函数的返回值强制类型转换// 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;//%100的余数是0~99,0~99的数字1,范围是1~100// 如果要⽣成100~200的随机数⽅法如下// 100 rand() % (200 - 100 1)// //余数的范围是0~100加100后就是100~200// 所以如果要⽣成a~b的随机数⽅法如下// a rand() % (b - a 1)//猜数字游戏实现//#include stdio.h// #include stdlib.h// #include time.h////// void game()// {// int r rand() % 100 1;// int guess 0;// while (1)// {// printf(请猜数字:);// scanf(%d, guess);// if (guess r)// {// printf(猜⼩了\n);// }// else if (guess r)// {// printf(猜⼤了\n);// }// else// {// printf(恭喜你猜对了\n);// break;// }// }// }//// void menu()// {// printf(***********************\n);// printf(****** 1. play ******\n);// printf(****** 0. exit ******\n);// printf(***********************\n);// }//// 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;// }//还可以加上猜数字的次数限制如果5次猜不出来就算失败.//#include stdio.h//#include stdlib.h// #include time.h//// void game()// {// int r rand() % 100 1;// int guess 0;// int count 5;// while (count)// {// printf(\n你还有%d次机会\n, count);// printf(请猜数字:);// scanf(%d, guess);// if (guess r)// {// printf(猜⼩了\n);// }// else if (guess r)// {// printf(猜⼤了\n);// }// else// {// printf(恭喜你猜对了\n);// break;// }// count--;// }// if (count 0)// {// printf(你失败了正确值是:%d\n, r);// }// }//// void menu()// {// printf(***********************\n);// printf(****** 1. play ******\n);// printf(****** 0. exit ******\n);// printf(***********************\n);// }//// 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;// }