我想写一个程序,你给正方形的两个顶点,然后程序找到另外两个。
例如:
输入:
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;
}
我不知道怎么解决