请教大师!如何用 scanf 正确输入数据?
用C语言求多条线段的交点坐标和交点个数,程序如下:#include<stdio.h>
#include<stdlib.h>
int between(float u,float v,float w)
{
float max,min;
max=(v>w)?v:w;
min=(v<w)?v:w;
return ((u<=max&&u>=min)?1:0);
}
void main()
{
int n,i,j,num;
float x1[100],y1[100],x2[100],y2[100],a[100],b[100],c[100],jx,jy;
while(scanf("%d",&n)&&n!=0)
{
num=0;
for(i=0;i<n;++i)
{
scanf("%f%f%f%f",&x1[i],&y1[i],&x2[i],&y2[i]);
if(x1[i]==x2[i])
{
a[i]=1;b[i]=0;c[i]=-x1[i];
}
else
{
a[i]=(y2[i]-y1[i])/(x1[i]-x2[i]);b[i]=1;
c[i]=(x1[i]*y2[i]-x2[i]*y1[i])/(x2[i]-x1[i]);
}
}
for(i=0;i<n;++i)
{
for(j=i+1;j<n;++j)
{
if(a[i]*b[j]-a[j]*b[i]!=0)
{
jx=(b[i]*c[j]-b[j]*c[i])/(a[i]*b[j]-a[j]*b[i]);
jy=(c[i]*a[j]-c[j]*a[i])/(a[i]*b[j]-a[j]*b[i]);
if(between(jx,x1[i],x2[i])&&between(jx,x1[j],x2[j])&&between(jy,y1[i],y2[i])&&between(jy,y1[j],y2[j]))
printf("%f,%f\n",jx,jy);
num++;
}
}
}
printf("%d\n",num);
}
return 0;
}
请教大师!我输入2条线段的坐标,线段(0,0),(3,3),线段(1,2),(5,1):
2
0,1,0,2,3,5,3,1
得到错误计算结果:
-107374176.000000,-107374176.000000
1
Press any key to continue
怎么才能用scanf正确输入坐标?