2016-05-24 38 views
1

我需要編寫一個程序來計算矩形的面積和周長。它必須使用該區域的功能,併爲周界使用第二個功能。使用函數的矩形長度和麪積程序

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): error C2146: syntax error: missing ';' before identifier 'printf'
(17): warning C4477: 'printf' : format string '%lf" requires an argument of type 'double', but variadic argument 1 has type 'double (___cdecl *)(double)'
(18): warning C4477: 'printf' : format string '%lf" requires an argument of type 'double', but variadic argument 1 has type 'double (___cdecl *)(double)''
(21): error C2081: 'w': name in formal parameter list illegal
(21): warning C4029: declared formal parameter list different from definition
(24): error C2081: 'w': name in formal parameter list illegal
(24): warning C4029: declared formal parameter list different from definition

回答

2

相反的double perimeter(double x, w),寫函數像這樣double perimeter(double x, double w)參數。

+1

謝謝你,拿了一堆錯誤的照顧。 (17)警告C4477:'printf':格式化字符串'%lf'需要和'double'類型的參數,但可變參數1的類型是double(___cdecl *)(double,double)' – Thad

+0

我明白了。 – Thad

-1

好吧,看起來你有更多的錯誤,所以我糾正它們,併發佈一個新的答案與全面更正的代碼,你可以與你的比較。

#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); 
} 
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); 

你是不是經過長度寬度值的功能。 你也在第二次掃描後錯過了一個分號。

試試這個:

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; 
} 

好運。

0

在下面的代碼中,你的程序是完美運行的,錯誤在於你使用你的函數的方式,考慮上面其他人寫給你的東西。

#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; 
} 

檢查此代碼與您的並找到您的錯誤。

0

您的代碼有問題

聲明和定義不匹配。正確的聲明將是
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)); 
相關問題