Last active
December 26, 2017 10:10
-
-
Save Glavak/200dd990f93e509aa0ec9654ff7bcbac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<conio.h> | |
#include <iostream> | |
#include <vector> | |
#define _CRT_SECURE_NO_WARNINGS | |
#pragma warning(disable:4996) | |
class string | |
{ | |
private: | |
char*datapointer; | |
int sizemem; | |
int numbel; | |
public: | |
string() //конструктор по умолчанию | |
{ | |
datapointer = new char[2]; | |
sizemem = 2; | |
numbel = 0; | |
datapointer[0] = '\0'; | |
} | |
string(char*str) //оператор приведения к const char* | |
{ | |
sizemem = strlen(str) + 1; | |
numbel = strlen(str); | |
datapointer = new char[sizemem]; | |
memcpy(datapointer, str, sizemem); | |
} | |
string(string & old) //конструктор копирования | |
{ | |
sizemem = old.sizemem + 1; | |
numbel = old.numbel; | |
datapointer = new char[sizemem]; | |
memcpy(datapointer, old.datapointer, sizemem); | |
} | |
string & operator =(string & old) //оператор присваивания | |
{ | |
sizemem = old.sizemem; | |
numbel = old.numbel; | |
delete[] datapointer; | |
datapointer = new char[sizemem]; | |
memcpy(datapointer, old.datapointer, sizemem); | |
return *this; | |
} | |
operator char *() const //конструктор с параметром char | |
{ | |
return datapointer; | |
} | |
~string() //деструктор | |
{ | |
delete[] datapointer; | |
} | |
void removeRepeatingWords() | |
{ | |
std::vector<char*> wordStarts; // список уже встреченных слов | |
char * newData = new char[sizemem]; // новый текст | |
sizemem = 0; | |
// Берём первое слово из текста | |
char *tok = strtok(datapointer, " "); | |
while (tok != NULL) | |
{ | |
// Пытаемся найти это слово в списке уже встреченных | |
int found = 0; | |
for (size_t j = 0; j < wordStarts.size(); j++) | |
{ | |
if (strcmp(wordStarts[j], tok) == 0) | |
{ | |
found = 1; | |
break; | |
} | |
} | |
// Если не нашли | |
if (!found) | |
{ | |
// То добавляем его в список | |
wordStarts.push_back(tok); | |
// И копируем в новую строку | |
memcpy(newData + sizemem, tok, strlen(tok)); | |
sizemem += strlen(tok); | |
newData[sizemem++] = ' '; | |
} | |
// Переходим к следующему слову | |
tok = strtok(NULL, " ,.-"); | |
} | |
delete[] datapointer; | |
newData[sizemem++] = '\0'; | |
datapointer = newData; | |
} | |
}; | |
int main() | |
{ | |
char * hjk = "hjk asd asd hjk ggg"; | |
string a(hjk); | |
string str = a; | |
str = a; | |
std::cout << str << std::endl; | |
str.removeRepeatingWords(); | |
std::cout << str << std::endl; | |
_getch(); | |
system("pause"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment