decode16.c
Upload User: protel
Upload Date: 2021-02-26
Package Size: 1k
Code Size: 3k
Development Platform:

C/C++

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. char dic[40000][29];
  6. int count=255+4096; 
  7. int main(void)
  8. {
  9.    char s[40000],entry[40000],buf[40000];
  10.    char e[1],se[1],bf[3]; 
  11.    char a,b,c,d; 
  12.    int i,j,k,n,m,sum,total;
  13.    int rf,fcout=0,en=0,slen=0; 
  14.    
  15.    for(n=0;n<=count;n++) {
  16.       dic[n][0]=(char)n;
  17.       //printf("dic[%d][0]=%sn",n,dic[n]);   
  18.    }
  19.    
  20.    FILE *fp,*fw;
  21.    fp=fopen("encode16.txt","r");
  22.    //fp=fopen("test.txt","r");
  23.    //fw=fopen("decode.txt","w"); 
  24.    fw=fopen("decode2.txt","w"); 
  25.    if (fp==NULL) {
  26.        printf("open file error!n");              
  27.    } 
  28.    if (fw==NULL) {
  29.        printf("file not write!n");              
  30.    }  
  31.    while (fscanf(fp,"%c%c%c%c",&a,&b,&c,&d)!=EOF){ 
  32.          sum=hex(a,b,c,d);
  33.          //printf("sum=%dn",sum); 
  34.          
  35.          k=find(sum);
  36.          //printf("k=%dn",k); 
  37.          strcpy(entry,dic[k]);
  38.       
  39.          if(k==0){   //not find,entry==NULL 
  40.             strcpy(buf,s); 
  41.             slen=strlen(buf);
  42.             buf[slen++]=s[0];
  43.             //printf("buf*=%sn",buf); 
  44.             strcpy(entry,buf);  
  45.             for(i=0;i<=strlen(buf)+29;i++) {
  46.                buf[i]='';                    
  47.             } 
  48.          }
  49.          
  50.          printf("%s",entry);   //out entry 
  51.          fprintf(fw,"%s",entry); 
  52.          
  53.          if(s[0]!=''){       //if(s!=NULL) 
  54.             strcpy(buf,s);           
  55.             slen=strlen(buf);
  56.             buf[slen++]=entry[0]; 
  57.             //printf("buf=%sn",buf); 
  58.             strcpy(dic[++count],buf);
  59.             total+=1; 
  60.             for(j=0;j<=strlen(buf)+29;j++) {
  61.                buf[j]='';                    
  62.             } 
  63.             //printf("add dic[%d]=%sn",count,dic[count]);
  64.          }
  65.          
  66.          strcpy(s,entry);       //s=entry 
  67.          //printf("s=%sn",s); 
  68.          
  69.          for(m=0;m<=strlen(entry);m++){
  70.             entry[m]='';                                
  71.          } 
  72.          for(rf=0;rf<=2;rf++){              
  73.             bf[rf]='';                    
  74.          }
  75.          e[0]='';
  76.          se[0]=''; 
  77.    }
  78.    fclose(fp);
  79.    fclose(fw);
  80.    printf("ntotal=%dn",total); 
  81.    system("pause");
  82.    return 0;    
  83. }
  84. hex(int w,int x,int y,int z)
  85. {
  86.    int al;
  87.    if (w>=48 && w<=57) {
  88.         w=(w-48)* pow(16,3); 
  89.     }
  90.     if (w>=97 && w<=102) {
  91.         w=(w-87)* pow(16,3);   
  92.     }
  93.     //  f=(int)b;
  94.     if (x>=48 && x<=57) {
  95.         x=(x-48)*pow(16,2);    
  96.     }
  97.     if (x>=97 && x<=102) {
  98.         x=(x-87)*pow(16,2);   
  99.     }
  100.     //   g=(int)c;
  101.     if (y>=48 && y<=57) {
  102.         y=(y-48)*pow(16,1);    
  103.     }
  104.     if (y>=97 && y<=102) {
  105.         y=(y-87)*pow(16,1);   
  106.     }
  107.     //   h=(int)d;
  108.     if (z>=48 && z<=57) {
  109.         z=(z-48)*pow(16,0);    
  110.     }
  111.     if (z>=97 && z<=102) {
  112.         z=(z-87)*pow(16,0);   
  113.     }
  114.     al=w+x+y+z;
  115.     return al; 
  116. find(int *sm)
  117. {
  118.    int x=0; 
  119.    if(sm<=count){
  120.       x=sm;            
  121.    }
  122.    return x;