以下是引用a13780393在2012-11-9 16:50:12的发言:
遇到回车往下接
帮你再改一个
程序代码:
#include <stdio.h>
#include <string.h>
#include <assert.h>
void dna8_val2str( unsigned long val, char str[8] )
{
assert( val < (1ul<<(8*2)) );
for( size_t i=0; i<8; ++i )
{
str[7-i] = "agct"[val%4];
val >>= 2;
}
}
int main()
{
unsigned long words = 0;
unsigned long numbers[65536] = { 0 };
// 处理
FILE* fin = fopen( "result1.txt", "r" );
if( !fin )
return 1;
int bav = 0;
unsigned long val = 0;
for( int c; c=fgetc(fin), c!=EOF; )
{
if( c == '@' ) // 遇到@则结束
break;
if( c == '\n' )
{
++words;
continue;
}
val = (val<<2)&0xFFFF;
switch( (char)c )
{
case 'a': val|=0; break;
case 'g': val|=1; break;
case 'c': val|=2; break;
case 't': val|=3; break;
default: // 出现了agct之外的字符
fclose( fin );
return 2;
}
if(bav<8)
++bav;
else
++numbers[val];
}
fclose( fin );
// 输出
FILE* fout = fopen( "result2.txt", "w" );
if( !fout )
return 3;
fprintf( fout, "The Number of total words are %ld\n", words );
fprintf( fout,"The Expect Number words are %f\n", words/65536.0 );
for( int i=0; i<65536; ++i )
{
if( numbers[i] !=0 )
{
char str[8];
dna8_val2str( i, str );
fprintf( fout, "%.8s\t%ld\t%f\n", str, numbers[i], numbers[i]/65536.0 );
}
}
fclose( fout );
return 0;
}
测试,result1.txt 内容如下
aaaaaaaaa
aaaaaaaag
aaaaaaaac
@
输出的 result2.txt 内容如下
The Number of total words are 3
The Expect Number words are 0.000046
aaaaaaaa
10
0.000153
aaaaaaag
1
0.000015
aaaaaaac
1
0.000015
aaaaaaga
1
0.000015
aaaaagaa
1
0.000015
aaaagaaa
1
0.000015
aaagaaaa
1
0.000015
aagaaaaa
1
0.000015
agaaaaaa
1
0.000015
gaaaaaaa
1
0.000015