C 程序持续崩溃,程序出现其他问题

C 程序持续崩溃,程序出现其他问题,c,C,我不确定我做错了什么。其中一个错误是在intmain()中首先提到矩形。我需要的程序,要求用户为2个矩形的尺寸,做一些计算,并返回这些值。我还希望它以某种方式将结构名称合并到头文件中。多谢各位 矩形2.h struct rectangle { double length; // variable to store length double width; // variable to store width }; // function to calcu

我不确定我做错了什么。其中一个错误是在int
main()
中首先提到矩形。我需要的程序,要求用户为2个矩形的尺寸,做一些计算,并返回这些值。我还希望它以某种方式将结构名称合并到头文件中。多谢各位

矩形2.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 );
矩形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;
}

由于多种原因,您的程序无法编译

矩形2.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和rectangle.c,下面是linux终端上的一个示例:
cc-orec main.c rectangle.c-lm
。 开关-lm包含数学库。我认为在使用borland命令行编译器的windows终端中,相同的示例如下:
bcc32 main.c rectangle.c


我希望这有帮助。很抱歉,现在没有时间再写了。

关于“持续崩溃”和“其他问题”的描述并不真实。例如,您可以参考一下吗?乍一看,您的程序甚至无法编译:在
main()
中有一个对
jane
变量的引用,以及
rectange
结构的
jane
成员,两者都不存在。甚至不编译。什么是
矩形。jane
应该在
main()
中?
main()
中没有名为
rectangle
的变量。使用所有警告和调试信息进行编译,即使用
gcc-Wall-Wextra-g
。改进您的代码以避免收到警告。然后使用调试器
gdb
。您的“修复我的代码”问题已脱离主题。顺便说一句,请将
\n
放在
printf
格式控制字符串的末尾(而不是开头)。畏避
#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);
}
#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;
}