程序代码:
#include<stdio.h>
#include<stdlib.h>
//完成猜数字游戏
void chaishuzi(){
srand((unsigned int)time(NULL));
int value = 0;
int key;
int sta = 0;
key = rand()%100; //使用rand之前,一定要声明srand;
while(value != 1000){
printf("please input value!\n >");
scanf("%d",&value);
if(key == value){
printf("恭喜,答对了!\n");
printf("\n****** continue 1 exit 0 *******\n");//答对之后判断是否继续,如果继\
续。重新给出猜测值
scanf("%d",&value);
while(1){
if(value == 1){
key = rand()%100;
break;
}
else if(value == 0){
return;
}else{
printf("input error !\n");
continue;
}
}
}else if(key <value){
printf("大了!\n");
}else{
printf("小了!\n");
}
}
}
//写代码可以在整型有序数组中查找想要到数字,找到了返回下标,找不到返回-1
//(折半查找)
int baniry_sort(int* arr,int size,int key){int left = 0;
int right = size - 1;
while(left <= right){
int mid =left + (right - left)/2;
if(arr[mid] == key){
return mid;
}else if(arr[mid] <key){
left = mid;
}else{
right = mid;
}
}
}
//编写代码模拟输入三次密码,密码正确,提示“登录成功”,密码错误,可以
//重新输入,最后输入三次。三次均错,则提示推出程序。
void passow(){
int value;
int key = 123;
int i = 3;
while(i-->0){
printf("\nplease input passow!\n>>");
scanf("%d",&value);
if(key == value){
printf("success!");
return;
}else{
printf("failuer,please input again!");
}
}
printf("input over 3, will exit!");
return;
}
//编写一个程序,可以直接接受键盘字符,如果是小写字符输出对应的大写字符
//如果接受的是大写字符,就输出对应到小写字符,如果是数字不输出
void myisupper(){
while(1){
printf("input char\n>>");
char value;
scanf("%c",&value);
if(value<'z'&&value>'a'){
printf("%c",value-32);
printf("\n");
}else if(value<'Z'&&value>'A'){
printf("%c",value+32);
printf("\n");
}
}
}
int main(){
printf("\n**********passsow***********************\n");
passow();
printf("\n");
printf("\n**********chaishuzi***********************\n");
chaishuzi();
printf("\n**********baniry_sort***********************\n");
int arr[] = {1,3,5,8,9};
int key = 5;
int ret = baniry_sort(arr,5, 5);
printf("expect : 2 actual: %d\n",ret);
printf("\n**********islower_isupper***********************\n");
myisupper();
return 0;
}
检测结果:
**********passsow***********************
please input passow!
>>123
success!
**********chaishuzi***********************
please input value!
>50
大了!
please input value!
>20
小了!
please input value!
>30
大了!
please input value!
>26
恭喜,答对了!
****** continue 1 exit 0 *******
0
**********baniry_sort***********************
expect : 2 actual: 2
**********islower_isupper***********************
input char
>>input char
>>D
d
input char
>>input char
>>c
C
input char
>>input char
>>