Advertisement
Guest User

Untitled

a guest
May 20th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <fstream>
  6.  
  7. bool isDigit(char a) {
  8. if (a >= 48 && a <= 57) {
  9. return true;
  10. }
  11. else {
  12. return false;
  13. }
  14. }
  15. int JapCompare(std::string str1, std::string str2) {
  16. //Tworzymy tablicy na ktorych bedzimy pracowac
  17. const char *s1 = str1.c_str();
  18. const char *s2 = str2.c_str();
  19. int index1 = 0;
  20. int index2 = 0;
  21. int log = 0;
  22. bool prev = false;
  23. int n1;
  24. int n2;
  25. while (true) {
  26. char e1 = NULL;
  27. char e2 = NULL;
  28. e1 = s1[index1];
  29. e2 = s2[index2];
  30. if (e1 == NULL) {
  31. if (e2 == NULL) {
  32. if (log == 1 ) {
  33. return -1;
  34. }
  35. else if(log == -1 ){
  36. return 1;
  37. }
  38. if (index1 >= index2) {
  39. return 1;
  40. }
  41. else {
  42. return -1;
  43. }
  44. }
  45. else {
  46. return -1;
  47. }
  48. }
  49. else if (e2 == NULL) {
  50. return 1;
  51. }
  52. //Porownujemy liczby
  53. if (isDigit(e1) && isDigit(e2)) {
  54. //Poczatek liczb
  55. int start1 = index1;
  56. int start2 = index2;
  57. //Dlugosc liczb
  58. n1 = 1;
  59. n2 = 1;
  60. while (s1[++index1] != NULL && isDigit(s1[index1])) {
  61. n1++;
  62. }
  63. while (s2[++index2] != NULL && isDigit(s2[index2])) {
  64. n2++;
  65. }
  66. //MAX
  67. int n;
  68. if (n1 > n2) {
  69. n = n1;
  70. }
  71. else {
  72. n = n2;
  73. }
  74. int d1 = n1 - n;
  75. int d2 = n2 - n;
  76. e1 = '0';
  77. e2 = '0';
  78. for (int i = 0; i < n; i++) {
  79. //Porownujemy
  80. if ((i + d1) >= 0) {
  81. e1 = s1[start1++];
  82. }
  83. if ((i + d2) >= 0) {
  84. e2 = s2[start2++];
  85. }
  86. if (e1 > e2) {
  87. return 1;
  88. }
  89. if (e1 < e2) {
  90. return -1;
  91. }
  92. }
  93. if (n1 < n2) {
  94. if (s1[index1] == NULL && s2[index2] == NULL) {
  95. if (prev == true) {
  96. if (log == 1) {
  97. return -1;
  98. }
  99. else if (log == -1 ){
  100. return 1;
  101. }
  102. }
  103. return 1;
  104. }
  105. if (s2[index2] == NULL) {
  106. return 1;
  107. }
  108. if (s1[index1] == NULL) {
  109. return -1;
  110. }
  111. if (prev == false) {
  112. log = -1;
  113. prev = true;
  114. }
  115. }
  116. if (n1 > n2) {
  117. if (s1[index1] == NULL && s2[index2] == NULL) {
  118. if (prev == true) {
  119. if (log == 1) {
  120. return -1;
  121. }
  122. else if(log == -1){
  123. return 1;
  124. }
  125. }
  126. return -1;
  127. }
  128. if (s2[index2] == NULL) {
  129. return 1;
  130. }
  131. if (s1[index1] == NULL) {
  132. return -1;
  133. }
  134. if (prev == false) {
  135. log = 1;
  136. prev = true;
  137. }
  138. }
  139.  
  140.  
  141. }
  142. else {
  143. if (e1 > e2) {
  144. return 1;
  145. }
  146. if (e1 < e2) {
  147. return -1;
  148. }
  149. index1++;
  150. index2++;
  151. }
  152. }//while
  153. }
  154. bool japLess(std::string str1, std::string str2) {
  155. if (JapCompare(str1, str2) == -1) {
  156. return true;
  157. }
  158. return false;
  159. }
  160. int main()
  161. {
  162. int count = 0;
  163. std::string k;
  164. std::vector<std::string> V;
  165. std::ifstream R("Test.txt");
  166. if (R.is_open()) {
  167. while (std::getline(R , k)) {
  168. if (k.empty()) {
  169. break;
  170. }
  171. V.push_back(k);
  172. count++;
  173. }
  174. }
  175. else {
  176. std::cout << "Unable open" << std::endl;
  177. }
  178. while (std::getline(std::cin, k)) {
  179. if (k.empty()) {
  180. break;
  181. }
  182. V.push_back(k);
  183. count++;
  184. }
  185.  
  186. std::sort(V.begin(), V.end(), japLess);
  187. //Printting
  188. for (int i = 0; i < count; i++) {
  189. std::cout << V[i] << std::endl;
  190. }
  191.  
  192.  
  193. return 0;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement