Advertisement
daniil_mironoff

Analog Tuple

Nov 15th, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.86 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. struct tuple {
  4.     int tuple_count = 0;
  5.     char * tuple_arr_type = new char[48];
  6.    
  7.     bool * tuple_bool = new bool[16];
  8.     char * tuple_char = new char[16];
  9.     int * tuple_int = new int[8];
  10.     double * tuple_double = new double[4];
  11.     long int * tuple_lint = new long int[4];
  12. };
  13.  
  14.  
  15. int count_element_char_arr (char * arr, char elem) {
  16.     int count = 0;
  17.     for (int i = 0; arr[i] != 0; i++) {
  18.         if (arr[i] == elem) {
  19.             count++;
  20.         }
  21.     }
  22.    
  23.     return count;
  24. }
  25.  
  26. // Add function's
  27. void tuple_add(tuple & arr, bool elem) {
  28.     int index = count_element_char_arr(arr.tuple_arr_type, 'b');
  29.    
  30.     arr.tuple_bool[index] = elem;
  31.     arr.tuple_arr_type[arr.tuple_count] = 'b';
  32.     arr.tuple_count++;
  33. }
  34.  
  35. void tuple_add(tuple & arr, char elem) {
  36.     int index = count_element_char_arr(arr.tuple_arr_type, 'c');
  37.    
  38.     arr.tuple_char[index] = elem;
  39.     arr.tuple_arr_type[arr.tuple_count] = 'c';
  40.     arr.tuple_count++;
  41. }
  42.  
  43. void tuple_add(tuple & arr, int elem) {
  44.     int index = count_element_char_arr(arr.tuple_arr_type, 'i');
  45.    
  46.     arr.tuple_int[index] = elem;
  47.     arr.tuple_arr_type[arr.tuple_count] = 'i';
  48.     arr.tuple_count++;
  49. }
  50.  
  51. void tuple_add(tuple & arr, double elem) {
  52.     int index = count_element_char_arr(arr.tuple_arr_type, 'd');
  53.    
  54.     arr.tuple_double[index] = elem;
  55.     arr.tuple_arr_type[arr.tuple_count] = 'd';
  56.     arr.tuple_count++;
  57. }
  58.  
  59. void tuple_add(tuple & arr, long int elem) {
  60.     int index = count_element_char_arr(arr.tuple_arr_type, 'l');
  61.    
  62.     arr.tuple_lint[index] = elem;
  63.     arr.tuple_arr_type[arr.tuple_count] = 'l';
  64.     arr.tuple_count++;
  65. }
  66.  
  67. // Del function's (no tuple)
  68. void del_elem_arr(bool * arr, int size, int index) {
  69.     for (int i = index; i < size - 1; i++) {
  70.         arr[i] = arr[i + 1];
  71.     }
  72. }
  73.  
  74. void del_elem_arr(char * arr, int size, int index) {
  75.     for (int i = index; i < size - 1; i++) {
  76.         arr[i] = arr[i + 1];
  77.     }
  78. }
  79.  
  80. void del_elem_arr(int * arr, int size, int index) {
  81.     for (int i = index; i < size - 1; i++) {
  82.         arr[i] = arr[i + 1];
  83.     }
  84. }
  85.  
  86. void del_elem_arr(double * arr, int size, int index) {
  87.     for (int i = index; i < size - 1; i++) {
  88.         arr[i] = arr[i + 1];
  89.     }
  90. }
  91.  
  92. void del_elem_arr(long int * arr, int size, int index) {
  93.     for (int i = index; i < size - 1; i++) {
  94.         arr[i] = arr[i + 1];
  95.     }
  96. }
  97.  
  98. // Del function (tuple)
  99. void tuple_del(tuple & arr, int index) {
  100.     if (index < arr.tuple_count && index > -1) {
  101.         char type = arr.tuple_arr_type[index];
  102.         int type_count = count_element_char_arr(arr.tuple_arr_type, type);
  103.        
  104.         int type_index = 0;
  105.         for (int i = 0; i < index; i++) {
  106.             if (arr.tuple_arr_type[i] == type) {
  107.                 type_index++;
  108.             }
  109.         }
  110.        
  111.         del_elem_arr(arr.tuple_arr_type, arr.tuple_count, index);
  112.         arr.tuple_count--;
  113.        
  114.         if (type == 'b') {
  115.             del_elem_arr(arr.tuple_bool, type_count, type_index);
  116.         } else
  117.         if (type == 'c') {
  118.             del_elem_arr(arr.tuple_char, type_count, type_index);
  119.         } else
  120.         if (type == 'i') {
  121.             del_elem_arr(arr.tuple_int, type_count, type_index);
  122.         } else
  123.         if (type == 'd') {
  124.             del_elem_arr(arr.tuple_double, type_count, type_index);
  125.         } else
  126.         if (type == 'l') {
  127.             del_elem_arr(arr.tuple_lint, type_count, type_index);
  128.         }
  129.     } else {
  130.         std::cout << "\nERROR: invalid index\n";
  131.     }
  132. }
  133.  
  134. // Return function's
  135. bool tuple_return_bool(tuple & arr, int index) {
  136.     char type = arr.tuple_arr_type[index];
  137.     bool value = 0;
  138.    
  139.     int type_index = 0;
  140.     for (int i = 0; i < index; i++) {
  141.         if (arr.tuple_arr_type[i] == type) {
  142.             type_index++;
  143.         }
  144.     }
  145.    
  146.     if (type == 'b') {
  147.         value = arr.tuple_double[type_index];
  148.     } else
  149.     if (type == 'c') {
  150.         value = arr.tuple_char[type_index];
  151.     } else
  152.     if (type == 'i') {
  153.         value = arr.tuple_int[type_index];
  154.     } else
  155.     if (type == 'd') {
  156.         value = arr.tuple_double[type_index];
  157.     } else
  158.     if (type == 'l') {
  159.         value = arr.tuple_lint[type_index];
  160.     }
  161.  
  162.     if (index >= arr.tuple_count || index < 0) {
  163.         std::cout << "\nERROR: invalid index. Return value zero (false).\n";
  164.         return 0;
  165.     }
  166.        
  167.     return value;
  168. }
  169.  
  170. char tuple_return_char(tuple & arr, int index) {
  171.     char type = arr.tuple_arr_type[index];
  172.     char value = 0;
  173.    
  174.     int type_index = 0;
  175.     for (int i = 0; i < index; i++) {
  176.         if (arr.tuple_arr_type[i] == type) {
  177.             type_index++;
  178.         }
  179.     }
  180.    
  181.     if (type == 'b') {
  182.         value = arr.tuple_double[type_index];
  183.     } else
  184.     if (type == 'c') {
  185.         value = arr.tuple_char[type_index];
  186.     } else
  187.     if (type == 'i') {
  188.         value = arr.tuple_int[type_index];
  189.     } else
  190.     if (type == 'd') {
  191.         value = arr.tuple_double[type_index];
  192.     } else
  193.     if (type == 'l') {
  194.         value = arr.tuple_lint[type_index];
  195.     }
  196.    
  197.     if (index >= arr.tuple_count || index < 0) {
  198.         std::cout << "\nERROR: invalid index. Return value zero (Null char).\n";
  199.         return 0;
  200.     }
  201.    
  202.     return value;
  203. }
  204.  
  205. int tuple_return_int(tuple & arr, int index) {
  206.     char type = arr.tuple_arr_type[index];
  207.     int value = 0;
  208.    
  209.     int type_index = 0;
  210.     for (int i = 0; i < index; i++) {
  211.         if (arr.tuple_arr_type[i] == type) {
  212.             type_index++;
  213.         }
  214.     }
  215.    
  216.     if (type == 'b') {
  217.         value = arr.tuple_double[type_index];
  218.     } else
  219.     if (type == 'c') {
  220.         value = arr.tuple_char[type_index];
  221.     } else
  222.     if (type == 'i') {
  223.         value = arr.tuple_int[type_index];
  224.     } else
  225.     if (type == 'd') {
  226.         value = arr.tuple_double[type_index];
  227.     } else
  228.     if (type == 'l') {
  229.         std::cout << "\nERROR: long int size larger than int. Return value zero (0)\n";
  230.     }
  231.    
  232.     if (index >= arr.tuple_count || index < 0) {
  233.         std::cout << "\nERROR: invalid index. Return value zero (Null char).\n";
  234.         return 0;
  235.     }
  236.    
  237.     return value;
  238. }
  239.  
  240. double tuple_return_double(tuple & arr, int index) {
  241.     char type = arr.tuple_arr_type[index];
  242.     double value = 0;
  243.    
  244.     int type_index = 0;
  245.     for (int i = 0; i < index; i++) {
  246.         if (arr.tuple_arr_type[i] == type) {
  247.             type_index++;
  248.         }
  249.     }
  250.    
  251.     if (type == 'b') {
  252.         value = arr.tuple_double[type_index];
  253.     } else
  254.     if (type == 'c') {
  255.         value = arr.tuple_char[type_index];
  256.     } else
  257.     if (type == 'i') {
  258.         value = arr.tuple_int[type_index];
  259.     } else
  260.     if (type == 'd') {
  261.         value = arr.tuple_double[type_index];
  262.     } else
  263.     if (type == 'l') {
  264.         value = arr.tuple_lint[type_index];
  265.     }
  266.    
  267.     if (index >= arr.tuple_count || index < 0) {
  268.         std::cout << "\nERROR: invalid index. Return value zero (0.0).\n";
  269.         return 0;
  270.     }
  271.    
  272.     return value;
  273. }
  274.  
  275. long int tuple_return_long_int(tuple & arr, int index) {
  276.     char type = arr.tuple_arr_type[index];
  277.     double value = 0;
  278.    
  279.     int type_index = 0;
  280.     for (int i = 0; i < index; i++) {
  281.         if (arr.tuple_arr_type[i] == type) {
  282.             type_index++;
  283.         }
  284.     }
  285.    
  286.     if (type == 'b') {
  287.         value = arr.tuple_double[type_index];
  288.     } else
  289.     if (type == 'c') {
  290.         value = arr.tuple_char[type_index];
  291.     } else
  292.     if (type == 'i') {
  293.         value = arr.tuple_int[type_index];
  294.     } else
  295.     if (type == 'd') {
  296.         value = arr.tuple_double[type_index];
  297.     } else
  298.     if (type == 'l') {
  299.         value = arr.tuple_lint[type_index];
  300.     }
  301.    
  302.     if (index >= arr.tuple_count || index < 0) {
  303.         std::cout << "\nERROR: invalid index. Return value zero (0).\n";
  304.         return 0;
  305.     }
  306.    
  307.     return value;
  308. }
  309.  
  310. void tuple_output(tuple & arr) {
  311.     int cout_bool = 0;
  312.     int cout_char = 0;
  313.     int cout_int = 0;
  314.     int cout_double = 0;
  315.     int cout_lint = 0;
  316.    
  317.     std::cout << "\nOutput tuple (size: " << arr.tuple_count << "): {";
  318.     for (int i = 0; arr.tuple_count > i; i++) {
  319.         char type = arr.tuple_arr_type[i];
  320.        
  321.         if (type == 'b') {
  322.             if (arr.tuple_bool[cout_bool] == 0) {
  323.                 std::cout << " False ";
  324.             } else {
  325.                 std::cout << " True ";
  326.             }
  327.            
  328.             cout_bool++;
  329.         } else
  330.         if (type == 'c') {
  331.             std::cout << " '" << arr.tuple_char[cout_char] << "' ";
  332.             cout_char++;
  333.         } else
  334.         if (type == 'i') {
  335.             std::cout << " " << arr.tuple_int[cout_int] << " ";
  336.             cout_int++;
  337.         } else
  338.         if (type == 'd') {
  339.             std::cout << " " << arr.tuple_double[cout_double] << " ";
  340.             cout_double++;
  341.         } else
  342.         if (type == 'l') {
  343.             std::cout << " " << arr.tuple_lint[cout_lint] << " ";
  344.             cout_lint++;
  345.         }
  346.     }
  347.     std::cout << "}\n";
  348. }
  349.  
  350. int main() {
  351.     tuple tuple_test;
  352.    
  353.     for (int i = 0; 10 > i; i++) {
  354.         static int k = 1;
  355.         k *= 2;
  356.        
  357.         tuple_add(tuple_test, k);
  358.     }
  359.    
  360.     tuple_output(tuple_test);
  361.    
  362.     for (int i = 0; 5 > i; i++) {
  363.         tuple_del(tuple_test, 0);
  364.     }
  365.    
  366.     tuple_output(tuple_test);
  367.    
  368.     for (int i = 0; tuple_test.tuple_count > i; i++) {
  369.         static int k = 0;
  370.         k += tuple_return_int(tuple_test, i);
  371.        
  372.         if (i == tuple_test.tuple_count - 1) {
  373.             std::cout << "Summa: " << k << std::endl;
  374.         }
  375.     }
  376.    
  377.     int k = tuple_test.tuple_count;
  378.     for (int i = 0; k > i; i++) {
  379.         char symbol = (tuple_return_int(tuple_test, i) % 26) + 65;
  380.         tuple_add(tuple_test, symbol);
  381.     }
  382.    
  383.     tuple_output(tuple_test);
  384.    
  385.     return 0;
  386. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement