这是一个完整程序.语法没有错误,输入一个文件老是提示打不开.
单步运行,实际上执行到 if((fp=fopen(filename,"r+"))==NULL)这一步就没有执行.这是为什么呢.
fp指针为NULL.
不解,请指教!
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
void main(int argc,char *argv[])
{
float **y=NULL,**yy=NULL,**z=NULL;
char filename[20];
int num[3];
void readFile(float ***y,float ***yy,int num[3],char*);
void getZ(float ***y,float ***yy,float ***z,int n,int ynum,int s);
void output(float ***z,float ***yy,int n,int ynum,int s);
if(argc<=1)
{
printf("Please input the name of file:");
scanf("%s",filename) ;
}
else
strcpy(filename,argv[1]);
readFile(&y,&yy,num,filename);
getZ(&y,&yy,&z,num[0],num[2],num[1]);
output(&z,&yy,num[0],num[2],num[1]);
}
void readFile(float ***y,float ***yy,int num[3],char* filename)
{
FILE *fp;
int i,j,k,n,n2=0,prej=0;
float real,imag,m,**p,**Y,**YY;
int search(float **p,int k,int i,int len);
printf("\n");
if((fp=fopen(filename,"r+"))==NULL)
{
printf("Can't open the file\n");
exit(0);
}
fscanf(fp,"%d %d %d",&num[0],&n2,&num[1]);
p=(float**)malloc(n2*sizeof(float*));
for(i=0;i<n2&&!feof(fp);i++)
{
p[i]=(float*)malloc(4*sizeof(float));
fscanf(fp,"%f %f %f %f",&p[i][0],&p[i][1],&p[i][2],&p[i][3]);
}
num[2]=n2;
for(i=0;i<n2;i++)
{
if(p[i][0]==0||p[i][1]==0) num[2]--;
for(j=i+1;j<n2;j++)
{
if(p[j][0]==p[i][0]&&p[j][1]==p[i][1]||p[j][0]==p[i][1]&&p[j][1]==p[i][0])
{
m=(p[j][2]+p[i][2])*(p[j][2]+p[i][2])+(p[j][3]+p[i][3])*(p[j][3]+p[i][3]);
real=(p[j][2]*p[i][2]-p[j][3]*p[i][3])*(p[j][2]+p[i][2])+(p[j][2]*p[i][3]+p[j][3]*p[i][2])*(p[j][3]+p[i][3]);
imag=(p[j][2]*p[i][3]+p[j][3]*p[i][2])*(p[j][2]+p[i][2])-(p[j][2]*p[i][2]-p[j][3]*p[i][3])*(p[j][3]+p[i][3]);
p[i][2]=real/m; p[i][3]=imag/m;
p[j][0]=p[n2-1][0]; p[j][1]=p[n2-1][1];
p[j][2]=p[n2-1][2]; p[j][3]=p[n2-1][3];
n2--; num[2]--;
free(p[n2]);
}
}
}
fclose(fp);
Y=(float**)malloc(num[0]*sizeof(float*));
YY=(float**)malloc(num[2]*sizeof(float*));
j=0;
m=0;
system("cls");
printf("节点导纳矩阵为:\n");
for(i=1;i<=num[0];i++)
{
k=0; /*指针p位置*/
n=0;
Y[i-1]=(float*)malloc(2*sizeof(float));
Y[i-1][0]=0; Y[i-1][1]=0;
while((n2-k)>0)
{
if((n=search(p,k,i,n2))>=0)
{
real=p[n][2]/(p[n][2]*p[n][2]+p[n][3]*p[n][3]);
imag=-p[n][3]/(p[n][2]*p[n][2]+p[n][3]*p[n][3]);
Y[i-1][0]+=real; Y[i-1][1]+=imag;
if((m=p[n][0])>i||(m=p[n][1])>i)
{
YY[j]=(float*)malloc(4*sizeof(float));
YY[j][0]=(float)i-1; YY[j][1]=m-1; YY[j][2]=-real; YY[j][3]=-imag;
j++;
}
}
else break;
k=(n+1);
}
m=0;
printf("Y%d%d=%.4f",i,i,Y[i-1][0]);
if(Y[i-1][1]>=0)printf("+");
printf("%.4fj\t",Y[i-1][1]);
for(n=prej;n<j;n++)
{
m+=1;
if((int)m%3==0) printf("\n");
printf("Y%.0f%.0f=%.4f",YY[n][0]+1,YY[n][1]+1,YY[n][2]);
if(YY[n][3]>=0)printf("+");
printf("%.4fj\t",YY[n][3]);
}
prej=j;
printf("\n");
}
printf("\n");
num[2]=j;
*y=Y; *yy=YY;
for(i=0;i<n2;i++) free(p[i]);
free(p);
}
int search(float **pp,int k,int i,int len)
{
int j;
for(j=k;j<len;j++)
{
if(pp[j][0]==i||pp[j][1]==i) return j;
}
return -1;
}
void getZ(float ***Y,float ***YY,float ***Z,int n,int ynum,int s)
{
float **f,sumReal,sumIm;
float **y=*Y;
float **yy=*YY;
float **z=NULL;
int prek=0,i,j,k;
struct u
{
int i;
int j;
float real;
float im;
struct u *next;
struct u *pre;
} *head,*myu,*ui,*uj;
head=NULL;myu=NULL;ui=NULL;uj=NULL;
z=(float **)malloc(sizeof(float*)*n);
for(i=0;i<n;i++)
{
z[i]=(float*)malloc(sizeof(float)*2);
sumReal=0;sumIm=0;myu=head;
while(myu!=NULL&&myu->i<i)
{
if(myu->j==i)
{
sumReal+=(myu->real*myu->real-myu->im*myu->im)*y[myu->i][0]-2*myu->real*myu->im*y[myu->i][1];
sumIm+= (myu->real*myu->real-myu->im*myu->im)*y[myu->i][1]+2*myu->real*myu->im*y[myu->i][0];
}
myu=myu->next;
}
y[i][0]=y[i][0]-sumReal;
y[i][1]=y[i][1]-sumIm;
for(j=i+1;j<n;j++)
{
sumReal=0;sumIm=0;myu=head;ui=NULL;uj=NULL;
while(myu!=NULL&&myu->i<i)
{
if(myu->j==i) ui=myu;
else if(myu->j==j) uj=myu;
if(uj!=NULL&&ui!=NULL&&ui->i==uj->i)
if(y[ui->i][0]!=0||y[ui->i][1]!=0)
{
sumReal+=-(ui->real*uj->real-ui->im*uj->im)*y[ui->i][0]-(ui->im*uj->real+ui->real*uj->im)*y[ui->i][1];
sumIm+= - (ui->real*uj->real-ui->im*uj->im)*y[ui->i][1]+(ui->im*uj->real+ui->real*uj->im)*y[ui->i][0];
}
myu=myu->next;
}
for(k=prek;k<ynum&&yy[k][0]<=i;k++)
if(yy[k][0]==i&&yy[k][1]==j)
{sumReal+=yy[k][2];sumIm+=yy[k][3];break;}
prek=k;
if(sumReal!=0||sumIm!=0)
{
myu=(struct u*)malloc(sizeof(struct u));
myu->next=NULL;
myu->i=i;myu->j=j;
myu->real=(sumReal*y[i][0]+sumIm*y[i][1])/(y[i][0]*y[i][0]+y[i][1]*y[i][1]);
myu->im=(sumIm*y[i][0]-sumReal*y[i][1])/(y[i][0]*y[i][0]+y[i][1]*y[i][1]);
if(head==NULL)
{
head=myu;
myu->next=NULL;
myu->pre=myu;
}
else
{
head->pre->next=myu;
myu->pre=head->pre;
head->pre=myu;
}
}
}
}
if(head==NULL)
{
z[s][0]=y[s-1][0]/(y[s-1][0]*y[s-1][0]+y[s-1][1]*y[s-1][1]);
z[s-1][1]=-y[s-1][1]/(y[s-1][0]*y[s-1][0]+y[s-1][1]*y[s-1][1]);
}
else
{
f=(float**)malloc((n-s+1)*sizeof(float*));
f[0]=(float *)malloc(2*sizeof(float));
f[0][0]=1;
f[0][1]=0;
ui=head;while(ui!=NULL&&ui->i<s-1){ui=ui->next;}
for(i=1;i<=n-s;i++)
{
myu=ui;sumReal=0;sumIm=0;
f[i]=(float *)malloc(2*sizeof(float));
while(myu!=NULL&&myu->i<i+s-1)
{
if(myu->j==i+s-1)
{
sumReal+=myu->im*f[myu->i-s+1][1]-myu->real*f[myu->i-s+1][0];
sumIm +=-myu->real*f[myu->i-s+1][1]-myu->im*f[myu->i-s+1][0];
}
myu=myu->next;
}
f[i][0]=sumReal;f[i][1]=sumIm;
}
myu=head->pre; head->pre=NULL;
for(i=n-1;i>=0;i--)
{
sumReal=0;sumIm=0;
while(myu!=NULL&&myu->i==i)
{
if(myu->j>=i+1)
{
sumReal+=myu->real*z[myu->j][0]-myu->im*z[myu->j][1];
sumIm +=myu->real*z[myu->j][1]+myu->im*z[myu->j][0];
}
myu=myu->pre;
}
if(i<s-1){z[i][0]=-sumReal;z[i][1]=-sumIm;}
else
{
z[i][0]=(y[i][0]*f[i-s+1][0]+y[i][1]*f[i-s+1][1])/(y[i][0]*y[i][0]+y[i][1]*y[i][1])-sumReal;
z[i][1]=(y[i][0]*f[i-s+1][1]-y[i][1]*f[i-s+1][0])/(y[i][0]*y[i][0]+y[i][1]*y[i][1])-sumIm;
}
}
}
for(i=0;i<n;i++)free(y[i]);
free(y);
while(head!=NULL)
{
ui=head;
head=head->next;
free(ui);
}
*Z=z;
}
void output(float ***Z,float ***YY,int n,int ynum,int s)
{
float **V;
float **z=*Z;
float **yy=*YY;
float I_real=0,I_im=0;
float If[2];
int i=0,j=1;
V=(float **)malloc(sizeof(float*)*(n+1));
V[0]=(float*)malloc(sizeof(float)*2);
V[0][0]=0;V[0][1]=0;
If[0]=z[s-1][0] /(z[s-1][0]*z[s-1][0]+z[s-1][1]*z[s-1][1]);
If[1]=-z[s-1][1] /(z[s-1][0]*z[s-1][0]+z[s-1][1]*z[s-1][1]);
printf("故障点电流If=%.4f",If[0]);
if(If[1]>=0)printf("+");
printf("%.4fj\n\n各节点电压为:\n",If[1]);
for(i=1;i<=n;i++)
{
V[i]=(float*)malloc(sizeof(float)*2);
if(i==s){V[i][0]=V[i][1]=0;continue;}
V[i][0]=1-z[i-1][0]*If[0]+z[i-1][1]*If[1];
V[i][1]= -z[i-1][0]*If[1]-z[i-1][1]*If[0];
printf("V%d=%.4f",i,V[i][0]);
if(V[i][1]>=0)printf("+");
printf("%.4fj\t",V[i][1]);
if(j%3==0)printf("\n");
j++;
}
for(i=0;i<n;i++)free(z[i]);
free(z);
printf("\n\n各支路电流为:\n");
for(i=0;i<ynum;i++)
{
I_real=(V[(int)yy[i][0]+1][1]-V[(int)yy[i][1]+1][1])*yy[i][3]-(V[(int)yy[i][0]+1][0]-V[(int)yy[i][1]+1][0])*yy[i][2];
I_im=-(V[(int)yy[i][0]+1][1]-V[(int)yy[i][1]+1][1])*yy[i][2]-(V[(int)yy[i][0]+1][0]-V[(int)yy[i][1]+1][0])*yy[i][3];
printf("I%d%d=%.4f",(int)yy[i][0]+1,(int)yy[i][1]+1,I_real);
if(I_im>=0)printf("+");
printf("%.4fj\t",I_im);
if((i+1)%3==0)printf("\n");
free(yy[i]);
}
free(yy);
for(i=0;i<=n;i++)free(V[i]);
free(V);
}