Guest User

Untitled

a guest
Nov 23rd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. const int MAX_ARRAYS = 10;
  6. const int MAX_LENGTH = 128;
  7.  
  8. char** allocateMemory(int rows, int cols);
  9. void deallocateMemory(char** p, int rows);
  10. void assignData(char **destination, char **source, int rows);
  11.  
  12. int loadStrings(char** strings);
  13. void printMenu();
  14.  
  15. void print(char** strings, int loaded);
  16.  
  17. void asciiOrder(char** strings, int loaded);
  18. void lenOrder(char** strings, int loaded);
  19.  
  20. void baseOption(char** strings, int loaded);
  21. void asciiOption(char** strings, int loaded);
  22. void lenOption(char** strings, int loaded);
  23.  
  24. int main(int argc, char const *argv[])
  25. {
  26. char** strings = allocateMemory(MAX_ARRAYS, MAX_LENGTH);
  27. cout << "Wczytaj maks 10 lancuchow znakow, jesli chcesz zakonczyc wcisnij Ctrl-D" << '\n';
  28. int loaded = loadStrings(strings);
  29.  
  30. baseOption(strings, loaded);
  31. asciiOption(strings, loaded);
  32. lenOption(strings, loaded);
  33.  
  34. baseOption(strings, loaded);
  35.  
  36. deallocateMemory(strings, MAX_ARRAYS);
  37. return 0;
  38. }
  39.  
  40. int loadStrings(char** strings)
  41. {
  42. int i = 0;
  43. while(cin.getline(strings[i], MAX_LENGTH) && i < MAX_ARRAYS)
  44. {
  45. i++;
  46. }
  47.  
  48. return i;
  49. }
  50.  
  51. void printMenu()
  52. {
  53. cout << " ----------------------------- \n"
  54. << "1. Wyswietlenie pierwotnej listy lancuchow \n"
  55. << "2. Wyswietlenie lancuchow w porzadku ASCI \n"
  56. << "3. Wyswietlenie lancuchow wg dlugosci \n"
  57. << "4. Koniec \n "
  58. << "----------------------------- \n";
  59. }
  60.  
  61. void print(char** strings, int loaded)
  62. {
  63. for(int i = 0; i < loaded; i++)
  64. {
  65. cout << i + 1 << " | " << strings[i] << '\n';
  66. }
  67. }
  68.  
  69.  
  70. void asciiOrder(char** strings, int loaded)
  71. {
  72. char temp[MAX_LENGTH];
  73. for(int i = 0; i < loaded - 1; i++)
  74. {
  75. for(int j = 0; j < loaded - 1; j++)
  76. {
  77. if(strcmp(strings[j], strings[j +1]) > 0)
  78. {
  79. strcpy(temp, strings[j]);
  80. strcpy(strings[j], strings[j + 1]);
  81. strcpy(strings[j + 1], temp);
  82. }
  83. }
  84. }
  85. }
  86.  
  87.  
  88. void lenOrder(char** strings, int loaded)
  89. {
  90. char temp[MAX_LENGTH];
  91. for(int i = 0; i < loaded - 1; i++)
  92. {
  93. for(int j = 0; j < loaded - 1; j++)
  94. {
  95. if(strlen(strings[j]) < strlen(strings[j + 1]))
  96. {
  97. strcpy(temp, strings[j]);
  98. strcpy(strings[j], strings[j + 1]);
  99. strcpy(strings[j + 1], temp);
  100. }
  101. }
  102. }
  103. }
  104.  
  105. char** allocateMemory(int rows, int cols)
  106. {
  107. char **p = new char*[rows];
  108.  
  109. for(int i = 0; i < cols; i++)
  110. {
  111. p[i] = new char[cols];
  112. }
  113.  
  114. return p;
  115. }
  116.  
  117. void deallocateMemory(char** p, int rows)
  118. {
  119. for(int i = 0; i < rows; i++)
  120. {
  121. delete [] p[i];
  122. }
  123.  
  124. delete [] p;
  125. }
  126.  
  127. void assignData(char** destination, char **source, int rows)
  128. {
  129. for(int i = 0; i < rows; i++)
  130. {
  131. strcpy(destination[i], source[i]);
  132. }
  133. }
  134.  
  135. void baseOption(char** strings, int loaded)
  136. {
  137. cout << "### Pierwotna lista ### " << '\n';
  138. print(strings, loaded);
  139. }
  140.  
  141. void asciiOption(char** strings, int loaded)
  142. {
  143. char **p = allocateMemory(loaded, MAX_LENGTH);
  144. assignData(p, strings, loaded);
  145.  
  146. cout << "### Posortowana wg ASCI ###" << '\n';
  147. asciiOrder(p, loaded);
  148. print(p, loaded);
  149.  
  150. deallocateMemory(p, loaded);
  151. }
  152.  
  153. void lenOption(char** strings, int loaded)
  154. {
  155. char **p = allocateMemory(loaded, MAX_LENGTH);
  156. assignData(p, strings, loaded);
  157.  
  158. cout << "### Posortowana wg długości ###" << '\n';
  159. lenOrder(p, loaded);
  160. print(p, loaded);
  161.  
  162. deallocateMemory(p, loaded);
  163. }
Add Comment
Please, Sign In to add comment