Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "library.h"
- ListF::ListF()
- {
- Start = nullptr;
- End = nullptr;
- }
- ListF::~ListF()
- {
- Reserv* prom;
- Reserv* t = Start;
- while (t != 0) {
- prom = t;
- t = t->next;
- delete prom;
- }
- }
- void ListF::ReadF(fstream& FD)
- {
- int idx, count;
- string cur;
- Reserv* Temp = new Reserv;
- FD >> idx;
- FD >> cur;
- FD >> count;
- if (idx > 0) {
- Temp->word = cur;
- Temp->idx = idx;
- Temp->count = count;
- Temp->next = nullptr;
- if (Start == nullptr) {
- Start = End = Temp;
- }
- else {
- End->next = Temp;
- End = Temp;
- }
- }
- }
- void ListF::ReadTextF(string text)
- {
- string word;
- bool inWord = false;
- for (char c : text) {
- if (isalpha(c)) {
- word += c;
- inWord = true;
- }
- else {
- if (inWord) {
- bool found = false;
- Reserv* current = Start;
- while (current != nullptr) {
- if (current->word == word) {
- found = true;
- current->count++;
- word.clear();
- inWord = false;
- break;
- }
- current = current->next;
- }
- if (!found) {
- AddWord(word);
- word.clear();
- inWord = false;
- }
- }
- }
- }
- }
- void ListF::AddWord(string word) {
- Reserv* newReserv = new Reserv;
- newReserv->word = word;
- newReserv->count = 1;
- newReserv->next = nullptr;
- int idx = LastIdx();
- newReserv->idx = ++idx;
- if (Start == nullptr) {
- Start = End = newReserv;
- }
- else {
- End->next = newReserv;
- End = newReserv;
- }
- }
- int ListF::LastIdx()
- {
- int idx = 0;
- Reserv* t = Start;
- while (t != 0) {
- idx = t->idx;
- t = t->next;
- }
- return idx;
- }
- Reserv* ListF::getStart()
- {
- return Start;
- }
- void ListF::PrintListF(fstream& FD)
- {
- Reserv* t = Start;
- while (t != 0) {
- FD << t->idx << " " << t->word << " " << t->count << endl;
- t = t->next;
- }
- }
- string ReadText(std::ifstream& input)
- {
- string res = "";
- string cur;
- while (!input.eof()) {
- getline(input, cur);
- res += " " + cur;
- }
- res += " ";
- return res;
- }
- ListB::ListB()
- {
- Start = nullptr;
- End = nullptr;
- }
- ListB::~ListB()
- {
- BD* prom;
- BD* t = Start;
- while (t != 0) {
- prom = t;
- t = t->next;
- delete prom;
- }
- }
- void ListB::ReadB(fstream& BDtext)
- {
- int idx, count;
- string cur;
- string t;
- BD* Temp = new BD;
- BDtext >> idx;
- getline(BDtext, cur);
- getline(BDtext, t); //"\n"
- if (idx > 0) {
- Temp->str = cur;
- Temp->idx = idx;
- Temp->next = nullptr;
- if (Start == nullptr) {
- Start = End = Temp;
- }
- else {
- End->next = Temp;
- End = Temp;
- }
- }
- }
- void ListB::ReadTextB(string text, ListF A)
- {
- string word1;
- string word2;
- string cur1;
- string cur2;
- bool inword1 = false;
- bool inword2 = false;
- bool Flag = false;
- for (char c : text) {
- if (c == '!' || c == ',' or c == '.') {
- word1 = "";
- word2 = "";
- inword1 = false;
- inword2 = false;
- Flag = false;
- }
- if (isalpha(c)) {
- word1 += c;
- inword1 = true;
- }
- if (c == ' ' && inword1) {
- inword2 = true;
- }
- if (isalpha(c) && inword2) {
- word2 += c;
- Flag = true;
- }
- if (c == ' ' && Flag) {
- Reserv* currentFD1 = A.getStart();
- while (currentFD1 != nullptr) {
- if (currentFD1->word == word1) {
- Reserv* currentFD2 = currentFD1->next;
- while (currentFD2 != nullptr) {
- if (currentFD2->word == word2) {
- int idx0 = currentFD2->idx;
- BD* currentBD = Start;
- while (currentBD != nullptr) {
- if (currentBD->str != "") { // нашли два слова и строка не пустая
- int length = currentBD->str.length();
- string cur = currentBD->str;
- string res = "";
- string residx = "";
- string rescount = "";
- int count0 = 0;
- bool Residx = false;
- bool Result = false;
- int RememberIdx = 0;
- for (int i = 0; i < length - 1; i++) {
- if (isdigit(cur[i]) && !isdigit(cur[i + 1])) {
- residx += cur[i];
- Residx = true;
- RememberIdx = i;
- }
- if (stoi(residx) == idx0) {
- if (Residx and isdigit(cur[i]) && !isdigit(cur[i + 1])) {
- rescount += cur[i];
- Result = true;
- }
- if (Result) {
- count0 = stoi(rescount) + 1;
- rescount = to_string(count0);
- res = cur.substr(0, RememberIdx)+residx + " " + rescount + cur.substr(i, length - i);
- break;
- }
- }
- }
- }
- else { // нашли два слова и строка пустая
- BD* currentBD2 = Start;
- while (currentBD2 != nullptr) {
- if (currentBD2->idx == stoi(word1)) currentBD2->str = to_string(currentBD2->idx) + " 1";
- currentBD2 = currentBD2->next;
- }
- }
- currentBD = currentBD->next;
- }
- }
- currentFD2 = currentFD2->next;
- }
- }
- else {
- int Last = LastIdx();
- BD* Temp0 = new BD;
- BD* Temp1 = new BD;
- Temp0->idx = 1;
- Temp0->str = "1 2 1";
- Temp0->next = Temp1;
- Temp1->idx = 2;
- Temp1->str = "";
- Temp1->next = nullptr;
- Start = Temp0;
- }
- currentFD1 = currentFD1->next;
- }
- }
- }
- }
- int ListB::LastIdx()
- {
- int Last = 0;
- BD* t = Start;
- while (t != 0) {
- Last = t->idx;
- t = t->next;
- }
- return Last;
- }
- void ListB::PrintListB(fstream& BDtext)
- {
- BD* t = Start;
- while (t != 0) {
- BDtext << t->idx << " " << t->str << endl;
- t = t->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement