Advertisement
Mike_be

PL3v21

Jun 6th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <vector>
  6. #include <regex>
  7. #include <streambuf>
  8. #include <Windows.h>
  9.  
  10. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); //Console address to change text color
  11.  
  12. /*
  13. Printing color messages in console
  14. Usage: print_str(sring_you_want_to_print, color_of_text, time_between_letters);
  15. Colors: https://i.stack.imgur.com/ZG625.png
  16. */
  17. inline bool print_str(std::string str, int k, int n)
  18. {
  19.     SetConsoleTextAttribute(hConsole, k);
  20.     for (int l = 0; l < int(str.size()); l++)
  21.     {
  22.         std::cout << str[l];
  23.         Sleep(n);
  24.     }
  25.     SetConsoleTextAttribute(hConsole, 14);
  26.     return true;
  27. }
  28.  
  29. /*
  30.  * Searches words for current assignment
  31.  */
  32. void word_search(std::string& input, std::vector<std::string>& vec)
  33. {
  34.     int count[32];
  35.     memset(count, 0, sizeof count);
  36.     std::string word = "";
  37.     bool err = false, correct = false;
  38.     for (unsigned int i = 0; i < input.length(); i++)
  39.     {
  40.         word.push_back(input[i]);
  41.         if (strchr("аоуыиеёэюяАОУЫИЕЁЭЮЯ", input[i]) && ++count[input[i] & 31] > 1)
  42.             correct = true;
  43.         if ((input[i] == ' ' || input[i] == '\n' || i+1 == input.length()) && correct)
  44.         {
  45.             if (!err)
  46.             {
  47.                 word.pop_back();
  48.                 vec.push_back(word);
  49.             }
  50.             memset(count, 0, sizeof count);
  51.             correct = false;
  52.             word = "";
  53.             err = false;
  54.         }
  55.         if (!(input[i] >= 'а' && input[i] <= 'я' || input[i] >= 'А' && input[i] <= 'Я'))
  56.             err = true;
  57.         if (input[i] == ' ' && !correct)
  58.         {
  59.             memset(count, 0, sizeof count);
  60.             correct = false;
  61.             word = "";
  62.             err = false;
  63.         }
  64.     }
  65. }
  66.  
  67. /*
  68. * Read function, reads from given path/.../file
  69. * Usage: read(vec_you_want_to_fill)
  70. * Now automatically reads everything and fills linked string to further work
  71. */
  72. void read(std::string& file)
  73. {
  74.     std::ifstream in;
  75.     std::string file_path;
  76.     print_str("Write file name or path to it: ", 10, 10);
  77.     do {
  78.         getline(std::cin, file_path);
  79.         if (file_path.length() > 256)
  80.         {
  81.             print_str("You wrote too long file path and i ", 4, 10);
  82.             file_path = "";
  83.         }
  84.         in.open(file_path);
  85.     } while (!in.is_open() && print_str("Couldn't read the file, type again: ", 4, 10));
  86.     file.assign((std::istreambuf_iterator<char>(in)),
  87.         std::istreambuf_iterator<char>());
  88.     in.close();
  89. }
  90.  
  91. int main()
  92. {
  93.     setlocale(LC_ALL, "Russian");
  94.     std::vector<std::string> words;
  95.     std::string file;
  96.     read(file);
  97.     print_str("Content in file: \"", 11, 5);
  98.     std::cout << file;
  99.     std::ofstream out("out.txt");
  100.     word_search(file, words);
  101.     print_str("\"\nCorrect words: {", 11, 5);
  102.     out << "Correct words: {";
  103.     for (int i = 0; i < words.size(); i++)
  104.     {
  105.         std::cout << ' ' << words[i]; out << ' ' << words[i] << ',';
  106.         print_str(",", 11, 5);
  107.     }
  108.     out << "}";
  109.     out.close();
  110.     print_str("\b }\n\nData was written in file in: \"C:\\Users\\Mike\\source\\repos\\ProgLang3 v21\\ProgLang3 v21\\ out.txt\"", 13, 5);
  111.     print_str("\nPress enter to continue. . .", 7, 5);
  112.     getchar();
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement