fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define NOTA 1
  5. #define NOTB 2
  6. #define NOTC 4
  7.  
  8. #define AND 1
  9. #define OR 2
  10. #define NOT 3
  11. #define PORTA 4
  12. #define PORTB 5
  13. #define PORTC 6
  14.  
  15. char nametable[7][5] = { "NONE" , "AND" , "OR" , "NOT" , "A" , "B" , "C" };
  16. unsigned int three_notflag[3] = { 0 , 0 , 0 };
  17.  
  18. typedef unsigned char flag;
  19. typedef unsigned char bln;
  20. typedef unsigned char tt;
  21. typedef unsigned char gate;
  22.  
  23. const tt three_not[3] = { 0x0f , 0x33 , 0x55 };
  24.  
  25. /* possible truth table ( 3 input tables for 3 depth ) */
  26. struct ptt_t{
  27. flag possible;
  28. tt srca,srcb;
  29. gate type;
  30. };
  31.  
  32. typedef struct ptt_t ptt;
  33. ptt ptts[3*256];
  34.  
  35. int current_count[3] = { 0 , 0 , 0 };
  36. int current_depth = 0;
  37.  
  38. void dump_exit( void )
  39. {
  40. FILE *fp;
  41. int i;
  42.  
  43. printf( "It's found .\n" );
  44. fopen_s( &fp , "answer.txt" , "w" );
  45.  
  46. for( i = current_depth*256 ; i < current_depth*256+256 ; i++ ) {
  47. if( ptts[i].possible ) {
  48. fprintf( fp , "%02x:%s" ,
  49. (unsigned char)i , nametable[ptts[i].type] );
  50. if( AND <= ptts[i].type && ptts[i].type <= OR )
  51. fprintf( fp , "(%02x,%02x)\n" , ptts[i].srca , ptts[i].srcb );
  52. else if( ptts[i].type == NOT )
  53. fprintf( fp , "(%02x)\n" , ptts[i].srca );
  54. else
  55. fprintf( fp , "\n" );
  56. }
  57. }
  58. fclose( fp );
  59.  
  60. exit(1);
  61. }
  62.  
  63. void cp_ptts( int srcdepth )
  64. {
  65. int i,ix = srcdepth*256;
  66.  
  67. for( i = 0 ; i < 256 ; i++ ) {
  68. ptts[i+ix+256] = ptts[i+ix];
  69. }
  70. current_count[srcdepth+1] = current_count[srcdepth];
  71. three_notflag[srcdepth+1] = three_notflag[srcdepth];
  72. }
  73.  
  74. void apply_gate( tt a , tt b , gate tp )
  75. {
  76. tt y;
  77. int ix,i;
  78.  
  79. switch( tp ) {
  80. case AND: y = a & b; break;
  81. case OR: y = a | b; break;
  82. case NOT: y = ~a; break;
  83. }
  84.  
  85. ix = y+current_depth*256;
  86.  
  87. if( ptts[ix].possible == 0 ) {
  88. ptts[ix].possible = 1;
  89. ptts[ix].srca = a;
  90. ptts[ix].srcb = b;
  91. ptts[ix].type = tp;
  92. current_count[current_depth]++;
  93.  
  94. for( i = 0 ; i < 3 ; i++ ) {
  95. if( three_not[i] == y ) {
  96. three_notflag[current_depth] |= ( 1 << i );
  97. if( three_notflag[current_depth] == 7 )
  98. dump_exit();
  99. }
  100. }
  101. }
  102. }
  103.  
  104. void apply_gate2all_conbination( void )
  105. {
  106. tt a,b;
  107. int i;
  108. gate types[2] = { AND , OR };
  109.  
  110. for(;;) {
  111. int oldcount = current_count[current_depth];
  112.  
  113. for( i = 0 ; i < 2 ; i++ ) {
  114. for( a = 0 ; a < 255 ; a++ ) {
  115. if( ptts[current_depth*256+a].possible ) {
  116. for( b = a+1 ; b != 0 ; b++ ) {
  117. if( ptts[current_depth*256+b].possible )
  118. apply_gate( a , b , types[i] );
  119. }
  120. }
  121. }
  122. }
  123. if( oldcount == current_count[current_depth] ) break;
  124. }
  125. }
  126.  
  127. tt mtt( bln a000 , bln a001 , bln a010 , bln a011 , bln a100 , bln a101 , bln a110 , bln a111 )
  128. {
  129. unsigned char ret = a000;
  130. ret += ( a001 ? 2 : 0 );
  131. ret += ( a010 ? 4 : 0 );
  132. ret += ( a011 ? 8 : 0 );
  133. ret += ( a100 ? 16 : 0 );
  134. ret += ( a101 ? 32 : 0 );
  135. ret += ( a110 ? 64 : 0 );
  136. ret += ( a111 ? 128 : 0 );
  137.  
  138. return ret;
  139. }
  140.  
  141. void do_recursive( void )
  142. {
  143. int i = 0;
  144.  
  145. cp_ptts(current_depth);
  146. current_depth++;
  147.  
  148. while( i < 256 ) {
  149. if( ptts[i+current_depth*256].possible == 0 ) {
  150. i++;
  151. continue;
  152. }
  153. apply_gate( i , 0 , NOT );
  154. apply_gate2all_conbination();
  155.  
  156. printf( "%d:%d\n" , current_depth , current_count[current_depth] );
  157.  
  158. if( current_depth < 2 ) {
  159. do_recursive();
  160. }
  161. cp_ptts(current_depth-1);
  162. i++;
  163. }
  164.  
  165. current_depth--;
  166. }
  167.  
  168. int main( int argc , char *argv[] )
  169. {
  170. int i;
  171.  
  172. for( i = 0 ; i < 256 ; i++ ) {
  173. ptts[i].possible = 0;
  174. }
  175.  
  176. ptts[mtt(0,0,0,0,1,1,1,1)].possible = 1;
  177. ptts[mtt(0,0,0,0,1,1,1,1)].type = PORTA;
  178. ptts[mtt(0,0,1,1,0,0,1,1)].possible = 1;
  179. ptts[mtt(0,0,1,1,0,0,1,1)].type = PORTB;
  180. ptts[mtt(0,1,0,1,0,1,0,1)].possible = 1;
  181. ptts[mtt(0,1,0,1,0,1,0,1)].type = PORTC;
  182. current_count[0] = 3;
  183.  
  184. current_depth = 0;
  185. /* make level 1 */
  186. apply_gate2all_conbination();
  187. /* go */
  188. do_recursive();
  189.  
  190. return 0;
  191. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function ‘dump_exit’:
prog.c:44:2: warning: implicit declaration of function ‘fopen_s’; did you mean ‘fopen’? [-Wimplicit-function-declaration]
  fopen_s( &fp , "answer.txt" , "w" );
  ^~~~~~~
  fopen
prog.c: In function ‘apply_gate’:
prog.c:95:6: warning: ‘y’ may be used uninitialized in this function [-Wmaybe-uninitialized]
    if( three_not[i] == y ) {
      ^
/usr/bin/ld: /home/jBhcYQ/ccn5Njn1.o: in function `dump_exit':
prog.c:(.text+0x44): undefined reference to `fopen_s'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty