gpt4 book ai didi

c - 在 C 中调用函数时出现预期表达式错误

转载 作者:行者123 更新时间:2023-11-30 16:21:09 25 4
gpt4 key购买 nike

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

在下面的代码中,当尝试设置 result= roll_die (int num_sides); 时,我不断收到错误代码“预期表达式”。它说它发生在 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);
}

最佳答案

正如我在评论中指出的,您需要将 main() 中的 num_sides 替换为 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
$

关于c - 在 C 中调用函数时出现预期表达式错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54974265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com