-2

我在第 41 行有一个错误:“int”之前的预期表达式 第 45 行:建议在“&”的操作数中使用括号来比较

我还想确定 43-45 中的代码是否有意义。

我是第一次在这个论坛上发帖,我是 C 的新手,所以请忍受这篇文章的业余性质

#include<stdio.h>
#include<math.h>
int main(void)

{
 // LOCAL DECLARATIONS

 int bricks; //the number of bricks available
 int spheres; //the number of spheres available
 int prisms; //the number of prisms available
 int final_string(int bricks, int spheres, int prisms); //the longest possible string with the given shapes in an alternating fashion

 // EXECUTABLE STATEMENTS

 printf("\nEnter the number of bricks: ");
 scanf("%d", &bricks);
 printf("Enter the number of spheres: ");
 scanf("%d", &spheres);
 printf("Enter the number of prisms: ");
 scanf("%d", &prisms);
 printf("\nLongest possible string of alternating shapes usuing only two different shapes: %d", int final_string(int bricks, int spheres, int prisms));

 int final_string(int bricks,int spheres,int prisms);
{
 return bricks * (bricks > spheres & bricks > prisms) + spheres * (spheres > bricks & spheres > prisms) + prisms * (prisms > bricks & prisms > spheres);
}

return(0);
4

3 回答 3

4
  1. Remove all the ints from the line:

    printf("\nLongest possible string of alternating shapes usuing only two different shapes: %d", int final_string(int bricks, int spheres, int prisms));
    
  2. Remove the semicolon after the function signature in the definition of final_string

  3. Move final_string outside of main
  4. Use the logical && operator instead of the bitwise &
  5. Buy and read a C book
  6. In the future ask "questions" like this on http://codereview.stackexchange.com
于 2013-06-24T16:47:44.870 回答
0

干得好 :

http://cfiddle.net/TyZAKw

如上所述,您放了太多“int”“;” 在实现它时在函数签名之后和 & 而不是 &&

于 2013-06-24T17:17:05.587 回答
0

线,

printf("\nLongest possible string of alternating shapes usuing only two different shapes: %d",  int final_string(int bricks, int spheres, int prisms));

是错的。在这一行中,int final_string(int bricks, int spheres, int prisms)是函数声明而不是调用它。将其替换为final_string(bricks, spheres, prisms)

并在外部定义该功能main。在C中,函数不能嵌套。所以你更正的代码看起来,

#include<stdio.h>
#include<math.h>
int main(void)
{
    // LOCAL DECLARATIONS

   int bricks; //the number of bricks available
   int spheres; //the number of spheres available
   int prisms; //the number of prisms available
   int final_string(int bricks, int spheres, int prisms); //the longest possible string with the given shapes in an alternating fashion

   // EXECUTABLE STATEMENTS

   printf("\nEnter the number of bricks: ");
   scanf("%d", &bricks);
   printf("Enter the number of spheres: ");
   scanf("%d", &spheres);
   printf("Enter the number of prisms: ");
   scanf("%d", &prisms);
   printf("\nLongest possible string of alternating shapes usuing only two different   shapes: %d", final_string( bricks,  spheres, prisms));
   return(0);
}

int final_string(int bricks,int spheres,int prisms)
{
    return bricks * (bricks > spheres & bricks > prisms) + spheres * (spheres > bricks & spheres > prisms) + prisms * (prisms > bricks & prisms > spheres);
}
于 2013-06-24T16:48:15.340 回答