zoukankan      html  css  js  c++  java
  • C基础--猜数字游戏(图形界面由MFC完成)

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <string.h>
    #include <ctype.h>
    
    
    void generate(char computer[])
    {
        int i = 0, j, t;
    
        srand(time(NULL));
        while (i < 4) {
            t = rand() % 10 + '0';
            for (j = 0; j < i; ++j)
                if (t == computer[j])
                    break;
            if (j == i) {
                computer[i] = t;
                ++i;
            }
        }
    }
    
    int is_valid(char man[])
    {
        int i, j;
    
        if (!(isdigit(man[0])
                    && isdigit(man[1])
                    && isdigit(man[2])
                    && isdigit(man[3])
                    && man[4] == ''))
            return 0;
        for (i = 0; i < 3; ++i)
            for (j = i + 1; j < 4; ++j)
                if (man[i] == man[j])
                    return 0;
        return 1;
    }
    
    int compare(char man[], char computer[])
    {
        int i, j, a = 0, b = 0;
        for (i = 0; i < 4; ++i)
            for (j = 0; j < 4; ++j)
                if (computer[i] == man[j] && i == j)
                    ++a;
                else if (computer[i] == man[j])
                    ++b;
        if (a == 4) {
            printf("YOU BET
    ");
            return 1;
        }
        if (a == 0 && b == 0) {
            printf("0000
    ");
            return 0;
        }
        while (a > 0) {
            putchar('A');
            --a;
        }
        while (b > 0) {
            putchar('B');
            --b;
        }
        putchar('
    ');
        return 0;
    }
    
    int usr_input(char array[])
    {
            printf("Please input four different letters:
    ");
            if (scanf("%s", array) != 1) {
                printf("Unkown error!
    ");
                return 1;
            }
            return 0;
    }
    void exit_output(char computer[])
    {
            printf("The answer is: %c%c%c%c
    ", computer[0], computer[1], computer[2], computer[3]);
    }
    void err_output(char str[])
    {
            printf("%s
    ", str);
    }
    int main(void)
    {
        char computer[100];
        char man[100];
        generate(computer);
        while (1) {
            usr_input(man);
            if ((strcmp(man, "EXIT") == 0) || (strcmp(man, "exit") == 0)) {
                exit_output(computer);
                return 0;
            }
            if (!is_valid(man)) {
                err_output("Invalid input!");
                continue;
            }
            if (compare(man, computer))
                return 0;
        }
    }
  • 相关阅读:
    HashMap Hashtable LinkedHashMap 和TreeMap
    RestTemplate -springwebclient
    IntelliJ IDEA 12:
    mac安装RabbitMQ
    mysql 常用,使用经验
    消息中间件性能究竟哪家强?
    log4j2配置文件log4j2.xml
    内存增长 避免
    nginx 服务器重启命令,关闭
    jquery获取css color 值返回RGB
  • 原文地址:https://www.cnblogs.com/zhuyaguang/p/4825180.html
Copyright © 2011-2022 走看看