【问题标题】:Program for length and area of rectangle using functions使用函数计算矩形长度和面积的程序
【发布时间】:2016-05-24 01:44:18
【问题描述】:

我需要编写一个程序来计算矩形的面积和周长。它必须对面积使用一个函数,对周长使用第二个函数。

perimeter = 2 x length + 2 x width = 2l + 2w
area = legnth x width = l x w

到目前为止,我的代码是这样的:

#include <stdio.h>
double perimeter(double x);
double area(double w);
int main (){
    double x, w;
    printf("Enter the length: ");
    scanf("%lf", &x);
    prntf("Enter the width: ");
    scanf("%lf", &w)
    printf("The perimeter is %lf\n", perimeter);
    printf("The area is %lf\n", area);
    return 0;   
}   
double perimeter(double x, w){
    return (2*x)+(2*w);
}
double area(double x, w){
    return (x*w);
}

我收到以下错误:

(17):错误 C2146:语法错误:缺少 ';'在标识符“printf”之前
(17):警告 C4477:“printf”:格式字符串“%lf”需要“double”类型的参数,但可变参数 1 的类型为“double (___cdecl *)(double)”
(18):警告 C4477:“printf”:格式字符串“%lf”需要“double”类型的参数,但可变参数 1 的类型为“double (___cdecl *)(double)”
(21): 错误 C2081: 'w': 形参列表中的名称非法
(21):警告 C4029:声明的形参列表与定义不同
(24): 错误 C2081: 'w': 形参列表中的名称非法
(24): 警告 C4029: 声明的形参列表与定义不同

【问题讨论】:

    标签: c


    【解决方案1】:

    double perimeter(double x, double w) 这样的参数编写函数,而不是double perimeter(double x, w)

    【讨论】:

    • 谢谢你,解决了一堆错误。现在我要解决这个问题:(17) 警告 C4477: 'printf' : 格式字符串 '%lf' 需要和参数类型为 'double',但可变参数 1 的类型为 double (___cdecl *)(double,double)'
    • 我明白了。
    【解决方案2】:

    这里有几个问题。

    首先,你的定义函数与你声明的函数不匹配:

    //Declared:
    double perimeter(double x);
    double area(double w);
    
    //Defined:
    double perimeter(double x, w){
        return (2*x)+(2*w);
    }
    double area(double x, w){
        return (x*w);
    }
    

    在您的函数声明中,您只有一个参数,但在您的定义中,您尝试使用两个。

    试试这个:

    double perimeter(double length, double width){
        return (2*length)+(2*width);
    }
    double area(double length, double width){
        return (length*width);
    }
    

    第二个问题是当你调用你的函数时:

    printf("The perimeter is %lf\n", perimeter);
    printf("The area is %lf\n", area);
    

    您没有将 lengthwidth 值传递给您的函数。 在第二次 scanf 之后,您还漏掉了一个分号。

    试试这个:

    int main (){
        double length, width;
        printf("Enter the length: ");
        scanf("%lf", &length);
        prntf("Enter the width: ");
        scanf("%lf", &width);
        printf("The perimeter is %lf\n", perimeter(length, width);
        printf("The area is %lf\n", area(length, width);
        return 0;   
    }   
    

    祝你好运。

    【讨论】:

      【解决方案3】:

      下面的代码是你的程序运行正常,错误是你使用函数的方式,考虑上面其他人给你写的东西。

      #include <stdio.h>
      
      double perimeter(double x, double w){
          return (2*x)+(2*w);
      }
      double area(double x, double w){
          return (x*w);
      }
      
      int main (){
          double x, w;
          printf("Enter the length: ");
          scanf("%lf", &x);
          printf("Enter the width: ");
          scanf("%lf", &w);
          printf("The perimeter is %lf\n", perimeter(x,w));
          printf("The area is %lf\n", area(x,w));
          return 0;
      }
      

      检查此代码并找出您的错误。

      【讨论】:

        【解决方案4】:

        你的代码有问题

        声明和定义不匹配。正确的声明应该是
        double perimeter(double w, double x)double area(double w,double x)

        注意:始终记住,在声明、定义和调用相应函数时,参数的顺序、类型和数量必须匹配。

        当您在 printf 中调用函数 perimeterarea 时,您没有传递参数。正如您将在声明和定义函数中看到的那样,您使用了perimeter(double x,w)area(double x,w)

        所以正确的行应该是

         printf("The perimeter is %lf\n", perimeter(x,w));
         printf("The area is %lf\n", area(x,w));
        

        【讨论】:

          【解决方案5】:

          好的,看来你的错误更多,所以我将它们全部更正并发布带有完整更正代码的新答案,你可以将它与你的比较。

          #include <stdio.h>
          double perimeter(double x, double w);
          double area(double x, double w);
          int main (){
              double x, w;
              printf("Enter the length: ");
              scanf("%lf", &x);
              printf("Enter the width: ");
              scanf("%lf", &w);
              printf("The perimeter is %lf\n", perimeter(x, w));
              printf("The area is %lf\n", area(x, w));
              return 0;
          }
          double perimeter(double x, double w){
              return (2*x)+(2*w);
          }
          double area(double x, double w){
              return (x*w);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-01-17
            • 1970-01-01
            • 1970-01-01
            • 2012-11-12
            • 1970-01-01
            • 1970-01-01
            • 2023-03-16
            • 2017-11-21
            相关资源
            最近更新 更多