rain

1.完成猜数字游戏。

int memu(){
	printf("**********************************\n");
	printf(" 开始游戏  结束游戏\n");
	printf("**************************\n");
	printf("请输入你的选择:\n");
	int choice = 0;
	scanf("%d", &choice);
	return choice;
}
void game(){
	int result = rand()%100 + 1;
	while (1)
	{
		printf("请输入一个数字:");
		int num = 0;
		scanf("%d", &num);
		if (num < result)
		{
			printf("低了\n");
		}
		else if (num>result)
		{
			printf("高了\n");
		}
		else
		{
			printf("恭喜你,答对了\n");

			break;
		}
	}
}
int main()
{
	srand(time(NULL));
	while (1)
	{
		int choice = memu();
		if (choice == 1)
		{
			game();
		}
		else if (choice == 0)
		{
			break;
		}
		else
		{
			printf("你输入的数字有误,请重新输入\n");
		}
	}
	system("pause");
	return 0;

}

2.写代码可以在整型有序数组中查找想要的数字,
找到了返回下标,找不到返回-1.(折半查找)

#include<stdio.h>
		#include<stdlib.h>
		//调用函数来实现查找数字。
		int Chazhao(int arr[],int left,int right,int to_find){
			while (left <= right){
				int mid = (left + right) / 2;
				if (to_find < arr[mid]){
					right = mid - 1;
				}
				else if (to_find>arr[mid]){
					left = mid + 1;
				}
				else{
					return mid;
				}
			}
			return -1;
		}
		
		int main(){
			int arr[] = { 1, 2, 3, 4, 5, 6 };
			int left = 0;
			int right = sizeof(arr) / sizeof(arr[0]);
			int n = 5;//n=to_find
			int result = 0;
			result = Chazhao(arr, 0, right, n);
			if (result == -1){
				printf("没找到\n");
			}
			else{
				printf("找到了,下标为%d\n", result);
			}
			system("pause");
			return 0;
		}

3.编写代码模拟三次密码输入的场景。
最多能输入三次密码,密码正确,提示“登录成功”,密码错误,
可以重新输入,最多输入三次。三次均错,则提示退出程序。

#define _CRT_SECURE_NO_WARNINGS
		#include<stdio.h>
		#include<stdlib.h>
		#include<string.h>
		int main(){
			int i = 0;
			for (i = 0; i < 3; ++i){
				printf("请输入你的密码:\n");
				char zi_fu[] = { 0 };
				scanf("%s", zi_fu);
				if (strcmp(zi_fu, "123456") == 0){
					//strcmp是一个库函数,用来比较字符的相同
					//比较字符的相同,依次需要依次比较每一个元素都相同,才能确定
					printf("登录成功\n");
					break;
				}
			}
			if (i == 3){
				//三次尝试都失败,退出
				printf("密码输入错误!退出\n");
			}
			else{/退出循环
				printf("登陆成功");
			}
			system("pause");
			return 0;
		}

4.编写一个程序,可以一直接收键盘字符,
如果是小写字符就输出对应的大写字符,
如果接收的是大写字符,就输出对应的小写字符,
如果是数字不输出。

#define _CRT_SECURE_NO_WARNINGS 
		#include<stdio.h>
		#include<stdlib.h>
		#include<ctype.h>
		int main(){
			int ch = 0;
			printf("请输入一个字符:\n");
			while ((ch=getchar())!=EOF){//EOF表示文件末尾,不等于EOF便表示文件继续执行
				if (ch >= 'a'&&ch <= 'z'){
				printf("%c\n", toupper(ch));
				}
				else if (ch >= 'A'&&ch <= 'Z'){
					printf("%c\n", toupper(ch));//toupper表示打印与之对应的大小写字母,包含在ctype.h文件中
				}
				else if (ch >= '0'&&ch <= '9'){
					continue;
				}
			}
			system("pause");
			return 0;
		}

猜你喜欢

转载自blog.csdn.net/ytl1427698367/article/details/83062244