UVA 213 By sixleaves

UVA 213 By sixleaves
 1  #include  < stdio.h >
 2  #include  < string .h >
 3 
 4 
 5  int  readChar();
 6  int  readInt( int  c);
 7  int  readCodes();
 8 
 9  /*
10   1.读取字符时候如何过滤掉换行符号,兼容类unix和windows操作系统 readChar()
11   2.熟练掌握将二进制字符串映射到数组的数据结构,这个数据结构是个二元组,我们可以用该二元组唯一确定一个长度为len的二进制字符串
12   (len, value) ---- > code[len][value]
13   
14    */
15 
16  int  main() {
17 
18      
19       while  ( readCodes() ) {  //  读编码头
20          
21           for  (;;) {   //  读信息
22              
23               int  len  =  readInt( 3 );
24               if  ( 0   ==  len)  break //  全0该信息结束
25              
26               for  (;;) {   //  读信息段
27                  
28                   int  v  =  readInt(len);
29                  
30                   if  (v  ==  ( 1   <<  len)  -   1 break //  全1一个信息段结束
31                  putchar(code[len][v]);
32                  
33              }
34              
35          }
36          
37          putchar( ' \n ' );
38      }
39  }
40 
41 
42  int  readInt( int  l) {
43      
44       int  v  =   0 ;
45       // 已经读取了3 - l个字符
46       while  (l -- ) {
47          
48          v  =  v  *   2   +  readChar()  -   ' 0 ' ;
49          
50      }
51       return  v;
52      
53  }
54 
55 
56  char  readChar() {
57      
58       char  ch;
59       do  {
60          
61          ch  =  getchar();
62          
63      } while  (  ' \n '   !=  ch  ||   ' \r '   !=  ch )
64       return  ch;
65  }
66 
67 
68  int  readCodes() {
69      
70      
71      memset(code,  0 sizeof (code));
72      
73       //  因为可能读取编码头独占一行,所以我们可能读取编码头时候会读取到上一次的回车换行,所以要使用readChar函数
74      code[ 1 ][ 0 =  readChar();
75      
76       for  ( int  len  =   2 ; len  <   8 ; len ++ ) {
77          
78           for  ( int  v  =   0 ; v  <  ( 1   <<  len)  -   1 ; v ++ ) {
79              
80               char  ch  =  getchar();
81               if  ( EOF  ==  ch )  return   0 ;
82               if  (  ' \n '   ==  ch  ||   ' \r '   ==  ch)  return   1 ;
83              code[len][v]  =  ch;
84              
85          }
86          
87      }
88       return   1 ;
89      
90      
91  }

你可能感兴趣的:(UVA 213 By sixleaves)