在C中调用函数时的预期表达错误

时间:2019-03-03 22:11:33

标签: c function random dice

我试图在C中第一次使用函数。我试图编写一个经典的掷骰子游戏,掷骰子10,000次,然后打印出使用函数将每个数字掷出多少次。 。

在下面的代码中,尝试设置result= roll_die (int num_sides);时,我不断收到错误代码“ Expected expression”。它说它发生在int上。当我删除int时,得到错误代码“使用未声明的标识符'num_sides'”。我该如何解决?

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

int roll_die(int num_sides){
num_sides = rand() % 6;
num_sides = num_sides + 1;
return num_sides;
}

int main(void)
{
srand((int)time(0));
unsigned int counter, result, num1=0, num2=0, num3=0, num4=0, num5=0, num6=0;
unsigned int num_rolls = 10000;
for(counter=0; counter<=num_rolls; counter++)
{
    result = roll_die (num_sides);
    if(result==1)
        num1++;
    else if(result==2)
        num2++;
    else if(result==3)
        num3++;
    else if(result==4)
        num4++;
    else if(result==5)
        num5++;
    else if(result==6)
        num6++;
    else{
        printf("Error occurred. \n"); return 0;
    }
}
printf("Number of 1s rolled: %d \n", num1);
printf("Number of 2s rolled: %d \n", num2);
printf("Number of 3s rolled: %d \n", num3);
printf("Number of 4s rolled: %d \n", num4);
printf("Number of 5s rolled: %d \n", num5);
printf("Number of 6s rolled: %d \n", num6);
}

1 个答案:

答案 0 :(得分:0)

正如我在评论中指出的那样,您需要用num_sides替换main()中的6,或者需要定义和初始化变量(int num_sides = 6;)。您的函数将6硬编码为边数,并忽略传入的值(它仅将参数用作局部变量,而忽略了作为值提供的内容)。

解决这两个问题会导致这样的代码:

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

static int roll_die(int num_sides)
{
    int result = rand() % num_sides + 1;
    return result;
}

int main(void)
{
    srand((int)time(0));
    unsigned int counter, result, num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0;
    unsigned int num_rolls = 10000;
    for (counter = 0; counter <= num_rolls; counter++)
    {
        result = roll_die(6);
        if (result == 1)
            num1++;
        else if (result == 2)
            num2++;
        else if (result == 3)
            num3++;
        else if (result == 4)
            num4++;
        else if (result == 5)
            num5++;
        else if (result == 6)
            num6++;
        else
        {
            printf("Error occurred. \n");
            return 0;
        }
    }
    printf("Number of 1s rolled: %d \n", num1);
    printf("Number of 2s rolled: %d \n", num2);
    printf("Number of 3s rolled: %d \n", num3);
    printf("Number of 4s rolled: %d \n", num4);
    printf("Number of 5s rolled: %d \n", num5);
    printf("Number of 6s rolled: %d \n", num6);
}

示例输出:

Number of 1s rolled: 1670 
Number of 2s rolled: 1653 
Number of 3s rolled: 1656 
Number of 4s rolled: 1687 
Number of 5s rolled: 1696 
Number of 6s rolled: 1639 

您还应该使用数组而不是6个numX变量。例如,使用int num[7] = { 0 };将使您可以将roll_die()返回的值用作数组的索引。这样压缩代码:

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

static int roll_die(int num_sides)
{
    return rand() % num_sides + 1;
}

int main(void)
{
    srand(time(0));
    unsigned num[7] = { 0 };
    unsigned num_rolls = 10000;
    int num_sides = 6;
    for (unsigned counter = 0; counter <= num_rolls; counter++)
        num[roll_die(num_sides)]++;
    for (int i = 1; i <= num_sides; i++)
        printf("Number of %ds rolled: %d \n", i, num[i]);
    return 0;
}

样本输出–除了随机序列不同之外,您还能发现与先前输出的任何差异吗?不应有任何东西!

Number of 1s rolled: 1705 
Number of 2s rolled: 1651 
Number of 3s rolled: 1653 
Number of 4s rolled: 1616 
Number of 5s rolled: 1631 
Number of 6s rolled: 1745 

这使得对N边骰子的归纳变得容易,像这样:

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

static int roll_die(int num_sides)
{
    return rand() % num_sides + 1;
}

int main(int argc, char **argv)
{
    int num_sides = 6;
    if (argc == 2)
    {
        num_sides = strtol(argv[1], 0, 0);
        if (num_sides < 2 || num_sides > 999)
        {
            fprintf(stderr, "Number of sides of %d is not in the range 2..999\n", num_sides);
            exit(EXIT_FAILURE);
        }
    }
    srand(time(0));

    unsigned num[num_sides + 1];
    for (int i = 1; i <= num_sides; i++)
        num[i] = 0;

    unsigned num_rolls = 10000;
    for (unsigned counter = 0; counter <= num_rolls; counter++)
        num[roll_die(num_sides)]++;

    for (int i = 1; i <= num_sides; i++)
        printf("Number of %ds rolled: %d \n", i, num[i]);

    return 0;
}

而且,如果将此程序称为die83,则示例运行可能类似于:

$ ./die83 8
Number of 1s rolled: 1294 
Number of 2s rolled: 1197 
Number of 3s rolled: 1256 
Number of 4s rolled: 1228 
Number of 5s rolled: 1230 
Number of 6s rolled: 1222 
Number of 7s rolled: 1278 
Number of 8s rolled: 1296 
$