1.猜数字游戏;2.二分法查找;3。简易密码验证程序;4.字符串中大写小写字母互换

//1.猜数字游戏
#define _CRT_SECURE_NO_WARNINGS 1
#include 
#include 
#include 
int replay(input, num);
void menu()
{
	printf("***********************\n");
	printf("** Open>>1   Over>>0 **\n");
	printf("***********************\n");
}
int playgame(int tmp)
{
	int a = 0;
	while (1)
	{
		printf("Input:>>");
		scanf("%d", &a);
		if (a == tmp)
		{
			printf("********猜对啦!********\n"); 
			break;
		}
		else if (a > tmp)
		{
			printf("********猜大啦!********\n");
		}
		else if (a < tmp)
		{
			printf("********猜小啦!********\n");
		}
	}
	return 0;
}
int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		int num = rand() % 100 + 1;
		    menu();
			printf("Input 1/0:>>");
			scanf("%d", &input);
			switch (input)
			{
			case 1:
				printf("游戏开始啦~\n");
				playgame(num);
				break;
			case 0:printf("成功退出!\n"); 
				break;
			default:printf("输入错误\n");
				break;
			}
	} while (input);
		system("pause");
		return 0;
}


//2.二分法查找(有序数组)
#define _CRT_SECURE_NO_WARNINGS 1
#include 
int Binary_Search(int a[], int mid, int size)
{
	int low = 0;
	int high = 0;
	int flag = 0;
	scanf("%d", &flag);
	low = 0;
	high = size;
	while (low <= high)
	{
		mid = low + (high - low) / 2;
		if (a[mid] == flag)
		{
			printf("找到了,下标为%d\n", mid);
			break;
		}
		if (a[mid] > flag)
		{
			high = mid - 1;
		}
		else
		{
			low = mid + 1;
		}
	}
	if (high < low)
	{	 
		printf("没找到\n");
		return -1;
	}
	else
		return mid;
}
int main()
{
	int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int mid = 0;
	int  size = 0;
	size = sizeof(a) / sizeof(a[0]) - 1;
	Binary_Search(a, mid, size); 
	system("pause");
	return 0;
}

//3.简易密码验证,三次机会输入,成功便进入,失败三次退出
#define _CRT_SECURE_NO_WARNINGS 1
#include 
int main()
{
	char a[] = "123456";
	char pwd[10] = { 0 };
	int count = 0;
	printf("请输入密码:>>");
	while (count < 3)
	{
		scanf("%s", pwd);
		if (strcmp(pwd, a) == 0)
		{
			printf("登陆成功!\n");
			system("pause");
			return 0;
		}
		else
		{
			if(count<2) printf("密码输入错误,请重新输入:>>");
			count++;
		}
	}
	printf("登陆失败(已输入三次错误密码)!");
	system("pause");
	return 0;
}

//4.输入字符串,将大写字母改变为小写,小写字母改变为大写,其余字符不变
#define _CRT_SECURE_NO_WARNINGS 1
#include 
int main()
{
	char ch[100] = { 0 };
	int i = 0;
	int len = 0;
	while (1)
	{
		len = sizeof(ch) / sizeof(ch[0]);
		scanf("%s", ch);
		for (i = 0; i < len; i++)
		{
			if (ch[i] >= 65 && ch[i] <= 91)
			{
				ch[i] = ch[i] + 32;
			}
			if (ch[i] >= 97 && ch[i] <= 122)
			{
				ch[i] = ch[i] - 32;
			}
		}
			printf("%s\n", ch);
	}
	system("pause");
	return 0;
}


你可能感兴趣的:(1.猜数字游戏;2.二分法查找;3。简易密码验证程序;4.字符串中大写小写字母互换)