程序不断崩溃,程序的其他问题

时间:2017-10-19 00:22:08

标签: c

我不确定我做错了什么。其中一个错误是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;
}

1 个答案:

答案 0 :(得分: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() 您还需要在函数isSquare和isGolden中使用struct length替换struct rectangle以匹配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。 switch -lm包含数学库。我认为使用borland命令行编译器的Windows终端中的相同示例将是这样的:bcc32 main.c rectangle.c

我希望这会有所帮助。抱歉,暂时没有时间写更多内容。