Advertisement
Lavig

Другий семестр. Лабораторна робота №17 (Завдання 3)

May 6th, 2025 (edited)
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <set>
  4. #include <iomanip>
  5. #include <windows.h>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10.     SetConsoleOutputCP(1251);
  11.     SetConsoleCP(1251);
  12.     setlocale(LC_ALL, "");
  13.     string text{}, word{}, clean{}, prev_word{};
  14.     multiset<string> words{};
  15.     cout << "Введіть текст: ";
  16.     getline(cin, text);
  17.     istringstream stream(text);
  18.     while (stream >> word) {
  19.         clean = "";
  20.         for (char c : word) {
  21.             if (isalnum((unsigned char)c))
  22.                 clean += tolower((unsigned char)c);
  23.         }
  24.         if (!clean.empty()) {
  25.             words.insert(clean);
  26.         }
  27.     }
  28.     if (!words.empty()) {
  29.         cout << "Частота слів:" << endl;
  30.         int total = words.size();
  31.         int count = 0;
  32.         for (auto& w : words) {
  33.             if (w != prev_word) {
  34.                 if (count > 0) {
  35.                     double percent = (double)count / total * 100.0;
  36.                     cout << prev_word << ": " << count << " (" << fixed << setprecision(2) << percent << "%)" << endl;
  37.                 }
  38.                 prev_word = w;
  39.                 count = 1;
  40.             }
  41.             else {
  42.                 count++;
  43.             }
  44.         }
  45.         if (count > 0) {
  46.             double percent = (double)count / total * 100.0;
  47.             cout << prev_word << ": " << count << " (" << fixed << setprecision(2) << percent << "%)" << endl;
  48.         }
  49.     }
  50.     else {
  51.         cout << "У тексті немає слів для підрахунку." << endl;
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement