Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <set>
- #include <iomanip>
- #include <windows.h>
- using namespace std;
- int main() {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- setlocale(LC_ALL, "");
- string text{}, word{}, clean{}, prev_word{};
- multiset<string> words{};
- cout << "Введіть текст: ";
- getline(cin, text);
- istringstream stream(text);
- while (stream >> word) {
- clean = "";
- for (char c : word) {
- if (isalnum((unsigned char)c))
- clean += tolower((unsigned char)c);
- }
- if (!clean.empty()) {
- words.insert(clean);
- }
- }
- if (!words.empty()) {
- cout << "Частота слів:" << endl;
- int total = words.size();
- int count = 0;
- for (auto& w : words) {
- if (w != prev_word) {
- if (count > 0) {
- double percent = (double)count / total * 100.0;
- cout << prev_word << ": " << count << " (" << fixed << setprecision(2) << percent << "%)" << endl;
- }
- prev_word = w;
- count = 1;
- }
- else {
- count++;
- }
- }
- if (count > 0) {
- double percent = (double)count / total * 100.0;
- cout << prev_word << ": " << count << " (" << fixed << setprecision(2) << percent << "%)" << endl;
- }
- }
- else {
- cout << "У тексті немає слів для підрахунку." << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement