1.演示多个字符从两端移动,向中间汇聚。
#include<stdio.h>
#include<windows.h>
int main()
{
char arr1[] = "###################";
char arr2[] = "welcome to bit!!!!!";
int start = 0;
int end = strlen(arr2) - 1;
printf("%s\n", arr1);
for (start = 0, end = strlen(arr1); start <= end; start++, end--)
{
Sleep(1000);
arr1[start] = arr2[start];
arr1[end] = arr2[end];
printf("%s\n", arr1);
}
system("pause");
return 0;
}
2.模拟用户登录情景,并且只能登录三次。只允许登录三次,如果密码正确则提示登录成功,如果三次均输入错误,则退出程序。
#include<stdio.h>
#include<windows.h>
#include<stdlib.h>
int main()
{
char *p = "123456";
char password[10];
int i = 0;
for (i = 0; i < 3; i++)
{
printf("请输入密码(最多九位):");
scanf("%s", password);
if (strcmp(p, password)==0)
{
printf("登录成功\n");
break;
}
else
{
printf("密码错误\n");
}
}
if (i < 3)
{
printf("取钱成功\n");
}
if (i == 3)
{
printf("登录失败,5秒后退出系统\n");
Sleep(5000);
exit(0);
}
system("pause");
return 0;
}
3.猜数字小游戏
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void menu()
{
printf("##############################\n");
printf("######欢迎使用猜数字游戏######\n");
printf("##############################\n");
printf("######1.开始#####2.结束#######\n");
printf("##############################\n");
}
int main()
{
int num = 0;
int input = 1;
while (input)
{
menu();
printf("请选择:");
scanf("%d", &input);
if (input == 1)
{
printf("游戏开始了!\n");
srand((unsigned)time(NULL));
int ret = rand() % 100 + 1;
while (1)
{
printf("请猜一个数:");
scanf("%d", &num);
if (num == ret)
{
printf("恭喜你,猜对了!\n");
break;
}
else
{
if (num > ret)
{
printf("你猜的数大了\n");
}
if (num < ret)
{
printf("你猜的数小了\n");
}
}
}
}
if (input == 2)
{
exit(1);
break;
}
}
system("pause");
return 0;