博客
关于我
C入门【五】(一)
阅读量:294 次
发布时间:2019-03-01

本文共 1602 字,大约阅读时间需要 5 分钟。

1. 编写一个程序,可以直接收键盘字符

程序功能:接收键盘输入字符,并根据字符类型进行转换处理。如果是小写字符输出对应的大写字符;如果是大写字符输出对应的小写字符;如果是数字则不进行转换输出。

个人看法:直接对输入字符进行比对更加直观且符合常规思维习惯。

int main(){   	char c;	scanf("%c", &c);	if (c >= 'A' && c <= 'Z') {   		c += 32;		printf("%c", c);	}	else if (c >= 'a' && c <= 'z') {   		c -= 32;		printf("%c", c);	} else {   		printf("%c", c);	}	system("pause");	return 0;}

代码解释:程序首先读取一个字符输入,然后通过条件判断语句判断字符类型。对于大写字符(A-Z),通过加32转换为小写字符输出;对于小写字符(a-z),通过减32转换为大写字符输出;对于数字字符则直接输出原样。最终系统暂停一秒并结束程序。

2. 猜数字游戏

游戏规则:程序模拟一个猜数字的游戏机制。随机生成一个目标数字,用户输入数字进行比对,若输入数字大于目标数字提示“大了”,小于则提示“小了”,正好匹配则提示“对了”并结束游戏。游戏可在任何时刻通过输入0退出。

技术要点: 1. 使用`rand()`函数生成随机数,结合`time(NULL)`确保每次运行时生成不同的随机数,避免游戏失去趣味性。 2. 实现简单的数字比对逻辑,通过条件语句判断输入数字与目标数字的关系并输出提示信息。

#define _CRT_SECURE_NO_WARNINGS  #include "stdio.h"  #include "stdlib.h"  #include "time.h"  #define random(x) (rand() % x)  int Menu(){   	printf("******************\n");	printf("1. 进入游戏\n");	printf("0. 退出游戏\n");	printf("******************\n");	return 0;} int Game(){   	int input;	int target = random(100) + 1;	while (1){   		printf("请输入的数字:");		scanf("%d", &input);		if (input > target){   			printf("大了\n");		} else if (input < target){   			printf("小了\n");		} else {   			printf("对了\n");			system("pause");		break;		}	}} int main(){   	int input = 0;	srand((unsigned)time(NULL));	do{   		Menu();		printf("请选择: ");		scanf("%d", &input);		switch (input){   		case 1:			Game();			break;		case 0:			break;		default:			printf("重新输入\n");			break;		}	} while (input);	system("pause");	return 0;}

代码解释:程序首先定义游戏菜单和游戏逻辑。用户通过选择1进入游戏,随机生成一个1到100之间的目标数字。用户输入数字后进行比对,根据结果输出提示并继续游戏。输入0时退出游戏。程序启动时会重新生成随机数以确保每次游戏的随机性。

转载地址:http://ukza.baihongyu.com/

你可能感兴趣的文章
Nmap扫描教程之Nmap基础知识
查看>>
nmap指纹识别要点以及又快又准之方法
查看>>
Nmap渗透测试指南之指纹识别与探测、伺机而动
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>