【问题标题】:Bulls and Cows - turning simple code into functionsBulls and Cows - 将简单的代码变成函数
【发布时间】:2021-06-13 16:06:00
【问题描述】:

我正在尝试编写公牛和奶牛游戏。 该程序生成一个 4 位数字,用户需要猜测这些数字。 如果用户猜到了一个数字和它的位置,那就是公牛。 如果用户只猜到了这个数字,那么它就成功了。 我需要在 3 个文件中将它发送给我们的老师:function.h、function.c 和 main.c

我不知道如何将代码分成 3 个不同的文件。 每次我试图从一个动作中创建一个函数时,它都不像简单的代码那样工作。 老师让我们为代码生成器、猜测的验证、公牛和命中写一个函数。 我将不胜感激。

简单代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    char code[5];
    char guess[5];
    int bulls, hits;
    int i, j;

    srand(time(NULL));

    for (i = 0; i < 4; i++) {
        code[i] = rand() % 9 + '0';
        for (j = 0; j < i; j++) {
            if (code[j] == code[i]) {
                i--;
            }
        }
    }

    for (i = 0; i < 4; i++) {
        printf("%c", code[i]);
    }
    putchar('\n');

    do {
        bulls = 0;
        hits = 0;
        
        scanf("%s", guess);

        for (i = 0; i < 4; i++) {
            for (j = i + 1; j < 4; j++) {
                if (guess[i] == guess[j]) {
                    printf("Invalid entry. The digits have to differ\n");
                    
                    break;
                }
            }
        }

        for (i = 0; i < 4; i++) {
            for (j = 0; j < 5; j++) {
                if (code[i] == guess[j]) {
                    if (i == j) {
                        bulls++;
                    } else {
                        hits++;
                    }
                }
            }
        }

        printf("%d bulls %d hits\n", bulls, hits);
    } while (bulls != 4);
    printf("you won");

    return 0;
}

函数.h:

//A function that gets an array of 5 chars and contains the random 4 digits:
void GenerateCode (char code[], int i, int j);
//A function that gets an array of 5 chars and checks if the guess is valid:
int validateGuess (char guess[], int i, int j);
//A function that counts the bulls (if the user guessed the number and its position):
int CountBull(char code[], int len1, char guess[],int len2);
//A function that counts the hits (if the user guessed  only the number, not the position):
int CountHits (char code[], char guess[], int size);
//A function that prints the number of hits and bulls:
void PrintScore (int bulls, int hits);

【问题讨论】:

  • 其实你的英语对于非英语母语的人来说已经相当不错了:)
  • 谢谢!!:):)
  • 尝试找出你需要采取的具体行动,并将它们放在独立的函数中。例如,您可以编写一个函数,该函数接受一个数字和一个猜测,并返回公牛和奶牛的结果(您可以通过指针参数返回它们)。或者你可以进一步分解它,有一个单独的公牛功能和奶牛功能。在这种情况下,您需要告诉 cows 函数忽略哪些已被识别为公牛的数字。
  • :D 非常欢迎
  • 根据经验,“简单代码”是结构良好的代码,即使是初学者也能理解。因此,如果您需要就任何代码提出问题,它通常不符合“简单”的条件。相反,您拥有的是(或多或少)用于小程序的复杂代码。您可以将此视为对未来的一般建议:如果您需要帮助,请以使帮助看起来有价值的方式描述您的问题,而不是贬低所需的努力;)

标签: c function


【解决方案1】:

这里。我稍微改进了一下。

function.h:

#pragma once

char code[5];
char guess[5];
int bulls, hits;
int i, j;

//A function that gets an array of 5 chars and contains the random 4 digits:
void GenerateCode();

//A function that gets an array of 5 chars and checks if the guess is valid:
int ValidateGuess();

//A function that counts the bulls (if the user guessed the number and its position):
int CountBullsAndHits(char code[], char guess[]);

//A function that prints the number of hits and bulls:
void PrintScore();

function.c:

#include "function.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void GenerateCode() {
    srand(time((void *)0));

    for (i = 0; i < 4; i++)
    {
        code[i] = rand() % 9 + '0';
        for (j = 0; j < i; j++)
        {
            if (code[j] == code[i])
            {
                i--;
            }
        }
    }

    printf("%s\n", code); // Remove this line if you want a true guessing game!
    printf("Guess the number! : ");
    ValidateGuess(guess);
    printf("You won!\n");
}

int ValidateGuess() {
    do {

    bulls = 0;
    hits = 0;

    scanf("%s", guess);

    for (i = 0; i < 4; i++)
    {
        for (j = i + 1; j < 4; j++)
        {
            if (guess[i] == guess[j])
            {
                printf("Invalid entry. The digits have to differ\n");

                break;
            }
        }
    }

    CountBullsAndHits(code, guess);
    PrintScore();
    } while (bulls != 4);
}

int CountBullsAndHits(char code[], char guess[]) {
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 5; j++)
        {
            if (code[i] == guess[j])
            {
                if (i == j)
                {
                    bulls++;
                }
                else {
                    hits++;
                }
            }
        }
    }
}

void PrintScore() {
    printf("%d bulls, %d hits\n", bulls, hits);
}

main.c:

#include "function.h"

int main()
{
    GenerateCode();
    return 0;
}

【讨论】:

    猜你喜欢
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多