几个简单小程序 猜数字

1.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
    void menu()
    {
        printf("**************************\n");
        printf("******  1.begin    *******\n");
        printf("******  0.exit     *******\n");
        printf("**************************\n");
    }
    void game()
    {
        int ret = rand()%100+1;
        int a = 0;
    while(1)
    {
        printf("Guesses the number\n");
        scanf("%d",&a);
        if(a==ret)
        {
            printf("Guess right\n");
            break;
        }
        else if (a>ret)
        {
            printf("Guess Big\n");
            break;
        }
        else 
        {
            printf("Guess Small\n");
    

        }
    }
    }
int main()
    {
        int b = 0 ;
        srand((unsigned int )time(NULL));
        do 
        {
            menu();
            printf("please choose");
            scanf("%d",&b);
        switch(b)
        {
        case 1 :
                 game();
                 break;
        case 2 :
                printf("exit game\n");
                break;
        default:
                printf("error!!!!\n");
                break;


        
        }
        
        }
        while(b);
        system("pause");
        return 0 ;

    }
2.
#include<stdio.h>
#include<stdlib.h>
int search(int arr[],int key,int right,int left)
{    
    while(left<=right)
    {
        int mid = left+(right-left)/2;
        if(arr[mid]>key)
            {
                left=mid-1;

        }
        else if (arr[mid]<key)
        {
            right=mid+1;
        }
        else 
        {
        return mid ;
        }
    }
    return -1;

}
    

    
    int main ()
    {
        int arr[] ={1,2,3,4,5,6,7,8,9,10};
        int key = 3;
        int left = 0 ;
        int right = sizeof(arr)/sizeof(arr[0])-1;
        int ret = search(arr,key,right,left);
        if (ret = -1)
            printf("找不到");
        else
            printf("%d",ret);
        system("pause");
        return 0;

    
    }

        
        
        
3.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
    int main()
    {
        int i = 0 ;
        char arr[20]= {0};
        for(i=0;i<3;i++)
        {
            printf("请输入密码");
            scanf("%s",&arr);
        if(strcmp==0(arr,"abcdef"))
        {
        break ;
        }
        else 
        {
        printf("密码错误,请重新输入");
        }
        }
        if (i<3)
            printf("登录成功\n");
        else
            printf("退出程序\n");
        


        system("pause");
        return 0 ;
    }
4.
#include<stdio.h>
#include<stdlib.h>
    int main()
    {
        int ch ;
        printf("请输入一个字符");
        while((ch=getchar())!=EOF)
        {
        if(ch>='a'&&ch<='z')
            printf("%c\n",ch-32);
        else
            if(ch>='A'&&ch<='Z')
        printf("%c\n",ch+32);
        
         else
             if(ch>='0'&&ch<='9')
            ;
        else
            ;

        
        }

        printf("\n");
        system("pause");
        return 0 ;
    } 

 

posted on 2018-10-11 21:32  singleday  阅读(542)  评论(0)    收藏  举报