【发布时间】:2021-03-21 21:47:55
【问题描述】:
我必须开发这4个功能:
double distance (const point_t * a, const point_t * b)double perimeter (const polygon_t * poly)polygon_t create_triangle (point_t p0, double base, double height)polygon_t create_square (point_t p0, double side)
程序主体的其余部分已经定义,但我不知道如何开发最后两个功能,我需要有人指出正确的方法。
我的想法是初始化向量,然后能够从中制作正方形和三角形,但我在开发函数时遇到了困难。 你能帮帮我吗?
附:我把练习的交付和我自己写的内容留给你:
“该程序使用动态点数组来表示任意边数的多边形。三角形和正方形,作为著名的多边形,可以使用 create_triangle() 和 create_square() 快捷方式方便地构建。 要构造其他多边形,必须通过适当的分配手动填充 struct polygon_t。 main() 构造一个三角形和一个正方形,并通过调用 perimeter() 函数计算周长。 polygon_t 和 point_t 类型以及 print_polygon() 函数已经实现,它们位于源代码的顶部,不可编辑。
实现以下函数,以便 main() 编译并完成程序:
函数polygon_t create_triangle (point_t p0, double base, double height),以便它按值返回polygon_t 类型的结构,该结构适当地填充了形成等腰三角形的点,该等腰三角形的原点(左下点)位于p0、底基长度和高度长度高度;
函数polygon_t create_square (point_t p0, double side),以便它按值返回polygon_t 类型的结构,该结构适当地填充了形成正方形的点,该正方形在p0 中具有原点(左下点),边长为边;
函数double perimeter (const polygon_t * poly),以便计算作为参数传递的多边形的周长:在所有连续点对之间使用distance()函数并计算边的总和;
不要对边数做任何假设:数据结构可以表示任何多边形;
函数double distance (const point_t * a, const point_t * b) 计算两个数据点之间的欧几里得距离。
示例:以下程序构造一个边为 2 的正方形和一个底边为 1 且高为 0.86602 的三角形;然后计算两者的周长。”
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
char* readline();
char* ltrim(char*);
char* rtrim(char*);
double parse_double(char*);
typedef struct {
double x, y;
} point_t;
typedef struct {
size_t len;
point_t* points;
} polygon_t;
void print_polygon(const polygon_t* poly) {
size_t i;
printf("polygon_t(%ld)[", poly->len);
for (i = 0; i < poly->len; ++i) {
printf("(%g, %g) ", poly->points[i].x, poly->points[i].y);
}
printf("\b]");
}
/* da implementare */
double distance(const point_t* a, const point_t* b) {
float dist;
point_t* x = (point_t*) a;
point_t* y = (point_t*) b;
dist= sqrt((a->x - b->x) + (a->y - b->y));
return dist;
}
/* da implementare: deve usare distance() */
double perimeter(const polygon_t* poly) {
float per_tr= distance(poly->points, poly->points) + distance(poly->points, poly->points) +
distance(poly->points, poly->points);
float per_sq= distance(poly->points, poly->points) * 4;
return per_sq && per_tr;
}
/* da implementare */
polygon_t create_triangle(point_t p0, double base, double height) {
size_t i;
point_t p2;
polygon_t p1;
for (i = 0; i < p1.len; ++i)
p1.points[i].x= 0;
//base= distance(p1.points, p1.points);
//height= sqrt(pow(distance(p1.points, p1.points),2) - (pow(base,2)/4.0)) ;
p1.len[p1.points].x= 0;
return p1;
}
/* da implementare */
polygon_t create_square(point_t p0, double side) {
polygon_t p2;
size_t i;
p0.x = 0;
p0.y = 0;
for (i = 0; i < p2.len; ++i)
p2.points[i].x= 0;
}
p2.len[p2.points]= p2.points[side * 4,0];
// side= distance(p2.points, p2.points);
return p2;
}
int main()
{
FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");
double x = parse_double(ltrim(rtrim(readline())));
double y = parse_double(ltrim(rtrim(readline())));
double l = parse_double(ltrim(rtrim(readline())));
double b = parse_double(ltrim(rtrim(readline())));
double h = parse_double(ltrim(rtrim(readline())));
point_t origin;
polygon_t sq;
polygon_t tr;
origin.x = x; /* input riga #1 */
origin.y = y; /* input riga #2 */
sq = create_square(origin, l); /* input riga #3 */
tr = create_triangle(origin, b, h); /* input riga #4, #5 */
double p1 = perimeter(&sq);
double p2 = perimeter(&tr);
fprintf(fptr, "%.2f\n", p1); /* output riga #1 */
fprintf(fptr, "%.2f\n", p2); /* output riga #2 */
fclose(fptr);
return 0;
}
char* readline() {
size_t alloc_length = 1024;
size_t data_length = 0;
char* data = malloc(alloc_length);
while (true) {
char* cursor = data + data_length;
char* line = fgets(cursor, alloc_length - data_length, stdin);
if (!line) {
break;
}
data_length += strlen(cursor);
if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
break;
}
alloc_length <<= 1;
data = realloc(data, alloc_length);
if (!data) {
data = '\0';
break;
}
}
if (data[data_length - 1] == '\n') {
data[data_length - 1] = '\0';
data = realloc(data, data_length);
if (!data) {
data = '\0';
}
} else {
data = realloc(data, data_length + 1);
if (!data) {
data = '\0';
} else {
data[data_length] = '\0';
}
}
return data;
}
char* ltrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
while (*str != '\0' && isspace(*str)) {
str++;
}
return str;
}
char* rtrim(char* str) {
if (!str) {
return '\0';
}
if (!*str) {
return str;
}
char* end = str + strlen(str) - 1;
while (end >= str && isspace(*end)) {
end--;
}
*(end + 1) = '\0';
return str;
}
double parse_double(char* str) {
char* endptr;
double value = strtod(str, &endptr);
if (endptr == str || *endptr != '\0') {
exit(EXIT_FAILURE);
}
return value;
}
【问题讨论】:
-
检查距离公式:
sqrt(pow(a->x - b->x, 2) + pow(a->y - b->y, 2)); -
这是 C,不是 C++?请仅标记您使用的语言。
-
create_square 函数的最开始有错误。您指的是 p2.len - 但它没有设置!您正在使用 p2.points - 但它从未分配过。添加 p2.len = 4; p2.points = malloc(4*sizeof(point_2));
-
我投票结束这个问题,因为 OP 正在为各种服务请求广泛的刷机请求:包括 设计我的算法列表 和 调试我的非- 可编译代码。此外,OP 并未积极响应 cmets。
-
亲爱的 Sam,以后请尽量让您的问题简短而准确。谢谢