#include<stdio.h> #include<math.h> #define N 10 void main() { int i,j=1,k=0,w=0,m; float p=0,y; FILE *read,*write; float pa[10],pb[10]; pa[0]=0,pb[0]=0; read=fopen("1.txt","r"); write=fopen("2.txt","w"); fprintf(write,"i/tpa/tpb/tk/t码字/n"); for(i=1;i<=N;i++) { fscanf(read,"%f/t",&pa[i]); p+=pa[i]; j++; if(p==1.0) break; } for(i=0;i<j;i++) { pb[i+1]=pb[i]+pa[i]; k=ceil(-log(pa[i])/log(2)); fprintf(write,"%d/t%1.3f/t%1.3f/t%u/t",i,pa[i],pb[i],k); y=pb[i]; for(m=0;m<k;m++) { y*=2; if(y>=1) { w=1; y=y-1; } else w=0; fprintf(write,"%d",w); } fprintf(write,"/n"); } }