0

我在粘贴下面的代码时遇到问题,它只是在运行时挂起。VS2010 没有给我任何警告或错误。

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


void clear_buffer(void)
{
    while(getchar() != '\n');
}

int validate(int low, int high) {
    int num;

    scanf("%d", &num);
    while(num < low || num > high) 
    {
        clear_buffer();
        printf("INVALID! Must enter value between %d and %d: ", low, high);
        scanf("%d", &num);
    }
    return num;
}

int getRand(int max) {
    int number;
    number = rand() % max + 1;
    return number;
}

int validatePick(int pick, int one, int two, int three, int four, int five) {
    int valid = 0;

    if (pick != one && pick != two && pick != three && pick != four && pick != five) {
        valid = 1;
    } else {
        valid = 0;
    }
    return valid;
}
void prnt(int qty, int one, int two, int three, int four, int five, int six) {
    int i = 0, flag = 0;
    while (flag != 2) {
        flag = 0;
        if (sort2(&one, &two) == 0 && sort2(&three, &four) == 0 && sort2(&five, &six) == 0)
            flag = 1;
        if (sort2(&two, &three) == 0 && sort2(&four, &five) == 0)
            flag = 1;
        flag += flag;
    }

    printf("Picks: %d, ", one);
    while (i <= qty){
        if (i== 2 && qty == 2)
            printf("%d\n", two);
        else if (i == 2 && qty != 2) 
            printf("%d, ", two);

        if (i == 3 && qty == 3) 
            printf("%d\n", three);
        else if (i == 3 && qty != 3) 
            printf("%d, ", three);

        if (i == 4 && qty == 4)
            printf("%d\n", four);
        else if (i == 4  && qty != 4)
            printf("%d, ", four);

        if (i == 5 && qty == 5)
            printf("%d\n", five);
        else if (i == 5  && qty != 5) 
            printf("%d, ", five);

        if (i == 6 && qty == 6) 
            printf("%d\n", six);
        i++;
    }
}



int sort2(int *n1, int *n2) {
    int tmp, valid = 0;

    if (*n1 > *n2)
    {
        tmp = *n2;
        *n2 = *n1;
        *n1 = tmp;
        valid = 1;
    }
    return valid;
}

int main () {
    int num1, num2;
    int pick, one = 0, two = 0, three = 0, four = 0, five = 0, six = 0;

    srand(time(NULL));

    printf("LOTTERY GENERATOR\n");
    printf("Enter the maximum value between 1 and 100: ");
    num1 = validate(2,100);
    printf("Enter quantity of numbers to pick, between 1 and 6: ");
    num2 = validate(1, 6);

    one = getRand(num1);
    while (two == 0 || three == 0 || four == 0 || five == 0 || six == 0) {
        pick = getRand(num1);
        if (validatePick(pick, one, two, three, four, five) == 1 && two == 0)
            two = pick;
        else if (validatePick(pick, one, two, three, four, five) == 1 && three == 0)
            three = pick;
        else if (validatePick(pick, one, two, three, four, five) == 1 && four == 0)
            four = pick;
        else if (validatePick(pick, one, two, three, four, five) == 1 && five == 0)
            five = pick;
        else  if (validatePick(pick, one, two, three, four, five) == 1)
            six = pick;
    }
    prnt(num2, one, two, three, four, five, six);
}

如果我输入 3 forEnter the maximum value between 1 and 100然后 2 程序就会挂起。我不明白为什么会这样。我没有在代码中看到错误。有任何想法吗?

4

2 回答 2

5

我认为问题在于你的逻辑。如果我正确理解该程序,您将尝试选择 6 个不同的数字,它们在从1到的范围内随机生成num1

现在问题来了:只有当所有六个变量 ( , .. ) 都不是时,您的while循环才会终止。可以将这些变量之一设置为非零值的唯一方法是 if返回,并且只有在生成的随机数尚未分配给, ...之一时才会发生这种情况。onetwosix0validatePick1onetwosix

这归结为类似反向鸽子洞问题。你试图用少于六只鸽子来填满六个鸽子洞,这是一项不可能完成的任务。

如果num1小于6,那么你不可能满足你的 while 循环的终止条件,你的程序似乎会挂起。

您可以通过在您的 while 循环中放置一个else案例并打印您生成的随机数以及在每次迭代中打印每个变量的值来验证这一点。

请注意,您的第二个输入 ,num2直到您的while循环之后才会以任何方式被引用,因此您在此处输入的值将无法限制您的程序尝试生成的唯一随机值的数量。

于 2012-11-07T02:34:01.210 回答
0

这可能是由于缓冲。请注意,如果3输入,则检查通过并且clear_buffer不会被调用。尝试更改函数,以便在输入之后调用它。

这就是我不太喜欢的原因scanf——很容易忘记缓冲区的状态,并发现你的程序卡在一个循环中。我更喜欢阅读整行,例如,使用fgets,然后标记它并自己解析它。对于像这样的简单程序,它非常容易使用strtol,甚至只是atoi. 至少通过这种方式,我可以更直接地了解所有输入的许多边缘情况。

于 2012-11-07T02:28:46.877 回答