24小时热门版块排行榜    

CyRhmU.jpeg
查看: 2364  |  回复: 6
本帖产生 2 个 程序强帖 ,点击这里进行查看
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

dubo

金虫 (著名写手)

优秀版主

★ ★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
余泽成(金币+3, 程序强帖+1): 谢谢参与应助! 2011-06-30 17:56:27
如果行数固定,建议用for()
你也可以仿照这个改改
CODE:
// reaMNdb.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include "iostream"
using namespace std;
#define natoms 200000
#define FILENAME1 water.gro
#define TRAJ_LEN 501
#define MN 12
#define cloumn_num  4
//=======define==========

struct grofile
{
        int totalatom;
        char title[100];
        int residuenr[natoms];
        char residuename[natoms][6];
        char atomname[natoms][6];
        int atomnr[natoms];
        float x[natoms],y[natoms],z[natoms],vx[natoms],vy[natoms],vz[natoms];
        float box_x,box_y,box_z;

};
struct pdbfile
{

        char title[100];
        char remark[1000];
        char cristal[1000];
        char mode_l[100];
        //int residuenr[natoms];
        char residuename[natoms][7];
        int atomnr[natoms];
        char atomname[natoms][5];
        char atomname2[natoms][4];
        int one[natoms];
        float x[natoms],y[natoms],z[natoms],dianhe[natoms],zero[natoms],vx[natoms],vy[natoms],vz[natoms];
        //        float box_x,box_y,box_z;
    char te_r[100];
        char endmd_l[100];
};
struct xvgfile
{
        float string[natoms][cloumn_num];
};
//=========struct============
//pdbfile oldfile,newfile;
xvgfile oldfile,newfile;
//grofile oldfile,newfile;
char *atom1="W\0 ";//the atom we will label
char *atom2="W\0";
float vdwd;//vandeva radio
bool label_water[natoms];//true overlapping
bool flag_vec=00;//judge if vec is effective
int ndx1[10000],ndx2[10000];//this is the index array for calc
float buffer[1000],buffer2[10000];//buffer store the distant average
int total_line=0;
int head_line=0;
int line;
//float work[100000][3];//temp_work array
//=========global val=========



void totalline(FILE *fp)
{
        char temp_str[500];
                //fgets(oldfile.title,100,fp);//title
        //int totalline=0;
                for (int i=0;1;i++)
                  { if(fgets(temp_str,1000,fp)==NULL)
                      break;
                          total_line++;
                   }
                       
                //return totalline;
       
}
void headline(FILE *fp)
{
        char temp_str[500];
        char a,b=' ';
        head_line=0;
                        //fgets(oldfile.title,100,fp);//title
                for (int i=0;1;i++)
                  {
                          fgets(temp_str,1000,fp);
                          a=temp_str[0];
                          //if(strcmp(a,b)==0)   
                          if(a-b==0)   
                      break;
                          
                          head_line++;
                   }
          //  return totalline;
       
}
void readxvg(FILE *fp)
{
            //int tt=oldfile.totalatom;
        int i=0;
        char temp_str[500];
            for (;i                 {   
                       
                        fgets(temp_str,1000,fp);
                }

                for (i=0;i                 {   
                        fscanf(fp,"%f%f",&oldfile.string[i][0],&oldfile.string[i][1]);
                }
               
        }
void xvg_out_file(FILE *fp)
{   
        for (int i=0;i     //fprintf(fp,"%8.5f %10.5f%15.8f\n",newfile.string[i][0],newfile.string[i][1],newfile.string[i][2]);
        fprintf(fp,"%15.5f%15.8f\n",newfile.string[i][0],newfile.string[i][2]);
}
void computer(FILE *fp){
             int i=0,j=0;
                float sum=0;
                 newfile=oldfile;
                for(;i                 {
                        sum=sum+newfile.string[i][1];
       }
                for(i=0;i                 {
                        newfile.string[i][2]=newfile.string[i][1]/sum;
                }


}
int main(int argc, char* argv[])
{  // read the gro
          char newfilename[100];
          char *filename;
          filename=newfilename;
          cout<<"input the input file name:\n";
          cin>>filename;
           FILE *fp,*fp_out;
                    fp=fopen(filename,"r");
                        totalline(fp);
                        rewind(fp);
                    headline(fp);
                        rewind(fp);
                        readxvg(fp);
                        fclose(fp);
                        //total_line+=0;
                        //head_line+=0;

                        computer(fp);
                        //newfile=oldfile;

                       
  //write the gro
         //  cout<<"input the output file name:\n";
                //cin>>filename;
                //fp_out=fopen(filename,"w");
                        fp_out=fopen("avg.xvg","a+");
                xvg_out_file(fp_out);
                fclose(fp_out);       

    return 0;
}

所读文件[ Last edited by dubo on 2011-6-29 at 23:23 ]
2楼2011-06-29 23:20:02
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

libralibra

至尊木虫 (著名写手)

骠骑将军

★ ★ ★ ★
小木虫(金币+0.5):给个红包,谢谢回帖
余泽成(金币+3, 程序强帖+1): 谢谢参与应助! 2011-06-30 17:56:59
多用内置库,fstream比fscanf简单些,好几个数组没必要,一个二维数组保存真正关心的数据就行了,冒号前的数就是下标+1
每行第一个都是1,没必要用数组保存了
gcc,vc6都调试通过了
CODE:
#include
#include
#include
using namespace std;

int main(int argc, char* argv[])
{
        double data[64][94]; // 数据 64*94
        int TR = 0; // 行数

        string sLine; // 保存读取到的每行字符串
        int i; // 循环下标
        int n; // 每行第一个1,扔掉没用
        int num; // 数字,1-94,做下标要减1
        char c; // 字符,接收冒号:,扔掉
        double fData; // 实际data,保存到数组

        ifstream in("bn_pn_train1.scale"); // 输入文件流

        while (getline(in,sLine))
        {
                istringstream is(sLine); // 从字符串构建输入字符串流

                is >> n; // 行首前导1,扔掉

                printf("%d",n);

                for (i=0; i<94; ++i)
                {
                        is >> num; // 得到序号
                        is >> c; // 得到冒号,扔掉
                        is >> fData; // 得到浮点数
                        data[TR][num-1] = fData; // 保存到数组

                        printf(" %d%c%g",num,c,fData);
                }

                printf("\n");

                TR++;
        }

        in.close(); // 关闭文件输入流

        printf("行数TR = %d\n",TR); //打印行数

        return 0;
}

结果

matlab/VB/python/c++/Java写程序请发QQ邮件:790404545@qq.com
4楼2011-06-30 04:58:33
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 dp.xiong 的主题更新
普通表情 高级回复(可上传附件)
信息提示
请填处理意见