c回顾之day5

1、完成猜数字游戏。
2、写代码可以在整型有序数组中查找想要的数字,找到了返回下标,找不到返回-1.(折半查找)
3、编写一个程序,可以一直接收键盘字符,如果是小写字符就输出对应的大写字符,如果接收的是大写字符,就输出对应的小写字符,如果是数字不输出。 
              
#include<stdio.h>
#include<time.h>
#include<windows.h>

void guessGame()
{
	int gnum;
	int data = rand()%100+1;
	int num;
	while(1){
		printf("0:play\n1:exit\n");
		scanf("%d",&num);
		while(!num){
			printf("请输入你所猜的数字:");
			scanf("%d",&gnum);
			if(gnum > data){
				printf("大了!\n");
			}
			else if(gnum < data){
				printf("小了!\n");
			}
			else{
				printf("猜中了!\n");
				break;
			}
		}
		if(1 == num){
			printf("Game Over!\n");
			break;
		}
	}
}

int binSearch()
{
	int arr[] = {1,4,6,7,8,12,18,34,56,57,67};
	int left = 0;
	int right = sizeof(arr)/sizeof(arr[0]) - 1;
	int mid = 0;
	int find;
	scanf("%d",&find);
	while(left <= right)
	{
		mid = (left + right)/2;
		if(arr[mid] < find){
			left = mid + 1;
		}else if(arr[mid] > find){
			right = mid - 1;
		}
		else{
			return mid;
		}
	}
	if(left > right){
		return -1;
	}
}


void convertChar()
{
	char ch;
	while(1){
		while((ch = getchar()) != EOF){//'\n'也属于一个字符,会被getchar获取
			if(ch >= 65 && ch <= 90){
				putchar(ch+32);
			}
			else if(ch >= 97 && ch <=122){
				putchar(ch-32);
			}
		}
	}
}

int main()
{
	//srand((unsigned)time(NULL));//随机种子
	//guessGame();
	//printf("%d\n",binSearch());
	convertChar();
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/tec_1535/article/details/79783093