| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5396 人关注过本帖
标题:关于文件数据处理
只看楼主 加入收藏
ton_well
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2015-10-8
收藏
得分:0 
回复 19楼 grmmylbs
万分感谢

新手报到
2016-03-28 17:08
ton_well
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2015-10-8
收藏
得分:0 
回复 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,我查了下说是结构体初始化的问题,但是改了好久,都为成功,希望您指点一下。谢谢了


新手报到
2016-04-29 11:04
ton_well
Rank: 1
等 级:新手上路
帖 子:23
专家分:0
注 册:2015-10-8
收藏
得分:0 
附件
dump2.zip (1.22 MB)

新手报到
2016-04-29 11:06
快速回复:关于文件数据处理
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018863 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved