Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://www.facebook.com/pages/C%C3%B9ng-h%E1%BB%8Dc-l%E1%BA%ADp-tr%C3%ACnh/632038696941833
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <fstream>
- using namespace std;
- class MyClass
- {
- public:
- MyClass(char *path = "D:/text.txt");
- MyClass(string NoiDung)
- {
- this->NoiDung = NoiDung;
- XuLy();
- }
- ~MyClass();
- void DocFile()
- {
- string BienTam;
- NoiDung = "";
- while (!f.eof())
- {
- getline(f, BienTam);
- NoiDung += BienTam;
- }
- XuLy();
- }
- void TachTu()
- {
- string w = "";
- int len = NoiDung.size();
- int l = 0;
- while (l <= len)
- {
- if ((NoiDung[l] >= 'A' && NoiDung[l] <= 'Z') || (NoiDung[l] >= 'a' && NoiDung[l] <= 'z') || (NoiDung[l] >= '0' && NoiDung[l] <= '9') || (NoiDung[l] == '-'))
- {
- w += NoiDung[l];
- }
- else
- {
- if (w != "")
- {
- Tu.push_back(w);
- w = "";
- }
- }
- l++;
- }
- }
- void TachCau()
- {
- string s = "";
- int len = NoiDung.size();
- int l = 0;
- while (l < len)
- {
- if (NoiDung[l] == '.' || NoiDung[l] == '!' || NoiDung[l] == '?')
- {
- Cau.push_back(s);
- s = "";
- }
- else
- s += NoiDung[l];
- l++;
- }
- }
- void XuLy()
- {
- TachCau();
- TachTu();
- }
- vector <string> TuXuatHienNhieuNhat(int &SoLanXuatHien)
- {
- vector <string> DanhSachTuXuatHienNhieuNhat;
- string w = "";
- int BienTam = 0;
- SoLanXuatHien = 0;
- sort(Tu.begin(), Tu.end());
- int len = Tu.size();
- for (int i = 1; i < len; i++)
- {
- if (Tu[i] == Tu[i - 1])
- BienTam++;
- else
- {
- if (BienTam >= SoLanXuatHien)
- {
- if (BienTam > SoLanXuatHien)
- {
- while (!DanhSachTuXuatHienNhieuNhat.empty())
- DanhSachTuXuatHienNhieuNhat.pop_back();
- }
- DanhSachTuXuatHienNhieuNhat.push_back(Tu[i - 1]);
- SoLanXuatHien = BienTam;
- }
- BienTam = 0;
- }
- }
- return DanhSachTuXuatHienNhieuNhat;
- }
- int LaySoTu()
- {
- return this->Tu.size();
- }
- int LaySoCau()
- {
- return this->Cau.size();
- }
- string LayNoiDungCau(int i)
- {
- if (i >= Cau.size()) return "";
- return Cau[i];
- }
- vector <string> SapXep()
- {
- sort(Tu.begin(), Tu.end());
- return this->Tu;
- }
- private:
- fstream f;
- string NoiDung;
- vector <string> Tu;
- vector <string> Cau;
- };
- MyClass::MyClass(char *path)
- {
- f.open(path, ios::in);
- DocFile();
- }
- MyClass::~MyClass()
- {
- f.close();
- }
- void main()
- {
- int n;
- MyClass myclass;
- fstream f;
- f.open("D:/output.txt", ios::out);
- int SoCau = myclass.LaySoCau();
- f << SoCau << endl;
- for (int i = 0; i < SoCau; i++)
- {
- MyClass a(myclass.LayNoiDungCau(i));
- int SoTu, SoLanXuatHien;
- SoTu = a.LaySoTu();
- f << SoTu << endl;
- }
- vector <string> TuXuatHienNhieu = myclass.TuXuatHienNhieuNhat(n);
- while (!TuXuatHienNhieu.empty())
- {
- f << TuXuatHienNhieu[TuXuatHienNhieu.size() - 1] << " ";
- TuXuatHienNhieu.pop_back();
- }
- f << endl;
- for (int i = 0; i < SoCau; i++)
- {
- MyClass a(myclass.LayNoiDungCau(i));
- vector <string> DanhSachSapXep = a.SapXep();
- for (int i = 0; i < DanhSachSapXep.size(); i++)
- f << DanhSachSapXep[i] << " ";
- while (!DanhSachSapXep.empty()) DanhSachSapXep.pop_back();
- f << endl;
- }
- f.flush();
- f.close();
- system("D:/output.txt");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement