AmateurKid

Untitled

Dec 25th, 2021
1,175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <fstream>
  2.  
  3. #include <iostream>
  4.  
  5. #include <clocale>
  6.  
  7. #include <Windows.h>
  8.  
  9. using namespace std;
  10.  
  11. int main() {
  12.  
  13. setlocale(LC_ALL, "Russian");
  14.  
  15. SetConsoleCP(1251);
  16.  
  17. SetConsoleOutputCP(1251);
  18.  
  19. char answ;
  20.  
  21. do {
  22.  
  23. char temp;
  24.  
  25. string word, word_check, direction;
  26.  
  27. char** chars = new char* [10];
  28.  
  29. for (int i = 0; i < 10; i++) {
  30.  
  31. chars[i] = new char[10];
  32.  
  33. }
  34.  
  35. ifstream file;
  36.  
  37. cout << "Enter your word >>";
  38.  
  39. cin >> word;
  40.  
  41. file.open("C:\\Users\\yarlo\\Desktop\\laba\\input.txt");
  42.  
  43. int g, d, i_coord, j_coord = 0;
  44.  
  45. int has_found = 0;
  46.  
  47. for (int i = 0; i < 10; i++) {
  48.  
  49. for (int j = 0; j < 10; j++) {
  50.  
  51. if (file.peek() != '\n')
  52.  
  53. {
  54.  
  55. file.get(temp);
  56.  
  57. chars[i][j] = temp;
  58.  
  59. }
  60.  
  61. else
  62.  
  63. {
  64.  
  65. file.ignore(1);
  66.  
  67. j--;
  68.  
  69. }
  70.  
  71. }
  72.  
  73. }
  74.  
  75. if (!file.is_open())
  76.  
  77. cout << "Error. Can't open your file" << endl;
  78.  
  79. for (int i = 0; i < 10; i++) {
  80.  
  81. for (int j = 0; j < 10; j++) {
  82.  
  83. if (chars[i][j] == word[0]) {
  84.  
  85. for (d = i; d > 0 && word_check.length() <= word.length() && has_found == 0; d--) {
  86.  
  87. word_check += chars[d][j];
  88.  
  89. if (word_check == word) {
  90.  
  91. has_found = 1;
  92.  
  93. i_coord = i;
  94.  
  95. j_coord = j;
  96.  
  97. direction = "up";
  98.  
  99. }
  100.  
  101. }
  102.  
  103. word_check.clear();
  104.  
  105. for (d = i; d < 10 && word_check.length() <= word.length() && has_found == 0; d++) {
  106.  
  107. word_check += chars[d][j];
  108.  
  109. if (word_check == word) {
  110.  
  111. has_found = 1;
  112.  
  113. i_coord = i;
  114.  
  115. j_coord = j;
  116.  
  117. direction = "down";
  118.  
  119. }
  120.  
  121. }
  122.  
  123. word_check.clear();
  124.  
  125. for (d = j; d < 10 && word_check.length() <= word.length() && has_found == 0; d++) {
  126.  
  127. word_check += chars[i][d];
  128.  
  129. if (word_check == word) {
  130.  
  131. has_found = 1;
  132.  
  133. i_coord = i;
  134.  
  135. j_coord = j;
  136.  
  137. direction = "right";
  138.  
  139. }
  140.  
  141. }
  142.  
  143. word_check.clear();
  144.  
  145. for (d = i; d > 0 && word_check.length() <= word.length() && has_found == 0; d--) {
  146.  
  147. word_check += chars[i][d];
  148.  
  149. if (word_check == word) {
  150.  
  151. has_found = 1;
  152.  
  153. i_coord = i;
  154.  
  155. j_coord = j;
  156.  
  157. direction = "left";
  158.  
  159. }
  160.  
  161. }
  162.  
  163. word_check.clear();
  164.  
  165. }
  166.  
  167. }
  168.  
  169. }
  170.  
  171. file.close();
  172.  
  173. ofstream file_output;
  174.  
  175. file_output.open("C:\\Users\\yarlo\\Desktop\\laba\\output.txt");
  176.  
  177. if (has_found == 1) {
  178.  
  179. file_output << "\nWord has been found";
  180.  
  181. file_output << "\nCoords>>" << i_coord + 1 << ' ' << j_coord + 1;
  182.  
  183. file_output << "\nDirection>>" << direction;
  184.  
  185. }
  186.  
  187. else {
  188.  
  189. file_output << "\nWord has not been found";
  190.  
  191. }
  192.  
  193. for (int i = 0; i < 10; i++) {
  194.  
  195. delete[] chars[i];
  196.  
  197. }
  198.  
  199. delete[] chars;
  200.  
  201. file_output.close();
  202.  
  203. cout << "\nDo you want to repeat?[y/n]>> ";
  204.  
  205. cin >> answ;
  206.  
  207. while (cin.fail() || cin.peek() != '\n' || (answ != 'y' && answ != 'Y' && answ != 'n' && answ != 'N')) {
  208.  
  209. cout << "Invalid answer";
  210.  
  211. cin.clear();
  212.  
  213. cin.ignore(10000, '\n');
  214.  
  215. cout << "\nDo you want to repeat?[y/n]>> ";
  216.  
  217. cin >> answ;
  218.  
  219. }
  220.  
  221. } while (answ == 'y' || answ == 'Y');
  222.  
  223. return 0;
  224.  
  225. }
Advertisement
Add Comment
Please, Sign In to add comment