Advertisement
Piratux

Untitled

Feb 15th, 2022
1,251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. #include <cstdio>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. const int INPUT_LENGTH = 5;
  7. int input[(1 << INPUT_LENGTH)][INPUT_LENGTH];
  8. int output[INPUT_LENGTH];
  9. int test_output[(1 << INPUT_LENGTH)][INPUT_LENGTH];
  10.  
  11. struct State {
  12.     int next_state[2];
  13.     int output[2];
  14. };
  15.  
  16. const int MAX_STATES = 10;
  17. int combinations[MAX_STATES];
  18. State states[MAX_STATES];
  19. int curr_state = 0;
  20. int total_states = 0;
  21.  
  22. void print_automata_format_info() {
  23.     cout << "Automata info format:\n";
  24.     cout << "(state, input) -> (new_state, output)\n";
  25. }
  26.  
  27. void print_automata() {
  28.     // (state, input) -> (new_state, output)
  29.     cout << "States:\n";
  30.     for (int i = 0; i < total_states; i++) {
  31.         cout << "(" << i << ", 0) -> (" << states[i].next_state[0] << ", " << states[i].output[0] << ")\n";
  32.         cout << "(" << i << ", 1) -> (" << states[i].next_state[1] << ", " << states[i].output[1] << ")\n";
  33.     }
  34.     cout << '\n';
  35. }
  36.  
  37. void generate_input() {
  38.     for (int i = 0; i < (1 << INPUT_LENGTH); i++) {
  39.         int mask = 1 << (INPUT_LENGTH - 1);
  40.         for (int j = INPUT_LENGTH - 1; j >= 0; j--) {
  41.             input[i][j] = (mask & i) ? 1 : 0;
  42.             mask >>= 1;
  43.         }
  44.     }
  45. }
  46.  
  47. void generate_verified_output() {
  48.     for (int i = 0; i < (1 << INPUT_LENGTH); i++) {
  49.         for (int j = 0; j < INPUT_LENGTH; j++) {
  50.             if (j == 0 || j == 1) {
  51.                 test_output[i][j] == 0;
  52.                 continue;
  53.             }
  54.  
  55.             // do AND
  56.             if (input[j] == 0) {
  57.                 test_output[i][j] = input[i][j - 2] & input[i][j - 1];
  58.             }
  59.  
  60.             // do XOR
  61.             if (input[i][j] == 1) {
  62.                 test_output[i][j] = input[i][j - 2] ^ input[i][j - 1];
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. void generate_output(int idx) {
  69.     for (int j = 0; j < INPUT_LENGTH; j++) {
  70.         output[j] = states[curr_state].output[idx][input[j]];
  71.         curr_state = states[curr_state].next_state[idx][input[j]];
  72.     }
  73. }
  74.  
  75. bool verify_output(int idx) {
  76.     for (int j = 0; j < INPUT_LENGTH; j++) {
  77.         if (output[j] != test_output[idx][j]) {
  78.             return false;
  79.         }
  80.     }
  81.  
  82.     return true;
  83. }
  84.  
  85. void verify_automata() {
  86.     for (int i = 0; i < (1 << INPUT_LENGTH); i++) {
  87.         curr_state = 0;
  88.  
  89.         generate_output(i);
  90.  
  91.         if (verify_output(i) == false) {
  92.             return;
  93.         }
  94.     }
  95.  
  96.     cout << "Automata found\n";
  97.     print_automata();
  98. }
  99.  
  100. void try_all_outputs(int idx) {
  101.     if (total_states == idx) {
  102.         for (int i = 0; i < total_states; i++) {
  103.             states[i].output[0] = combinations[i] & 1;
  104.             states[i].output[1] = combinations[i] >> 1;
  105.         }
  106.  
  107.         verify_automata();
  108.         return;
  109.     }
  110.  
  111.     combinations[idx] = 0;
  112.     try_all_outputs(idx + 1);
  113.     combinations[idx] = 1;
  114.     try_all_outputs(idx + 1);
  115.     combinations[idx] = 2;
  116.     try_all_outputs(idx + 1);
  117.     combinations[idx] = 3;
  118.     try_all_outputs(idx + 1);
  119. }
  120.  
  121. void generate_automatas(int state_idx) {
  122.     if (state_idx == total_states) {
  123.         try_all_outputs(0);
  124.         return;
  125.     }
  126.  
  127.     for (int i = 0; i < total_states; i++) {
  128.         states[state_idx].next_state[0] = i;
  129.         for (int j = 0; j < total_states; j++) {
  130.             states[state_idx].next_state[1] = j;
  131.             generate_automatas(state_idx + 1);
  132.         }
  133.     }
  134. }
  135.  
  136. int main() {
  137.     generate_input();
  138.     generate_verified_output();
  139.  
  140.     print_automata_format_info();
  141.  
  142.     total_states = 4;
  143.  
  144.     generate_automatas(0);
  145. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement