回复 19楼 grmmylbs
您好,版主,不好意思又来麻烦您了。又遇到些问题,希望您能帮忙看下。我在您上次给的指点上修改了下程序:
#include<stdio.h>
#include<stdlib.h>
FILE *pfile;
typedef struct
{
int id;
int type;
float x;
float y;
float z;
} Data;
typedef struct
{
Data element[100] ;
int length;
} Chains ;
int main()
{
Data tmp[50]; //定义一个临时变量,存储每一行数据
Data *result ; //这个数组长度要与文件行数相一致,不能小于
Chains *now[100]={NULL} ;
int n, i = 1, count , k , total , a , c; float S = 0, m, x1, y1, z1, sum ;
int chains,ret;
FILE *infile;
if ((now[100]=(Chains *)malloc(sizeof(Chains)))==NULL)
{printf("Assignment of space for chain is failed!/n"); exit(0);}
infile = fopen("dump2.lammpstrj", "r");
if (infile == NULL)
{
printf("\nFailed to open the file");
exit(1);
}
result = malloc(10000 * sizeof(Data));
//注意分配内存时 数字的大小对应ID大小
for (i = 0; i < 9; i++)
{
ret = fgets((char *)tmp, sizeof(tmp), infile); //跳过文字行
}
while (ret)
{
memset((void *)result,0, 1800 * sizeof(Data));
i = 1;
while (fgets((char *)tmp, sizeof(tmp), infile)) //读每一行
{
sscanf((char *)tmp, "%d %d %f %f %f", &(result[i].id), &(result[i].type), &(result[i].x), &(result[i].y), &(result[i].z)); //放到数组中
printf("%d %d
%d
%f
%f
%f\n", i, result[i].id, result[i].type, result[i].x, result[i].y, result[i].z); //测试显示到屏幕上
if (result[i].id == 6157)
{
float r , r0; int n ,count =0 ;
for( r0 = 1.0 ; r0<=50.0 ; r0 =r0 + 0.5)
{
r = r0 - 0.5;
for ( n = 1 ; n <= 8000 ;
n++
)
{
i = 1 , k = 1 ;
if (
result[n].z <= r0 &&
result[n].z >= r)
{
now [k]-> element [i] = result [n] ;//
if( i != n)
{
now [k]-> length = i;
k++;
i = 0 ;
}
i++;
}
}
total = k ;
k = 1; i = 0; count = 0 ; chains = 1; sum = 0.0 ;//计算链取向率,根据分子链长度设定N,n,最后要除以K=N*(n-2)。如下几个关键字的设定
for ( k = 1 ; k <= total ; k++)
{
for (i = 1; (i + 2) <= now[k]->length; i++)
{
x1 = (now[k+2]->element->x - now[k+2]->element->x);
y1 = (now[k+2]->element->y - now[k+2]->element->y);
z1 = (now[k+2]->element->z - now[k+2]->element->z);
m = y1*y1 / (x1*x1 + y1*y1 + z1*z1);
S = S + (3 * m - 1) / 2;
printf("%f\n", S);
//过程没问题
}
k++;
sum = S + sum;
}
//
printf("%d\n", chains);
//检查链与主链骨架数是否正确
if((pfile=fopen("output.data","a"))==NULL)
printf("lFile could not be opened.\n");
else
{
fprintf(pfile,"%f\n", sum / (total*(n - 2)) );
}
fclose(pfile);
for (i = 0; i < 9; i++)
{
ret = fgets((char *)tmp, sizeof(tmp), infile); //跳过非数值行
}
break;
}
}
i++;
}
//fclose(infile);
}
free(result);
fclose(infile);
system("pause");
return 0;
}
运行的时候上面的程序就会在上面红线的地方报错:CXX0030 expression cannot be evaluated,我查了下说是结构体初始化的问题,但是改了好久,都为成功,希望您指点一下。谢谢了