我不知道我在做什么错。其中一个错误是矩形首先在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;
}
“保持崩溃”和“其他问题”不是真正的描述。我可以向你推荐例如https://ericlippert.com/2014/03/05/how-to-debug-small-programs/? – Evert
乍一看,你的程序甚至不能编译:在main()中有一个'jane'变量的引用,而在'rectange'结构中有一个'jane'成员,它们都不存在。 – Evert
甚至不编译。什么'rectangle.jane'应该在'main()'中? 'main()'中没有名为'rectangle'的变量。 – AlexP