00 01 字符串如何读取?
我的一个文件中有00 01 10 11四种字符串,请问如何统计它们各自所占的比例?这里的关键问题是00 01的处理。
非常感谢!
int i=0, N_Number=0; double c, Cca[Dim]; int NOpen[Dim], iN; char c0[Dim][2], c1[Dim][2], c2[Dim][2], c3[Dim][2], b0[2], b1[2], b2[2], b3[2]; while( fscanf(fp, "%lf %d %s %s %s %s\n", &c, &iN, b0, b1, b2, b3)!=EOF ) { Cca[i] = c; NOpen[i] = iN; strcpy(c0[i], b0); strcpy(c1[i], b1); strcpy(c2[i], b2); strcpy(c3[i], b3); i++; N_Number++; }2.挑选需统计行到新文本(注意字符串的输出改为整形)
while (i<N_Number) { if ( (NOpen[i]==0) && (NOpen[i+1]>0) ) { fprintf(pn, "%lf %d %c%c %c%c %c%c %c%c\n", Cca[i], NOpen[i], c0[i][0],c0[i][1], c1[i][0],c1[i][1],c2[i][0],c2[i][1],c3[i][0],c3[i][1]); } i++; }3.字符统计
for (i=0; i<N_Number; i++) { if( (c0[i][0]=='0')&&(c0[i][1]=='0') ) a00++; if( (c0[i][0]=='0')&&(c0[i][1]=='1') ) a01++; if( (c0[i][0]=='1')&&(c0[i][1]=='0') ) a10++; if( (c0[i][0]=='1')&&(c0[i][1]=='1') ) a11++; if( (c1[i][0]=='0')&&(c1[i][1]=='0') ) a00++; if( (c1[i][0]=='0')&&(c1[i][1]=='1') ) a01++; if( (c1[i][0]=='1')&&(c1[i][1]=='0') ) a10++; if( (c1[i][0]=='1')&&(c1[i][1]=='1') ) a11++; if( (c2[i][0]=='0')&&(c2[i][1]=='0') ) a00++; if( (c2[i][0]=='0')&&(c2[i][1]=='1') ) a01++; if( (c2[i][0]=='1')&&(c2[i][1]=='0') ) a10++; if( (c2[i][0]=='1')&&(c2[i][1]=='1') ) a11++; if( (c3[i][0]=='0')&&(c3[i][1]=='0') ) a00++; if( (c3[i][0]=='0')&&(c3[i][1]=='1') ) a01++; if( (c3[i][0]=='1')&&(c3[i][1]=='0') ) a10++; if( (c3[i][0]=='1')&&(c3[i][1]=='1') ) a11++; fprintf(pn, "%lf %lf %lf %lf\n", a00/(double)4, a01/(double)4, a10/(double)4, a11/(double)4); a00=0, a01=0, a10=0, a11=0; }