rooq37

TABLICA CPP

Dec 14th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. ***Table.h***
  2.  
  3. #ifndef Table_h
  4. #define Table_h
  5. using namespace std;
  6. #include <string>
  7. #include <iostream>
  8. class Table {
  9. public:
  10. Table();
  11. ~Table();
  12. void setLength(int length);
  13. bool setValue(int offset, int value);
  14. void addValues(Table & other);
  15. void print();
  16. private:
  17. int * table;
  18. int size;
  19. };
  20. #endif
  21.  
  22. ***Table.cpp***
  23.  
  24. #include "Table.h"
  25.  
  26. Table::Table() {
  27. table = new int[10];
  28. for (int i = 0; i < 10; i++) {
  29. table[i] = NULL;
  30. }
  31. }
  32.  
  33. Table::~Table() {
  34. delete[] table;
  35. }
  36.  
  37. void Table::setLength(int length) {
  38. if (length < size) {
  39. int * newTab = new int[length];
  40. int i = 0;
  41. while (i < length) {
  42. newTab[i] = table[i];
  43. i++;
  44. }
  45. delete[] table;
  46. table = newTab;
  47. size = length;
  48. }
  49. else if (length > size) {
  50. int * newTab = new int[length];
  51. int i = 0;
  52. while (i < size) {
  53. newTab[i] = table[i];
  54. i++;
  55. }
  56. while (i < length) {
  57. newTab[i] = NULL;
  58. i++;
  59. }
  60. delete[] table;
  61. table = newTab;
  62. size = length;
  63. }
  64. }
  65.  
  66. bool Table::setValue(int offset, int value) {
  67. if (offset < size) {
  68. table[offset] = value;
  69. return true;
  70. }
  71. return false;
  72. }
  73.  
  74. void Table::addValues(Table & other) {
  75. int index = 0;
  76. setLength(size + other.size);
  77. for (int i = 0; i < size && index<other.size; i++) {
  78. if (table[i] == NULL) {
  79. table[i] = other.table[index];
  80. index++;
  81. }
  82. }
  83. }
  84.  
  85. void Table::print() {
  86. for (int i = 0; i < size; i++) {
  87. if (table[i] != NULL) {
  88. cout << table[i] << endl;
  89. }
  90. }
  91. }
  92.  
  93. ***Main.cpp***
  94.  
  95. #include "Table.h"
  96.  
  97. int main() {
  98. Table table_0, table_1;
  99. table_0.setLength(2);
  100. table_1.setLength(3);
  101. cout<<table_0.setValue(0, 3)<<endl;
  102. cout<<table_0.setValue(1, 4)<<endl;
  103. cout<<table_0.setValue(2, 9)<<endl;
  104.  
  105. cout << table_1.setValue(0, 5) << endl;
  106. cout << table_1.setValue(1, 6) << endl;
  107. cout << table_1.setValue(2, 7) << endl;
  108.  
  109. table_0.addValues(table_1);
  110.  
  111. table_0.print();
  112.  
  113. getchar();
  114.  
  115. }
Advertisement
Add Comment
Please, Sign In to add comment