SHARE
    TWEET
    Lavig

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

    May 6th, 2025 (edited)
    418
    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. using namespace std;
    7. int main() {
    8. SetConsoleOutputCP(1251);
    9. SetConsoleCP(1251);
    10. setlocale(LC_ALL, "");
    11. string text{}, word{}, clean{}, prev_word{};
    12. multiset<string> words{};
    13. cout << "Введіть текст: ";
    14. getline(cin, text);
    15. istringstream stream(text);
    16. while (stream >> word) {
    17. clean = "";
    18. for (char c : word) {
    19. if (isalnum((unsigned char)c))
    20. clean += tolower((unsigned char)c);
    21. }
    22. if (!clean.empty()) {
    23. words.insert(clean);
    24. }
    25. }
    26. if (!words.empty()) {
    27. cout << "Частота слів:" << endl;
    28. int total = words.size();
    29. int count = 0;
    30. for (auto& w : words) {
    31. if (w != prev_word) {
    32. if (count > 0) {
    33. double percent = (double)count / total * 100.0;
    34. cout << prev_word << ": " << count << " (" << fixed << setprecision(2) << percent << "%)" << endl;
    35. }
    36. prev_word = w;
    37. count = 1;
    38. }
    39. else {
    40. count++;
    41. }
    42. }
    43. if (count > 0) {
    44. double percent = (double)count / total * 100.0;
    45. cout << prev_word << ": " << count << " (" << fixed << setprecision(2) << percent << "%)" << endl;
    46. }
    47. }
    48. else {
    49. cout << "У тексті немає слів для підрахунку." << endl;
    50. }
    51. }
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /