1

我想写一个程序,你给正方形的两个顶点,然后程序找到另外两个。

例如:

输入:

2 3 4 5

输出

(3,6) (0,5)

但是当它从程序中读取数据时我遇到了问题,一切正常

int main(void)
{

    rationalNumber *z,e,f,g,h;

    point *x, a, b, c, d;

    e.l = 2; e.m = 1; f.l=3; f.m=1; g.l = 4; g.m = 1; h.l=5; h.m=1;

    a.x = e;
    a.y = f;
    b.x = g;
    b.y = h;
    output_point(D_2(z,a, b));


return 0;
}

但是当从用户那里读取数据时,我收到了这条消息

“线程 1:EXC_BAD_ACCESS(代码=1,地址=0x0)”

在这个地方:

if (scanf("%d/%u",&(a->n), &(a->d)) == 1) a->d=1;

所有代码:

#include<stdio.h>
#include<stdlib.h>

typedef struct rn{
    int n;   /**numerator**/
    unsigned d;    /**denomirator**/
} rationalNumber;

int gcd(int,int);

void output(rationalNumber);
void input(rationalNumber *);
void Minus(rationalNumber*, rationalNumber, rationalNumber);
void Sum( rationalNumber *, rationalNumber, rationalNumber);
void Multiplication(rationalNumber *, rationalNumber, rationalNumber);
void Reciprocal( rationalNumber *);

typedef struct dot{
    rationalNumber x;
    rationalNumber y;
} point;

void load_point(point *);
void output_point(point);
point C_1(rationalNumber *, point, point);
point D_1(rationalNumber *, point, point);
point C_2(rationalNumber *, point, point);
point D_2(rationalNumber *, point, point);


int main(void)
{

    rationalNumber *z;
    point *a,b,c;

    load_point(a);
    b = *a;
    load_point(a);
    c = *a;

    output_point(D_2(z,b, c));


return 0;
}

int gcd(int a, int b)
{
  if(b!=0)
    return gcd(b,a%b);

       return a;
}


void output(rationalNumber a)
{
    if(a.d == 1)  printf("%d",a.n);
    else printf("%d/%u",a.n, a.d);
}

void input(rationalNumber *a)
{
    int nwd;
    if (scanf("%d/%u",&(a->n), &(a->d)) == 1) a->d=1;
    else
    {
        nwd = abs(gcd(a->n, a->d));
        a->n = a->n/nwd;
        a->d = a->d/nwd;
    }


}

void Minus(rationalNumber *a, rationalNumber b, rationalNumber c)
{
   int nwd;
      if(b.d == c.d)
      {
          a->n = b.n - c.n;
          a->d = b.d;
          nwd = abs(gcd(a->n, a->d));
          a->n = a->n/nwd;
          a->d = a->d/nwd;

      }else
      {
          a->n = b.n*c.d - c.n*b.d;
          a->d = b.d * c.d;
          nwd = abs(gcd(a->n, a->d));
          a->n = a->n/nwd;
          a->d = a->d/nwd;

      }
}

rationalNumber minus( rationalNumber a)
{
    return a;
}

void Sum( rationalNumber *a, rationalNumber b, rationalNumber c)
{
    int nwd;
    if(b.d == c.d)
    {
        a->n = b.n + c.n;
        a->d = b.d;
    }else
    {
        a->n = b.n*c.d + c.n*b.d;
        a->d = b.d * c.d;
        nwd = abs(gcd(a->n, a->d));
        a->n = a->n/nwd;
        a->d = a->d/nwd;

    }

}

void Multiplication( rationalNumber *a, rationalNumber b, rationalNumber c)
{
    int nwd;

    a->n = b.n*c.n;
    a->d = b.d *c.d;

    nwd = abs(gcd(a->n, a->d));
    a->n = a->n/nwd;
    a->d = a->d/nwd;

}


void Reciprocal( rationalNumber *a)
{
    int buffor;

    if(a->n<0)
    {
        buffor = abs(a->n);
        a->n = -(a->d);
        a->d = buffor;
    }
    else if(a->n>0)
    {
        buffor = a->n;
        a->n = a->d;
        a->d = buffor;
    }


}

void load_point(point *a)
{
    input(&a->x);
    input(&a->y);

}

void output_point(point a)
{
    printf("(");
    output(a.x);
    printf(",");
    output(a.y);
    printf(")\n");
}

point C_1(rationalNumber *c_1, point a, point b)
{

    point c;

    Sum(c_1,b.x,a.y);

    Minus(c_1,*c_1, b.y);
    c.x = *c_1;

    Sum(c_1,b.y,b.x);

    Minus(c_1,*c_1, a.x);
    c.y = *c_1;

    return c;
}

point D_1(rationalNumber *d_1, point a, point b)
{
    point d;

    Sum(d_1,a.x,a.y);

    Minus(d_1,*d_1, b.y);
    d.x = *d_1;

    Sum(d_1,a.y,b.x);

    Minus(d_1,*d_1, a.x);
    d.y = *d_1;

    return d;
}

point C_2(rationalNumber *c_1,point a, point b)
{
    point c;

    Minus(c_1,b.x,a.y);

    Sum(c_1,*c_1, b.y);
    c.x = *c_1;

    Minus(c_1,b.y,b.x);

    Sum(c_1,*c_1, a.x);
    c.y = *c_1;

    return c;
}

point D_2(rationalNumber *d_1,point a, point b)
{
    point d;

    Minus(d_1,a.x,a.y);

    Sum(d_1,*d_1, b.y);
    d.x = *d_1;

    Minus(d_1,a.y,b.x);

    Sum(d_1,*d_1, a.x);
    d.y = *d_1;

    return d;
}

我不知道怎么解决

4

1 回答 1

0

如果正方形的一侧为(a, b),则可以立即将其两个法线中的一个设置为(b, -a)(或者(-b, a),您的问题不明确),最后一个点可以通过添加两个向量来获得,因此(a + b, b - a)(或(a - b, b + a),取决于您选择的向量第一的)

唯一可以将其传输到平面上任何一对点的方法是首先减去一个向量以移动图形原点....一旦问题解决,您可以将整个物体移回您所在的点有它。

所以,假设你有两个向量P = (a, b)Q = (c, d),第一个任务是减去P = (a, b)Q = (c, d)原点开始,然后你得到P'(a - a, b - b) = P'(0, 0); Q' = (c - a, d - b),现在您生成您的法线向量(您交换坐标并将符号更改为其中之一),假设R' = (-d + b, c - a),并将两个向量相加得到S' = (c - a - d + b, c - a + d - b),对角线相反。这是你得到的两点。让我们将整个事情移回原点:

P' = (0, 0) ==> P = (0 + a, 0 + b) = (a, b) = P
Q' = (c - a, d - b) ==> (c - a + a, d - b + b) = (c, d) = Q
R' = (b - d, c - a) ==> (b - d + a, c - a + b) = R
S' = (c - a - d + b, c - a + d - b) ==> (c - a - d + b + a, c - a + d - b + b) = (c + b - d, c - a + d) = S

所以,如果你得到两分P = (a, b)Q = (c, d)你必须返回R = (a + b - c, -a + b + c)and S = (b + c - d, -a + c + d)

double a, b, c, d;

scan("%g%g%g%g", &a, &b, &c, &d);
printf("Given (%g, %g) and (%g, %g),\n"
       "we got the other two vertices "
       "as (%g, %g) and (%g, %g)\n",
       a, b, c, d,
       a + b - c, b - a + c, 
       b + c - d, c - a + d);
于 2020-06-01T17:59:56.963 回答