【问题标题】:Probelm with struct and typedef [closed]struct和typedef的问题[关闭]
【发布时间】:2021-03-21 21:47:55
【问题描述】:

我必须开发这4个功能:

  1. double distance (const point_t * a, const point_t * b)
  2. double perimeter (const polygon_t * poly)
  3. polygon_t create_triangle (point_t p0, double base, double height)
  4. 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-&gt;x - b-&gt;x, 2) + pow(a-&gt;y - b-&gt;y, 2));
  • 这是 C,不是 C++?请仅标记您使用的语言。
  • create_square 函数的最开始有错误。您指的是 p2.len - 但它没有设置!您正在使用 p2.points - 但它从未分配过。添加 p2.len = 4; p2.points = malloc(4*sizeof(point_2));
  • 我投票结束这个问题,因为 OP 正在为各种服务请求广泛的刷机请求:包括 设计我的算法列表调试我的非- 可编译代码。此外,OP 并未积极响应 cmets。
  • 亲爱的 Sam,以后请尽量让您的问题简短而准确。谢谢

标签: arrays c struct typedef


【解决方案1】:

除了不正确的算法之外,您的实现的语法将无法编译。以下所有问题都可以在您的编译输出中识别出来如果它设置为显示所有警告。无论如何,一些错误会导致错误和编译失败。

问题 1 - 未初始化的变量:

   size_t i;
   point_t p2;
   polygon_t p1;


   for (i = 0; i < p1.len; ++i)
                   ^^^^^^  uninitialized

p1.len 在首次使用时未初始化。它可以是运行时的任何值,可能调用undefined behavior

还有更多可能未初始化的变量:

Build Status (so.prj - Debug)
 s0_15.c - 9 warnings
  
  60, 20    warning: variable 'p1.len' may be ***uninitialized***when used here 
      57, 4    note: variable 'p1' is declared here
  61, 7    warning: variable 'p1.points' may be ***uninitialized***when used here 
      57, 4    note: variable 'p1' is declared here
  64, 14    warning: variable 'p1.points' may be ***uninitialized***when used here 
      57, 4    note: variable 'p1' is declared here
  77, 21    warning: variable 'p2.len' may be ***uninitialized***when used here 
      72, 5    note: variable 'p2' is declared here
  79, 8    warning: variable 'p2.points' may be ***uninitialized***when used here 
      72, 5    note: variable 'p2' is declared here
  83, 24    warning: variable 'p2' may be ******uninitialized****** when used here 
      72, 5    note: variable 'p2' is declared here

注意:实际位置在您的输出中会有所不同。

问题 2 - 缺少花括号和超出范围的对象:

polygon_t create_square(point_t p0, double side) {
    polygon_t p2;//has block scope!
    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];

这里没有定义p2,只在上面的{...}内定义。

是因为这样的说法:

   for (i = 0; i < p2.len; ++i)
       p2.points[i].x= 0;
    }

缺少左大括号{

   for (i = 0; i < p2.len; ++i)
    {//add this curly
       p2.points[i].x= 0;
    }

【讨论】:

    【解决方案2】:

    首先需要固定距离:

    #define pow2(a) ((a) * (a))
    
    /* da implementare */
    double distance(const point_t* a, const point_t* b) {
        return sqrt(pow2(a->x - b->x) + pow2(a->y - b->y));
    }
    

    对于周长,您需要所有点之间以及最后一个点和第一个点之间的距离:

    /* da implementare: deve usare distance() */
    double perimeter(const polygon_t* poly) {
        double res = 0;
    
        for (int i = 0; i < poly->len - 1; i++) {
            res += distance(&poly->points[i], &poly->points[i+1]);
        }
        res += distance(&poly->points[poly->len - 1], &poly->points[0]);
        return res;
    }
    

    然后你需要 malloc 点数组来存储一些 point_t

    对于三角形:

    
     |\
     | \
    h|  \
     |___\
    p0 base
    
    /* da implementare */
    polygon_t create_triangle(point_t p0, double base, double height) {
        polygon_t p;
        p.len = 3;
        p.points = malloc(3 * sizeof(point_t));
        p.points[0] = p0;
        p.points[1].x = p0.x + base;
        p.points[1].y = p0.y;
        p.points[2].x = p0.x;
        p.points[2].y = p0.y + height;
    
        return p;
    }
    

    正方形

    +------+
    |      |
    |      |
    +------+
    p0
    
    /* da implementare */
    polygon_t create_square(point_t p0, double side) {
        polygon_t p;
        p.len = 4;
        p.points = malloc(4 * sizeof(point_t));
        p.points[0] = p0;
        p.points[1].x = p0.x;
        p.points[1].y = p0.y + side;
        p.points[2].x = p0.x + side;
        p.points[2].y = p0.y + side;
        p.points[3].x = p0.x + side;
        p.points[3].y = p0.y;
        return p;
    }
    

    你需要释放p.points

    【讨论】:

    • @Bathsheba 我删除了pow 的宏。我已经使用它,因为数学库已经存在
    猜你喜欢
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多