gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:27:57 25 4
gpt4 key购买 nike

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

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;
}

最佳答案

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

rectangle2.h 基本上没问题。

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

这是整个文件的一个版本,它将按照您的设想工作。

矩形2.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矩形,以匹配 rectangle2.h 中的原型(prototype)。

矩形.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 和矩形.c,以下是 Linux 终端上的示例:cc -o rec main.cfragment.c -lm。开关 -lm 用于包含数学库。我认为在 Windows 终端中使用 borland 命令行编译器的相同示例将如下所示:bcc32 main.c矩形.c

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

关于c - 程序不断崩溃,程序的其他问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46821157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com