我不知道我在做什麼錯。其中一個錯誤是矩形首先在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