我的程序编译之后,没问题,就是要输入一个数据文件名,我建了一个txt的文件,输入之后还是错了,请大神指点
[color=#FF0000]# include"stdio.h"# include"malloc.h"
# include"math.h"
# include<conio.h>
void CGM(int n,float*w,float*h,float*x,float*y,float*d,float*x0,float*y0,float*h0);
void main()
{int n,i;
float*d,*x,*y,*w,*h,x0,y0,h0,x1,y1,h1;
FILE*fp;
char fn[80];
printf("输入数据文件名:");
scanf("%s",fn);
fp=fopen(fn,"r");
fscanf(fp,"%d,&n");
x=(float *)malloc (sizeof(float)*n);
y=(float *)malloc (sizeof(float)*n);
w=(float *)malloc (sizeof(float)*n);
d=(float *)malloc (sizeof(float)*n);
for(i=0;i<n;i++)
{fscanf(fp,"%f%f%f%f",&w[i],&h[i],&x[i],&y[i]);
}
CGM(n,w,h,x,y,d,&x0,&y0,&h0);
getch();
}[/color]
void CGM(int n,float*w,float*h,float*x,float*y,float*d,float*x0,float*y0,float*h0)
{float x1,y1;
float spx,spy,sa;
int i;
float hk_1,hk,dhx,dhy;
int f=0;
spx=0;spy=0;sa=0;
for(i=0;i<n;i++)
{spx+=h[i]*w[i]*x[i];
spy+=h[i]*w[i]*x[i];
sa+=h[i]*w[i];
}
x1=spx/sa;y1=spy/sa;
printf("XkYkDhxDhyHk");
while(1){
for(i=0;i<n;i++){
d[i]=float(sqrt((x1-x[i])*(x1-x[i])+(y1-y[i])*(y1-y[i])));
}
hk=0;
for(i=0;i<n;i++){
hk+=h[i]*w[i]*d[i];
}
dhx=0;dhy=0;
for(i=0;i<n;i++){
dhx+=h[i]*w[i]*(x1-x[i])/d[i];
dhy+=h[i]*w[i]*(y1-y[i])/d[i];
}
printf("xk=%8.4fyk=%8.4fdhx=%8.4fdhy=%8.4fhk=%8.4f\n",x1,y1,dhx,dhy,hk);
spx=0;spy=0;sa=0;
for(i=0;i<n;i++){
spx+=h[i]*w[i]*x[i]/d[i];
spy+=h[i]*w[i]*y[i]/d[i];
sa+=h[i]*w[i]/d[i];
}
x1=spx/sa;y1=spy/sa;
if(f==0){
f=1;
hk_1=hk;
}else{
if(hk_1<hk)break;
hk_1=hk;
}
}
}