Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "library.h"
- #include <fstream>
- #include <iostream>
- #include <string>
- using namespace std;
- List::List()
- {
- Start = nullptr;
- End = nullptr;
- }
- List::~List()
- {
- Reserv* prom;
- Reserv* t = Start;
- while (t != 0) {
- prom = t;
- t = t->next;
- delete prom;
- }
- }
- void List::Print(std::ofstream& file2)
- {
- Reserv* t = Start;
- while (t != 0) {
- file2 << t->word << " Count: " << t->count << endl;
- t = t->next;
- }
- }
- void List::Add(string cur)
- {
- Reserv* Temp = new Reserv;
- Temp->word = cur;
- Temp->next = nullptr;
- if (Start == nullptr) {
- Start = End = Temp;
- }
- else {
- End->next = Temp;
- End = Temp;
- }
- }
- void List::Read(std::ifstream& file1)
- {
- string cur;
- while (!file1.eof()) {
- getline(file1, cur);
- Reserv* Temp = find(cur);
- if (Temp == nullptr) this->Add(cur);
- }
- }
- Reserv* List::find(string cur)
- {
- Reserv* t = Start;
- while (t != 0) {
- if (t->word == cur) {
- return t;
- }
- t = t->next;
- }
- return nullptr;
- }
- void List::Count(string* Array, int n)
- {
- Reserv* t = Start;
- while (t != 0) {
- for (int i = 0; i < n; ++i) {
- //cout << Array[i] << " " << t->word << endl;
- if (t->word == Array[i]) {
- t->count++;
- }
- }
- t = t->next;
- }
- }
- List List::InsertSort()
- {
- List res;
- Reserv* t = Start;
- while (t != nullptr) {
- Reserv* NewElement = new Reserv;
- NewElement->count = t->count;
- NewElement->word = t->word;
- if (res.Start == nullptr) {
- res.Start = res.End = NewElement;
- }
- else {
- if (res.Start->count > NewElement->count) {
- NewElement->next = res.Start;
- res.Start = NewElement;
- }
- else {
- Reserv* i = res.Start->next;
- Reserv* prev = nullptr; // i - 1 index
- while (i != nullptr) {
- if (i->count > NewElement->count) {
- NewElement->next = i;
- if (prev != nullptr) {
- prev->next = NewElement;
- }
- break;
- }
- prev = i;
- i = i->next;
- }
- if (i == nullptr) {
- res.End->next = NewElement;
- res.End = NewElement;
- }
- }
- }
- t = t->next;
- }
- return res;
- }
- void List::BubleSort()
- {
- Reserv* cur = Start;
- while (cur != nullptr) {
- Reserv* min = cur;
- Reserv* temp = cur->next;
- while (temp != nullptr) {
- if (temp->count < min->count) {
- min = temp;
- }
- temp = temp->next;
- }
- swap(cur->word, min->word);
- swap(cur->count, min->count);
- cur = cur->next;
- }
- }
- string* TextDivision(string input, int wordCount)
- {
- string* words = new string[wordCount];
- int wordIndex = 0;
- int charIndex = 0;
- while (charIndex < input.length()) {
- while (isspace(input[charIndex]) or input[charIndex]==',' or input[charIndex] == ':' or input[charIndex] == ';') {
- charIndex++;
- }
- //cout << charIndex << " " << endl;
- string cur = "";
- while (charIndex < input.length() and !isspace(input[charIndex]) and input[charIndex] != ',' and input[charIndex] != ':' and input[charIndex] != ';') {
- cur += input[charIndex];
- charIndex++;
- }
- //cout << cur << endl;
- words[wordIndex] = cur;
- wordIndex++;
- }
- return words;
- }
- string ReadText(std::ifstream& file3)
- {
- string res= "";
- string cur;
- while (!file3.eof()) {
- getline(file3, cur);
- res += " " + cur;
- }
- return res;
- }
- int CountWord(string input)
- {
- int wordCount = 0;
- for (int i = 0; i < input.length(); i++) {
- if (isspace(input[i]) or input[i] == ',' or input[i] == ':' or input[i] == ';') {
- wordCount++;
- while (isspace(input[i]) or input[i] == ',' or input[i] == ':' or input[i] == ';') {
- i++;
- }
- //cout << i << " " << input[i] << endl;
- }
- //cout << i << " " << input[i] << endl;
- }
- return wordCount;
- }
Add Comment
Please, Sign In to add comment