gpt4 book ai didi

编译器想要 ";"- 我不知道为什么(在 C 中)

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

在处理项目欧拉问题(#10)时,我遇到了一个有趣的编译错误。我的编译器告诉我需要一个分号。确切的错误是49: ';' expected

现在我知道这对于 C 代码来说并不罕见,但问题是我已经三重检查了所有分号、圆括号和方括号,没有丢失任何一个。甚至不在循环中。很可能我只是想念它。

但是编译器告诉我49: ';' expected - 但第 49 行的代码只是一个括号 ( { )。

有人知道这里可能出了什么问题吗?

代码如下:

// this is a program to find the sum of all primes below 2 million
// uses the Seive of Eratosthenes

#include <stdio.h>

int main() {
long int sum = 0;
long int s = 2000000; // size of array
long int i; // 'for' loops
long int n; // number to test
long int a; // prime checker
long int multiples;

long int* array; // define pointer for calloc int array
// calloc the array
array = (int*)calloc(s, sizeof(int)); // allocates 2,000,000 spots (s) of
// size "int" to array "array"
// set all array values equal to 1
for (i = 0; i < 2000000; i++) {
array[i] = 1;
}

/*
VALUES IN ARRAY
The values of the array indicates whether number-
(0) IS PRIME
(1) UNTESTED
(2) IS COMPOSITE
*/

// implement prime finder
for (n = 0; n < 2000000; n++) {
if (array[n] == 0) // IF n IS PRIME
{
multiples = n;
multiples += n;

while (multiples < 2000000) {
array[multiples] = 2;
multiples += n;
}
} else
(array[n] == 2) // IF n IS COMPOSITE (is a multiple)
{ // THIS IS LINE 49
continue;
}
else(array[n] == 1) // UNTESTED
{
for (a = 2; a < n; a++) // double checks for composites
{
// tests factors
if (n % a == 0) {
printf("ERROR");
goto end;
}
}

array[n] = 0;
multiples = n;
multiples += n;

while (multiples < 2000000) {
array[multiples] = 2;
multiples += n;
}
}
}

// read array and sum primes
for (n = 0; n < 2000000; n++) {
if (array[n] == 0) // IF n IS PRIME
{
sum += n;
} else
(array[n] == 2) // IF n IS COMPOSITE
{
continue;
}
else(array[n] == 1) // IF n MAY/MAY NOT BE PRIME
{
printf("ERROR");
goto end;
}
}

printf("The sum of all primes < 2,000,000 is... %ld!\n", sum);

end:
getchar();
return 0;
}

最佳答案

else 更改为 else if 就可以了。

并用所有其他来做到这一点。

关于编译器想要 ";"- 我不知道为什么(在 C 中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25223290/

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