• 13.练习题(年月日,打字游戏)


    练习

    1.任意给出一个年、月、日,判断是这一年的第几天:闰年算法:能被4整除且不能被100整除,或者能被400整除。
    如:2012 5 10 是这一年的第131天。
    提示:使用数组的方式计算,将每月的天数放在一个数组中。

    #include
    int main()
    {
    	int days[] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
    	int year, month, day;
    	int i,sum=0;
    	printf("请输入日期:\n");
    	scanf_s("%d %d %d", &year, &month, &day);
    	for (i = 0; i < month-1; i++)
    	{
    		sum = sum + days[i];
    	}
    	if (month > 2 && (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
    	{
    		sum = sum + day + 1;
    
    	}
    	else
    	{
    		sum = sum + day;
    	}
    	printf("%d %d %d是一年中的第%d天!\n", year, month, day, sum);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    在这里插入图片描述

    2.打字游戏
    1)随机函数
    A. srand( (unsigned) time (NULL));以当前时间为准,设置随机种子
    注意:此函数在每次开始游戏后调用一次即可
    B. ch = rand();
    注意:rand()函数,每调用一次,产生一个随机数
    2)获得键值函数
    ch = getch(); //无需按下回车,可直接获得键盘上按下的键值
    3)时间函数
    start_time=time(NULL);
    end_time=time(NULL);
    4)清空屏幕
    system(“cls”);

    #include
    #include
    #include
    #include
    #include
    void help(void)
    {
    	printf("\n *******************************************");
    	printf("\n *输入过程中无法退出!                     *");
    	printf("\n *请按所给字母敲击键盘!                   *");
    	printf("\n *请按任意键开始测试,按下首字母时开始计时!*");
    	printf("\n *输入出错则以 _ 表示                      *");
    	printf("\n *按空格键继续游戏                         *");
    	printf("\n *按ESC退出游戏                            *");
    	printf("\n *******************************************\n\n");
    }
    int main()
    {
    	char ch;
    	char str[51] = "";
    	int i;
    	int count;
    	time_t start_time, end_time;
    	while (1)
    	{
    		system("cls");
    		help();
    		ch = _getch();//阻塞程序,由键盘获得字符#include
    		srand((unsigned)time(NULL));//产生种子#include
    		for (i = 0; i < 50; i++)
    		{
    			str[i] = rand() % 26 + 'a';
    		}
    		str[50] = '\0';
    		printf("%s\n", str);
    		count = 0;
    		for (i = 0; i < 50; i++)
    		{
    			ch = _getch();
    			if (i == 0)
    			{
    				start_time = time(NULL);
    			}
    			if (ch == str[i])
    			{
    				count++;
    				printf("%c", ch);
    				continue;
    			}
    			else
    			{
    				printf("_");
    				continue;
    			}	
    		}
    		end_time = time(NULL);
    		printf("\n正确率为 %d %c\n", count * 100 / 50,'%');
    		printf("用时 %lld 秒\n", (long int)end_time - start_time);
    		while (1)
    		{
    			ch = _getch();
    			if (ch == ' ')
    			{
    				break;
    			}
    			else if(ch == 27)
    			{
    				return 0;
    			}
    		}
    	}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    在这里插入图片描述

  • 相关阅读:
    Python模拟UDP客户端和服务器收发信息?(菜鸟简陋版)
    26.39万起的2024款小鹏G9能大卖吗?
    【计算机视觉】2.图像特征提取
    【校招VIP】[前端][一本][6分]项目需要考虑到PC端和移动端
    图像拼接后丢失数据,转tiff报错rasterfile failed: an unknown
    Dynamic DataSource 多数据源配置【 Springboot + DataSource + MyBatis Plus + Druid】
    【无标题】
    js制作九宫格抽奖功能
    dart 学习 之 异常
    线程池底层原理详解与源码分析(补充部分---ScheduledThreadPoolExecutor类分析)
  • 原文地址:https://blog.csdn.net/GUDGET/article/details/127966006