//	This source and compression method apply to GNU General Public License. 
//			Copyright (C) 2010 and later by Jasenko Dzinleski 



//		This program is free software; you can redistribute it
//	and/or modify it under the terms of the GNU General Public License as
//	published by the Free Software Foundation; either version 2 of the
//	License, or (at your option) any later version. 

//	This program is distributed in the hope that it will be useful, but
//	WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//	General Public License for more details. 


//	You should have received a copy of the GNU General Public License along
//	with this program; if not, write to the Free Software Foundation, Inc.,
//	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.



//
//			binary factor number merit scale routine
//			written by Dzinleski Jasenko December,2010
//
//			to Maria Dzinleska





#include <stdio.h>
#include <string.h>
#include <math.h>

#define mult 1

FILE	*f1,*f2;

int 		ba16[16][16][16][16][2];	int	ba16i=0;

int		b12=((0xffff>>4 )<<4 );
int		b11=((0xffff>>5 )<<5 );
int		b10=((0xffff>>6 )<<6 );
int		b09=((0xffff>>7 )<<7 );
int		b08=((0xffff>>8 )<<8 );
int		b07=((0xffff>>9 )<<9 );
int		b06=((0xffff>>10)<<10);
int		b05=((0xffff>>11)<<11);
int		b04=((0xffff>>12)<<12);

char		p_s[256];

int p_format(double in_n,char fm[256],char arr0[256])
{

int		sig;
char		arr1[256];
double 		iv,fv;
__int64	lv,rv;
char		fa[256];int	fai=0;
int		i,j,k,l,m,n,e;


	if(in_n<0){sig=1;in_n*=-1.0;iv=in_n;}else{sig=0;iv=in_n;}

	for(i=0;i<256;++i){fa[i]='\0';}fai=0;
	for(j=0;j<256;++j){arr0[j]='\0';}

	for(i=1;i<12;++i)
	{
		lv=(long)iv/pow(10,i);//printf("%d %d %d\n",lv,i,(long)pow(10,i));
		if(lv==0){e=i-1;break;}
	}
	if(i==12){printf("Overflow...\n");arr0[0]='0';return(0);}

	if(e-1<0)
	{
		rv=(long)iv/pow(10,e);//printf("!%c",(char)(48+rv));
		fa[fai]=(char)(48+rv);++fai;

	}else{

	rv=(long)iv/pow(10,e);//printf("!%c",(char)(48+rv));
	fa[fai]=(char)(48+rv);++fai;
	rv*=pow(10,e);fv=(long)iv-rv;
	for(i=e-1;i>=0;--i)
	{
		lv=(long)fv/pow(10,i);//printf("!%c",(char)(48+lv));
		fa[fai]=(char)(48+lv);++fai;
		rv+=lv*pow(10,i);fv=iv;fv-=rv;
	}

	}

	//printf("%s\n",fa);

	if(strlen(fm)!=0)
	{

		m=0;l=strlen(fm);
		for(i=strlen(fm)-1;i>=0;--i)
		{if(fm[i]==','||fm[i]=='.'){if(l==strlen(fm)){l=i+1;++m;}else{++m;}}}
		k=(strlen(fm)-((strlen(fm)-l)+m))-strlen(fa);

		if(k>=0)
		{

			j=0;for(i=0;i<k;++i)
			{if(fm[i]=='.'||fm[i]==','){++k;}arr0[j]=fm[i];++j;}n=i;

			for(i=0;i<strlen(fa);++i)
			{
				if(fm[i+n]=='.'||fm[i+n]==','){arr0[j]=fm[i+n];++j;++n;}
				arr0[j]=fa[i];++j;
			}
				
			if(l!=strlen(fm))
			{
				--l;arr0[j]=fm[l];++j;
				for(i=l+1;i<strlen(fm);++i){arr0[j]=fm[i];++j;}

				iv=in_n;
				for(i=0;i<256;++i){fa[i]='\0';}fai=0;

				iv=pow(10,(strlen(fm)-(l+1)))*(iv-(double)((long)iv));

				for(i=1;i<12;++i)
				{
					lv=(long)iv/pow(10,i);//printf("%d %d %d\n",lv,i,(long)pow(10,i));
					if(lv==0){e=i-1;break;}
				}
				if(i==12){printf("Overflow...\n");arr0[0]='0';return(0);}

				if(e-1<0)
				{
					rv=(long)iv/pow(10,e);//printf("!%c",(char)(48+rv));
					fa[fai]=(char)(48+rv);++fai;

				}else{

				rv=(long)iv/pow(10,e);//printf("!%c",(char)(48+rv));
				fa[fai]=(char)(48+rv);++fai;
				rv*=pow(10,e);fv=(long)iv-rv;
				for(i=e-1;i>=0;--i)
				{
					lv=(long)fv/pow(10,i);//printf("!%c",(char)(48+lv));
					fa[fai]=(char)(48+lv);++fai;
					rv+=lv*pow(10,i);fv=iv;fv-=rv;
				}

				}

				//printf("s\n",fa);

				if((strlen(fm)-(l+1))==strlen(fa))
				{

				for(i=strlen(arr0)-1;i>=0;--i)
				{if(arr0[i]=='.'||arr0[i]==','){j=i;break;}}++j;
				for(i=0;i<strlen(fa);++i){arr0[j]=fa[i];++j;}

				}else{

					k=(strlen(fm)-(l+1))-strlen(fa);
					for(i=strlen(arr0)-1;i>=0;--i)
					{if(arr0[i]=='.'||arr0[i]==','){j=i;break;}}++j;
					for(i=0;i<k;++i){arr0[j]='0';++j;}
					for(i=0;i<strlen(fa);++i){arr0[j]=fa[i];++j;}

				}

			}

		}else{

			k=strlen(fa)-(strlen(fm)-((strlen(fm)-l)+m));
			j=0;for(i=0;i<k;++i){arr0[j]=fa[i];++j;}n=i;
			
			for(i=n;i<strlen(fa);++i)
			{
				if(fm[i-n]=='.'||fm[i-n]==','){arr0[j]=fm[i-n];++j;--n;}
				arr0[j]=fa[i];++j;
			}

			if(l!=strlen(fm))
			{
				--l;arr0[j]=fm[l];++j;
				for(i=l+1;i<strlen(fm);++i){arr0[j]=fm[i];++j;}

				iv=in_n;
				for(i=0;i<256;++i){fa[i]='\0';}fai=0;

				iv=pow(10,(strlen(fm)-(l+1)))*(iv-(double)((long)iv));

				for(i=1;i<12;++i)
				{
					lv=(long)iv/pow(10,i);//printf("%d %d %d\n",lv,i,(long)pow(10,i));
					if(lv==0){e=i-1;break;}
				}
				if(i==12){printf("Overflow...\n");arr0[0]='0';return(0);}

				if(e-1<0)
				{
					rv=(long)iv/pow(10,e);//printf("!%c",(char)(48+rv));
					fa[fai]=(char)(48+rv);++fai;

				}else{

				rv=(long)iv/pow(10,e);//printf("!%c",(char)(48+rv));
				fa[fai]=(char)(48+rv);++fai;
				rv*=pow(10,e);fv=(long)iv-rv;
				for(i=e-1;i>=0;--i)
				{
					lv=(long)fv/pow(10,i);//printf("!%c",(char)(48+lv));
					fa[fai]=(char)(48+lv);++fai;
					rv+=lv*pow(10,i);fv=iv;fv-=rv;
				}

				}

				//printf("s\n",fa);

				if((strlen(fm)-(l+1))==strlen(fa))
				{

				for(i=strlen(arr0)-1;i>=0;--i)
				{if(arr0[i]=='.'||arr0[i]==','){j=i;break;}}++j;
				for(i=0;i<strlen(fa);++i){arr0[j]=fa[i];++j;}

				}else{

					k=(strlen(fm)-(l+1))-strlen(fa);
					for(i=strlen(arr0)-1;i>=0;--i)
					{if(arr0[i]=='.'||arr0[i]==','){j=i;break;}}++j;
					for(i=0;i<k;++i){arr0[j]='0';++j;}
					for(i=0;i<strlen(fa);++i){arr0[j]=fa[i];++j;}

				}
				
			}

		}

	}else{

		j=0;for(i=0;i<strlen(fa);++i){arr0[j]=fa[i];++j;}

	}

	for(i=0;i<strlen(arr0);++i){if(arr0[i]=='#'){arr0[i]=' ';}}
	for(i=0;i<strlen(arr0)-1;++i){if((arr0[i]==' ')&&(arr0[i+1]==','||arr0[i+1]=='.')){arr0[i+1]=' ';}}

	if(sig==1)
	{
		for(i=0;i<256;++i){arr1[i]='\0';}
		j=0;arr1[j]='-';++j;
		for(i=0;i<strlen(arr0);++i){arr1[j]=arr0[i];++j;}
		for(i=0;i<256;++i){arr0[i]='\0';}
		j=0;for(i=0;i<strlen(arr1);++i){arr0[j]=arr1[i];++j;}
	}

	//printf("%s\n",arr0);



	return(0);
}

double p_val(char arr0[256])
{

	int	j=0,k=0,l;

	long		fr;
	int		rvi;
	long		rvl;
	__int64	rvd;
	double		rvd_;

	char 		arr1[256];

	for(j=0;j<256;++j){arr1[j]='\0';}
	l=0;for(j=0;j<strlen(arr0);++j){if((arr0[j]>='0'&&arr0[j]<='9')||(arr0[j]=='.'||arr0[j]==',')){arr1[l]=arr0[j];++l;}}

	for(j=0;j<256;++j){arr0[j]='\0';}l=0;for(j=0;j<strlen(arr1);++j){arr0[l]=arr1[j];++l;}

	for(j=0;j<strlen(arr0);++j){if(arr0[j]=='.'){break;}}

	if(j==strlen(arr0))
	{
		rvl=0;l=-1;
		for(j=-1+strlen(arr0);j>=0;--j)
		{
			++l;rvl+=(long)(-48+arr0[j])*pow(10,l);
		}
		rvi=(int)rvl;
		rvd_=(double)rvl;

	}else{

			fr=0;l=-1;
			for(k=-1+strlen(arr0);k>j;--k)
			{
				++l;fr+=(long)(-48+arr0[k])*pow(10,l);
			}

			rvd=0;l=-1;
			for(k=j-1;k>=0;--k)
			{
				++l;rvd+=(__int64)(-48+arr0[k])*pow(10,l);

			}rvd*=(__int64)pow(10,strlen(arr0)-j-1);

			rvd_=(double)(rvd+(__int64)fr)/(__int64)pow(10,strlen(arr0)-j-1);
				
	}

	return(rvd_);
}

int	p_1(char infn[255],char outfn[255])
{

	char	fld[50][256];
	int	fi=0;
	int	fl[50];

	int	fbyte;
	int	i,j,k,l,m,p;

	f1=fopen(infn,"rb");
	f2=fopen(outfn,"w");
	fbyte=getc(f1);
	while(!feof(f1))
	{
		fi=0;for(i=0;i<50;++i){fl[i]=0;for(j=0;j<256;++j){fld[i][j]='\0';}}
		while(!feof(f1)&&fbyte!=13&&fbyte!=10){if(fbyte==9){++fi;}else{fld[fi][fl[fi]]=fbyte;++fl[fi];}fbyte=getc(f1);}
		while(!feof(f1)&&fbyte==13||fbyte==10){fbyte=getc(f1);}
		//printf("%d\n",fi);
		if(fi==4){fprintf(f2,"%s\n",fld[3]);}
	}
	fclose(f1);

	return(0);

}

int main()
{

	char		fbyte;
	int		i,j,k,l,m,n,p,r,nH,nL;
	int		a,b,c,d,e;
	int		ai,gc1,gc2,gc3,gc4;
	__int64	lb,la;
	__int64	ga[4][2];
	char		nb[256];
	long		lv;



	for(i=0;i<16;++i){for(j=0;j<16;++j){for(k=0;k<16;++k){for(l=0;l<16;++l){
		ba16[i][j][k][l][0]=-1;ba16[i][j][k][l][1]=0;ba16[i][j][k][l][2]=0;
	}}}}ba16i=0;

	p_1("CL2001031616AA.CEL","n_.txt");
		
	f1=fopen("n_.txt","r");
	j=0;
	while(!feof(f1))
	{
		fscanf(f1,"%s\n",&nb);
		for(i=-1+strlen(nb);i>=0;--i){if(nb[i]=='.'){break;}}
		if(i==-1){i=strlen(nb);/*printf("Error...\n");return(0);*/}
		lv=0;l=-1+i;for(k=0;k<i;++k){lv+=(-48+(int)nb[k])*pow(10,l);--l;}
		lv*=mult;
		nH=(lv&0xffff0000)>>16;++j;
		nL=lv&0x0000ffff;++j;

		n=((nH<<8)&0xff00)|(nL&0x00ff);
		if(ba16[(n&0xf000)>>12][(n&0x0f00)>>8][(n&0x00f0)>>4][(n&0x000f)>>0][0]==-1)
		{
			ba16[(n&0xf000)>>12][(n&0x0f00)>>8][(n&0x00f0)>>4][(n&0x000f)>>0][0]=ba16i;
			ba16[(n&0xf000)>>12][(n&0x0f00)>>8][(n&0x00f0)>>4][(n&0x000f)>>0][1]=1;
			++ba16i;
		}else{
			++ba16[(n&0xf000)>>12][(n&0x0f00)>>8][(n&0x00f0)>>4][(n&0x000f)>>0][1];
		}
	}
	fclose(f1);

	for(i=0;i<16;++i){for(j=0;j<16;++j){for(k=0;k<16;++k){for(l=0;l<16;++l){
	
		if(ba16[i][j][k][l][0]!=-1)
		{
	
			ga[0][0]=0;ga[0][1]=0;for(c=0;c<16;++c){if(ba16[c][j][k][l][0]!=-1){++ga[0][1];n=(c<<12)|(j<<8)|(k<<4)|(l<<0);ga[0][0]+=n*ba16[c][j][k][l][1];}}
			ga[1][0]=0;ga[1][1]=0;for(c=0;c<16;++c){if(ba16[i][c][k][l][0]!=-1){++ga[1][1];n=(i<<12)|(c<<8)|(k<<4)|(l<<0);ga[1][0]+=n*ba16[i][c][k][l][1];}}
			ga[2][0]=0;ga[2][1]=0;for(c=0;c<16;++c){if(ba16[i][j][c][l][0]!=-1){++ga[2][1];n=(i<<12)|(j<<8)|(c<<4)|(l<<0);ga[2][0]+=n*ba16[i][j][c][l][1];}}
			ga[3][0]=0;ga[3][1]=0;for(c=0;c<16;++c){if(ba16[i][j][k][c][0]!=-1){++ga[3][1];n=(i<<12)|(j<<8)|(k<<4)|(c<<0);ga[3][0]+=n*ba16[i][j][k][c][1];}}
						
			b=-1;for(a=0;a<4;++a){if(b==-1){b=ga[a][0];ai=0;}else{if(b<ga[a][0]){b=ga[a][0];ai=a;}}}

			n=(i<<12)|(j<<8)|(k<<4)|(l<<0);
			if((ba16[i][j][k][l][1]*ga[ai][1]*n)>0&&ga[ai][0]>0)
			{

				p_format(((double)n/(double)8),"00000.00000",p_s);printf("%s",p_s);
				printf("%c",(char)9);
				p_format(
				log((double)ga[ai][0]/(double)(ba16[i][j][k][l][1]*ga[ai][1]*n)),"00000.00000",p_s);printf("%s",p_s);
				printf("%c%c",(char)13,(char)10);

			}
		}
	}}}}

	return(0);

}