2017-10-19 40 views
-3

我不知道我在做什么错。其中一个错误是矩形首先在int main()中被提及。我需要该程序向用户询问2个矩形的尺寸,并进行一些计算并返回这些值。我也希望它以某种方式将结构名称合并到头文件中。谢谢程序不断崩溃,程序的其他问题

rectangle2.h

struct rectangle 
{ 
    double length;  // variable to store length 
    double width;  // variable to store width 
}; 

// function to calculate the area 
double area( struct rectangle jane ); 
// function to calculate the perimeter 
double perimeter(struct rectangle luis); 
// function to calculate the diagonal length from one corner to another 
double diagonal(struct rectangle adrian); 
// function to determine if the rectangle is a square 
// returns true when it is a square, false when it is not 
bool isSquare(struct length fernie); 
// function to determine whether the rectangle is golden 
// https://en.wikipedia.org/wiki/Golden_rectangle 
// (a + b)/a is equal to a/b 
// returns true when it is a golden rectangle, false when it is not 
bool isGolden(struct length claudia); 
// function to determine if two rectangles are similar 
// two rectangles are similar if the ratio of the length and width of 
// one rectangle is equal to the ratio of the length and width of 
// the other rectangle 
bool areSimilar(struct rectangle pedro, struct rectangle omar); 

rectangle.c

#include "rectangle2.h" 
#include <stdio.h> 
#include <stdbool.h> 

double area(struct rectangle jane) //to calculate area of rectangle 
{ 
return jane.width * jane.length; 
} 

double perimeter(struct rectangle luis) //to calculate perimeter of rectangle 
{ 
return 2 * (luis.length + luis.width); 
} 

double diagonal(struct rectangle adrian) //to calculate diagonal of rectangle 
{ 
return (adrian.length * adrian.length) + (adrian.width * adrian.width); 
} 

bool isSquare(struct length fernie ) //checks if rectangles are square 
{ 
    if((fernie.width * fernie.length) == (fernie.length * fernie.length)) 
     return true; 

    else 
     return false; 
} 

bool isGolden(struct length claudia) //checks if rectangles are golden 
{ 
if(((claudia.width + claudia.length)/claudia.width) == (claudia.width/claudia.length)) 
return true; 

else 
return false; 
} 

bool areSimilar(struct rectangle pedro, struct rectangle omar) //checks if rectangles are similar 
{ 
if((pedro.length/pedro.width) == (omar.length/omar.width)) 
return true; 

else 
return false; 
} 

的main.c

int main() 

{ 
struct rectangle sides; 
sides.length; 
sides.width; 


//asks the user for the length and width for 2 rectangles 
printf("\nEnter dimensions of Rectangle 1: "); 

printf("\nEnter Length: "); 

scanf("%lf" , sides.length); 

printf("\nEnter Width: "); 

scanf("%lf" , sides.width); 
printf("\nEnter dimensions of Rectangle 2: "); 

printf("\nEnter Length: "); 

scanf("%lf",sides.length); 

printf("\nEnter Width: "); 

scanf("%lf" , sides.width); 


//printing statements after all calculations have been made 

printf("\nArea of Rectangle 1 is: %lf" , area(&jane)); 
printf("\nArea of Rectangle 2 is: %lf",area(rectangle.jane)); 

printf("\nPerimeter of Rectangle 1: %lf" , perimeter(rectangle.rec1.luis)); 
printf("\nPerimeter of Rectangle 2: %f",perimeter(rectangle.rec2.luis)); 

printf("\nDiagonal of Rectangle 1: %lf" , diagonal(rectangle.rec1.adrian)); 
printf("\nDiagonal of Rectangle 2: %lf" , diagonal(rectangle.rec2.adrian)); 

return 0; 
} 
+3

“保持崩溃”和“其他问题”不是真正的描述。我可以向你推荐例如https://ericlippert.com/2014/03/05/how-to-debug-small-programs/? – Evert

+0

乍一看,你的程序甚至不能编译:在main()中有一个'jane'变量的引用,而在'rectange'结构中有一个'jane'成员,它们都不存在。 – Evert

+0

甚至不编译。什么'rectangle.jane'应该在'main()'中? 'main()'中没有名为'rectangle'的变量。 – AlexP

回答

0

你的程序不能编译多个原因。

rectangle2.h大部分没问题。

您需要将函数isSquare和isGolden的参数从struct length更改为struct rectangle,因为struct length未定义。

这里是整个文件的一个版本,它可以像你想象的那样工作。

rectangle2.h

typedef enum {false, true = !false} bool; 

struct rectangle 
{ 
    double length; 
    double width; 
}; 

double area(struct rectangle jane); 
double perimeter(struct rectangle luis); 
double diagonal(struct rectangle adrian); 
bool isSquare(struct rectangle fernie); 
bool isGolden(struct rectangle claudia); 
bool areSimilar(struct rectangle pedro, struct rectangle omar); 

rectangle.c你不需要包括stdio.h因为你不打印,并在该文件中进行扫描。你需要包括math.h功能对角线在数学上是正确的。如果在使用这种方法时遇到任何问题,请将函数更改为没有sqrt()的方式。
您还需要将struct length替换为struct rectangle,其功能是isSquare和isGolden以匹配rectangle2.h中的原型。

rectangle.c

#include "rectangle2.h" 
#include <math.h> 

double area(struct rectangle jane) 
{ 
    return jane.width * jane.length; 
} 

double perimeter(struct rectangle luis) 
{ 
    return 2 * (luis.length + luis.width); 
} 

double diagonal(struct rectangle adrian) 
{ 
    return sqrt(adrian.length*adrian.length + adrian.width*adrian.width); 
} 

bool isSquare(struct rectangle fernie) 
{ 
    return (fernie.width == fernie.length); 
} 

bool isGolden(struct rectangle claudia) 
{ 
    double p = (claudia.length + claudia.width)/claudia.length; 
    double q = claudia.length/claudia.width; 
    return p == q; 
} 

bool areSimilar(struct rectangle pedro, struct rectangle omar) 
{ 
    return (pedro.length/pedro.width) == (omar.length/omar.width); 
} 

,最后你的main.c中是最差的。我相信你会通过查看我的例子来弄清楚它出了什么问题,但另外要求自由。

main.c

#include "rectangle2.h" 
#include <stdio.h> 

struct rectangle rect1, rect2; 
char *s; 

int main() 

{ 
    printf("\nEnter dimensions of Rectangle 1:"); 
    printf("\nEnter Length: "); 
    scanf("%lf", &rect1.length); 
    printf("\nEnter Width: "); 
    scanf("%lf", &rect1.width); 

    printf("\nEnter dimensions of Rectangle 2:"); 
    printf("\nEnter Length: "); 
    scanf("%lf", &rect2.length); 
    printf("\nEnter Width: "); 
    scanf("%lf", &rect2.width); 

    printf("\nArea of Rectangle 1 is: %lf", area(rect1)); 
    printf("\nArea of Rectangle 2 is: %lf", area(rect2)); 

    printf("\nPerimeter of Rectangle 1: %lf", perimeter(rect1)); 
    printf("\nPerimeter of Rectangle 2: %lf", perimeter(rect2)); 

    s = areSimilar(rect1, rect2) ? "true" : "false"; 
    printf("\nRectangle 1 & 2 are similar: %s", s); 

    s = isSquare(rect1) ? "true" : "false"; 
    printf("\nRectangle 1 is square: %s", s); 

    printf("\nDiagonal of Rectangle 1: %lf" , diagonal(rect1)); 
    printf("\nDiagonal of Rectangle 2: %lf\n" , diagonal(rect2)); 

    return 0; 
} 

,使你需要编译的main.c和rectangle.c可执行文件,这里是一个Linux终端上的例子:cc -o rec main.c rectangle.c -lm。 开关-lm包含数学库。我认为使用borland命令行编译器的windows终端中的相同示例如下所示:bcc32 main.c rectangle.c

我希望这可以帮助。对不起,现在没时间再写更多。