Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. #include "pch.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <map>
  7. #include<mutex>
  8. #include <future>
  9. #include <unordered_map>
  10. #include <windows.h>
  11. using namespace std;
  12.  
  13. const string DIR = "C:/Users/aiden/Desktop/Word Frequency Analyzer/";
  14.  
  15. int main(void)
  16. {
  17. vector<string> fileName = { "input2.txt","input1.txt"};
  18. static vector<pair<int, string>> finalWordCount;
  19. static vector<thread> workers;
  20. mutex lock_finalWordCount;
  21. for (string& item : fileName)
  22. {
  23. std::string currentFile = DIR + item;
  24. cout << currentFile << endl;
  25. workers.push_back(thread([&currentFile, &lock_finalWordCount]{
  26. cout << "Running thread" <<"n";
  27. unordered_map<string, int>thisWordCount;
  28. ifstream filestream;
  29. string word;
  30. filestream.open(currentFile);
  31. if (!filestream) {
  32. cout << "Unable to open file" << endl;
  33. exit(1);
  34. }
  35. else if (filestream.is_open()) {
  36. while (filestream >> word)
  37. {
  38. ++thisWordCount[word];
  39. }
  40. }
  41. filestream.close();
  42. cout << "n" << "Locking reasource " << "n";
  43. lock_finalWordCount.lock();
  44. for (auto item : thisWordCount) {
  45. if (item.second > 1) { finalWordCount.push_back(pair<int,string>(item.second, item.first)); }
  46. }
  47. lock_finalWordCount.unlock();
  48. cout << "n" << "releasing reasource " << "n";//for debugging
  49. }));
  50.  
  51. }
  52.  
  53. for (auto& thread : workers) {
  54. thread.join();
  55. }
  56.  
  57. sort(finalWordCount.begin(), finalWordCount.end(), [](const pair<int, string> &a, const pair<int, string> &b){
  58. return (a.first > b.first);
  59. });
  60.  
  61. cout << flush;
  62. system("CLS");
  63. cout << "____________________" << endl;
  64. for (const auto &it : finalWordCount) {
  65. cout << it.first << "t" << it.second << "n";
  66. }
  67. cout << "____________________" << endl;
  68. return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement