Advertisement
huutho_96

Bài Tập OOP tuần 5

May 3rd, 2015
517
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. //https://www.facebook.com/pages/C%C3%B9ng-h%E1%BB%8Dc-l%E1%BA%ADp-tr%C3%ACnh/632038696941833
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6. #include <fstream>
  7. using namespace std;
  8.  
  9. class MyClass
  10. {
  11. public:
  12. MyClass(char *path = "D:/text.txt");
  13. MyClass(string NoiDung)
  14. {
  15. this->NoiDung = NoiDung;
  16. XuLy();
  17. }
  18. ~MyClass();
  19. void DocFile()
  20. {
  21. string BienTam;
  22. NoiDung = "";
  23. while (!f.eof())
  24. {
  25. getline(f, BienTam);
  26. NoiDung += BienTam;
  27. }
  28. XuLy();
  29. }
  30. void TachTu()
  31. {
  32. string w = "";
  33. int len = NoiDung.size();
  34. int l = 0;
  35. while (l <= len)
  36. {
  37. if ((NoiDung[l] >= 'A' && NoiDung[l] <= 'Z') || (NoiDung[l] >= 'a' && NoiDung[l] <= 'z') || (NoiDung[l] >= '0' && NoiDung[l] <= '9') || (NoiDung[l] == '-'))
  38. {
  39. w += NoiDung[l];
  40. }
  41. else
  42. {
  43. if (w != "")
  44. {
  45. Tu.push_back(w);
  46. w = "";
  47. }
  48. }
  49. l++;
  50. }
  51. }
  52. void TachCau()
  53. {
  54. string s = "";
  55. int len = NoiDung.size();
  56. int l = 0;
  57. while (l < len)
  58. {
  59.  
  60. if (NoiDung[l] == '.' || NoiDung[l] == '!' || NoiDung[l] == '?')
  61. {
  62. Cau.push_back(s);
  63. s = "";
  64. }
  65. else
  66. s += NoiDung[l];
  67. l++;
  68. }
  69. }
  70. void XuLy()
  71. {
  72. TachCau();
  73. TachTu();
  74. }
  75. vector <string> TuXuatHienNhieuNhat(int &SoLanXuatHien)
  76. {
  77. vector <string> DanhSachTuXuatHienNhieuNhat;
  78. string w = "";
  79. int BienTam = 0;
  80. SoLanXuatHien = 0;
  81. sort(Tu.begin(), Tu.end());
  82. int len = Tu.size();
  83. for (int i = 1; i < len; i++)
  84. {
  85. if (Tu[i] == Tu[i - 1])
  86. BienTam++;
  87. else
  88. {
  89. if (BienTam >= SoLanXuatHien)
  90. {
  91. if (BienTam > SoLanXuatHien)
  92. {
  93. while (!DanhSachTuXuatHienNhieuNhat.empty())
  94. DanhSachTuXuatHienNhieuNhat.pop_back();
  95. }
  96. DanhSachTuXuatHienNhieuNhat.push_back(Tu[i - 1]);
  97. SoLanXuatHien = BienTam;
  98. }
  99. BienTam = 0;
  100. }
  101. }
  102. return DanhSachTuXuatHienNhieuNhat;
  103. }
  104. int LaySoTu()
  105. {
  106. return this->Tu.size();
  107. }
  108. int LaySoCau()
  109. {
  110. return this->Cau.size();
  111. }
  112. string LayNoiDungCau(int i)
  113. {
  114. if (i >= Cau.size()) return "";
  115. return Cau[i];
  116. }
  117. vector <string> SapXep()
  118. {
  119. sort(Tu.begin(), Tu.end());
  120. return this->Tu;
  121. }
  122. private:
  123. fstream f;
  124. string NoiDung;
  125. vector <string> Tu;
  126. vector <string> Cau;
  127. };
  128.  
  129. MyClass::MyClass(char *path)
  130. {
  131. f.open(path, ios::in);
  132. DocFile();
  133. }
  134.  
  135. MyClass::~MyClass()
  136. {
  137. f.close();
  138. }
  139.  
  140. void main()
  141. {
  142. int n;
  143. MyClass myclass;
  144. fstream f;
  145. f.open("D:/output.txt", ios::out);
  146. int SoCau = myclass.LaySoCau();
  147. f << SoCau << endl;
  148. for (int i = 0; i < SoCau; i++)
  149. {
  150. MyClass a(myclass.LayNoiDungCau(i));
  151. int SoTu, SoLanXuatHien;
  152. SoTu = a.LaySoTu();
  153. f << SoTu << endl;
  154. }
  155. vector <string> TuXuatHienNhieu = myclass.TuXuatHienNhieuNhat(n);
  156. while (!TuXuatHienNhieu.empty())
  157. {
  158. f << TuXuatHienNhieu[TuXuatHienNhieu.size() - 1] << " ";
  159. TuXuatHienNhieu.pop_back();
  160. }
  161. f << endl;
  162. for (int i = 0; i < SoCau; i++)
  163. {
  164. MyClass a(myclass.LayNoiDungCau(i));
  165. vector <string> DanhSachSapXep = a.SapXep();
  166. for (int i = 0; i < DanhSachSapXep.size(); i++)
  167. f << DanhSachSapXep[i] << " ";
  168. while (!DanhSachSapXep.empty()) DanhSachSapXep.pop_back();
  169. f << endl;
  170. }
  171. f.flush();
  172. f.close();
  173. system("D:/output.txt");
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement