Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- struct tuple {
- int tuple_count = 0;
- char * tuple_arr_type = new char[48];
- bool * tuple_bool = new bool[16];
- char * tuple_char = new char[16];
- int * tuple_int = new int[8];
- double * tuple_double = new double[4];
- long int * tuple_lint = new long int[4];
- };
- int count_element_char_arr (char * arr, char elem) {
- int count = 0;
- for (int i = 0; arr[i] != 0; i++) {
- if (arr[i] == elem) {
- count++;
- }
- }
- return count;
- }
- // Add function's
- void tuple_add(tuple & arr, bool elem) {
- int index = count_element_char_arr(arr.tuple_arr_type, 'b');
- arr.tuple_bool[index] = elem;
- arr.tuple_arr_type[arr.tuple_count] = 'b';
- arr.tuple_count++;
- }
- void tuple_add(tuple & arr, char elem) {
- int index = count_element_char_arr(arr.tuple_arr_type, 'c');
- arr.tuple_char[index] = elem;
- arr.tuple_arr_type[arr.tuple_count] = 'c';
- arr.tuple_count++;
- }
- void tuple_add(tuple & arr, int elem) {
- int index = count_element_char_arr(arr.tuple_arr_type, 'i');
- arr.tuple_int[index] = elem;
- arr.tuple_arr_type[arr.tuple_count] = 'i';
- arr.tuple_count++;
- }
- void tuple_add(tuple & arr, double elem) {
- int index = count_element_char_arr(arr.tuple_arr_type, 'd');
- arr.tuple_double[index] = elem;
- arr.tuple_arr_type[arr.tuple_count] = 'd';
- arr.tuple_count++;
- }
- void tuple_add(tuple & arr, long int elem) {
- int index = count_element_char_arr(arr.tuple_arr_type, 'l');
- arr.tuple_lint[index] = elem;
- arr.tuple_arr_type[arr.tuple_count] = 'l';
- arr.tuple_count++;
- }
- // Del function's (no tuple)
- void del_elem_arr(bool * arr, int size, int index) {
- for (int i = index; i < size - 1; i++) {
- arr[i] = arr[i + 1];
- }
- }
- void del_elem_arr(char * arr, int size, int index) {
- for (int i = index; i < size - 1; i++) {
- arr[i] = arr[i + 1];
- }
- }
- void del_elem_arr(int * arr, int size, int index) {
- for (int i = index; i < size - 1; i++) {
- arr[i] = arr[i + 1];
- }
- }
- void del_elem_arr(double * arr, int size, int index) {
- for (int i = index; i < size - 1; i++) {
- arr[i] = arr[i + 1];
- }
- }
- void del_elem_arr(long int * arr, int size, int index) {
- for (int i = index; i < size - 1; i++) {
- arr[i] = arr[i + 1];
- }
- }
- // Del function (tuple)
- void tuple_del(tuple & arr, int index) {
- if (index < arr.tuple_count && index > -1) {
- char type = arr.tuple_arr_type[index];
- int type_count = count_element_char_arr(arr.tuple_arr_type, type);
- int type_index = 0;
- for (int i = 0; i < index; i++) {
- if (arr.tuple_arr_type[i] == type) {
- type_index++;
- }
- }
- del_elem_arr(arr.tuple_arr_type, arr.tuple_count, index);
- arr.tuple_count--;
- if (type == 'b') {
- del_elem_arr(arr.tuple_bool, type_count, type_index);
- } else
- if (type == 'c') {
- del_elem_arr(arr.tuple_char, type_count, type_index);
- } else
- if (type == 'i') {
- del_elem_arr(arr.tuple_int, type_count, type_index);
- } else
- if (type == 'd') {
- del_elem_arr(arr.tuple_double, type_count, type_index);
- } else
- if (type == 'l') {
- del_elem_arr(arr.tuple_lint, type_count, type_index);
- }
- } else {
- std::cout << "\nERROR: invalid index\n";
- }
- }
- // Return function's
- bool tuple_return_bool(tuple & arr, int index) {
- char type = arr.tuple_arr_type[index];
- bool value = 0;
- int type_index = 0;
- for (int i = 0; i < index; i++) {
- if (arr.tuple_arr_type[i] == type) {
- type_index++;
- }
- }
- if (type == 'b') {
- value = arr.tuple_double[type_index];
- } else
- if (type == 'c') {
- value = arr.tuple_char[type_index];
- } else
- if (type == 'i') {
- value = arr.tuple_int[type_index];
- } else
- if (type == 'd') {
- value = arr.tuple_double[type_index];
- } else
- if (type == 'l') {
- value = arr.tuple_lint[type_index];
- }
- if (index >= arr.tuple_count || index < 0) {
- std::cout << "\nERROR: invalid index. Return value zero (false).\n";
- return 0;
- }
- return value;
- }
- char tuple_return_char(tuple & arr, int index) {
- char type = arr.tuple_arr_type[index];
- char value = 0;
- int type_index = 0;
- for (int i = 0; i < index; i++) {
- if (arr.tuple_arr_type[i] == type) {
- type_index++;
- }
- }
- if (type == 'b') {
- value = arr.tuple_double[type_index];
- } else
- if (type == 'c') {
- value = arr.tuple_char[type_index];
- } else
- if (type == 'i') {
- value = arr.tuple_int[type_index];
- } else
- if (type == 'd') {
- value = arr.tuple_double[type_index];
- } else
- if (type == 'l') {
- value = arr.tuple_lint[type_index];
- }
- if (index >= arr.tuple_count || index < 0) {
- std::cout << "\nERROR: invalid index. Return value zero (Null char).\n";
- return 0;
- }
- return value;
- }
- int tuple_return_int(tuple & arr, int index) {
- char type = arr.tuple_arr_type[index];
- int value = 0;
- int type_index = 0;
- for (int i = 0; i < index; i++) {
- if (arr.tuple_arr_type[i] == type) {
- type_index++;
- }
- }
- if (type == 'b') {
- value = arr.tuple_double[type_index];
- } else
- if (type == 'c') {
- value = arr.tuple_char[type_index];
- } else
- if (type == 'i') {
- value = arr.tuple_int[type_index];
- } else
- if (type == 'd') {
- value = arr.tuple_double[type_index];
- } else
- if (type == 'l') {
- std::cout << "\nERROR: long int size larger than int. Return value zero (0)\n";
- }
- if (index >= arr.tuple_count || index < 0) {
- std::cout << "\nERROR: invalid index. Return value zero (Null char).\n";
- return 0;
- }
- return value;
- }
- double tuple_return_double(tuple & arr, int index) {
- char type = arr.tuple_arr_type[index];
- double value = 0;
- int type_index = 0;
- for (int i = 0; i < index; i++) {
- if (arr.tuple_arr_type[i] == type) {
- type_index++;
- }
- }
- if (type == 'b') {
- value = arr.tuple_double[type_index];
- } else
- if (type == 'c') {
- value = arr.tuple_char[type_index];
- } else
- if (type == 'i') {
- value = arr.tuple_int[type_index];
- } else
- if (type == 'd') {
- value = arr.tuple_double[type_index];
- } else
- if (type == 'l') {
- value = arr.tuple_lint[type_index];
- }
- if (index >= arr.tuple_count || index < 0) {
- std::cout << "\nERROR: invalid index. Return value zero (0.0).\n";
- return 0;
- }
- return value;
- }
- long int tuple_return_long_int(tuple & arr, int index) {
- char type = arr.tuple_arr_type[index];
- double value = 0;
- int type_index = 0;
- for (int i = 0; i < index; i++) {
- if (arr.tuple_arr_type[i] == type) {
- type_index++;
- }
- }
- if (type == 'b') {
- value = arr.tuple_double[type_index];
- } else
- if (type == 'c') {
- value = arr.tuple_char[type_index];
- } else
- if (type == 'i') {
- value = arr.tuple_int[type_index];
- } else
- if (type == 'd') {
- value = arr.tuple_double[type_index];
- } else
- if (type == 'l') {
- value = arr.tuple_lint[type_index];
- }
- if (index >= arr.tuple_count || index < 0) {
- std::cout << "\nERROR: invalid index. Return value zero (0).\n";
- return 0;
- }
- return value;
- }
- void tuple_output(tuple & arr) {
- int cout_bool = 0;
- int cout_char = 0;
- int cout_int = 0;
- int cout_double = 0;
- int cout_lint = 0;
- std::cout << "\nOutput tuple (size: " << arr.tuple_count << "): {";
- for (int i = 0; arr.tuple_count > i; i++) {
- char type = arr.tuple_arr_type[i];
- if (type == 'b') {
- if (arr.tuple_bool[cout_bool] == 0) {
- std::cout << " False ";
- } else {
- std::cout << " True ";
- }
- cout_bool++;
- } else
- if (type == 'c') {
- std::cout << " '" << arr.tuple_char[cout_char] << "' ";
- cout_char++;
- } else
- if (type == 'i') {
- std::cout << " " << arr.tuple_int[cout_int] << " ";
- cout_int++;
- } else
- if (type == 'd') {
- std::cout << " " << arr.tuple_double[cout_double] << " ";
- cout_double++;
- } else
- if (type == 'l') {
- std::cout << " " << arr.tuple_lint[cout_lint] << " ";
- cout_lint++;
- }
- }
- std::cout << "}\n";
- }
- int main() {
- tuple tuple_test;
- for (int i = 0; 10 > i; i++) {
- static int k = 1;
- k *= 2;
- tuple_add(tuple_test, k);
- }
- tuple_output(tuple_test);
- for (int i = 0; 5 > i; i++) {
- tuple_del(tuple_test, 0);
- }
- tuple_output(tuple_test);
- for (int i = 0; tuple_test.tuple_count > i; i++) {
- static int k = 0;
- k += tuple_return_int(tuple_test, i);
- if (i == tuple_test.tuple_count - 1) {
- std::cout << "Summa: " << k << std::endl;
- }
- }
- int k = tuple_test.tuple_count;
- for (int i = 0; k > i; i++) {
- char symbol = (tuple_return_int(tuple_test, i) % 26) + 65;
- tuple_add(tuple_test, symbol);
- }
- tuple_output(tuple_test);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement