📜 ⬆️ ⬇️

Battered banality. As a schoolboy bot wrote

I'm 16 and I'm a schoolboy. Not so long ago, I was visited by the idea to write a bot ... No, not PHP-sharing, sadly hanging on nobody else's site. And not even a useless respondent to phrases like "! Weather."

Bot thought for fun as a "gossip" on the desktop. Awful, right? But I want to know my mistakes, because I have never shown my code to anyone, only Pascal is at school. So, following some purely structural approach, hated, I wrote the original version in C ++.

The idea is as follows. The bot takes a phrase from the console, extracts the words, checks each one in the dictionary located in the “Memory.txt” file, and returns the found answer to each word; if the answer is not found for one word, then it returns the stipulated phrase (not fundamentally).
')
The dictionary in the “Memory.txt” file is structured in the simplest way:
word = answer

Example:
apple = delicious apples

Bot.h- header file, about it later. The main functions will be located in the file Bot.cpp:

/**  , 2015   Cbot */ #include"Bot.h" 


Define a name for the dictionary in the same file:

 ///  -   const char *const MemoryPath="Memory.txt"; 


The “warp base” of a bot is a function that selects words from a single line into an array of strings and returns a pointer to an array.
const std :: string * const GetWords (const std :: string & Word)
  ///    - ,    ... int MaxIndex=0; ///     const std::string *const GetWords(const std::string &Word) { ///     256  std::string *const PtrWords=new std::string[256]; ///   MaxIndex=0; ///       bool Fix=false; /// - ,    for(int i=0; i<Word.size(); ++i) { ///-   if(Word[i]==' '||Word[i]=='.'||Word[i]==','||Word[i]=='!'||Word[i]=='?'||Word[i]=='='||Word[i]=='/') { ///  ,       Fix=true; continue; } ///     ,      if(Fix) { Fix=false; ++MaxIndex; } PtrWords[MaxIndex]+=Word[i]; } return PtrWords; } 


The following function gets the string to search for and looks for it in the dictionary; if the answer is not found, it returns an empty string "". I want to draw attention to the fact that if a word is found inside another word in the file of associations, the answer will be counted.
const std :: string GetAssociation (const std :: string & Word)
 ///   const std::string GetAssociation(const std::string &Word) { std::ifstream Memory(MemoryPath, std::ios::in); if(!Memory) { std::ofstream NewMemory(MemoryPath); NewMemory.close(); Memory.open(MemoryPath); return ""; } while(!Memory.eof()) { std::string Buffer=""; std::getline(Memory, Buffer); if(Buffer.find(Word)!=-1) { std::string Result[2]; for(int i=0, Index=0; i<Buffer.size(); ++i) { if(Buffer[i]=='=') { ///    '=' -  if(Index==1) { break; } ++Index; continue; } Result[Index]+=Buffer[i]; } if(Result[0].find(Word)!=-1) { Memory.close(); return Result[1]; } } } Memory.close(); return ""; } 


Now you can think about the optional trivia-terrible parody of learning - adding new associations when in the line the symbol '-'.
Example:
Evil is good on the contrary
The dictionary goes:
Evil = this is the opposite
Do not forget that when you find a word inside another, the answer is counted, so that the result can be interesting.
void PutAssociation (const std :: string & Left, const std :: string & Right)
 ///   void PutAssociation(const std::string &Left, const std::string &Right) { std::ofstream Memory(MemoryPath, std::ios::app); Memory<<Left<<'='<<Right<<std::endl; Memory.close(); } 


In my view, the structural approach does not cancel the encapsulation, so we will add an anonymous namespace - for the banal encapsulation, which includes all previous functions.

Thus, the previous functions will no longer be available when the “Bot.h” header file is connected. Beg to refer to the guru:
This is the most advanced way to avoid declaring functions and variables with static binding.
Access can only be made within a unit.
broadcast (i.e. in the file received after preprocessing),
in which they are located, just like to static variables.
Stephen C. Durhest, “C ++. Sacred knowledge "

Here, all together:
namespace
 namespace { ///  -   const char *const MemoryPath="Memory.txt"; ///     int MaxIndex=0; ///     const std::string *const GetWords(const std::string &Word) { ///   256  std::string *const PtrWords=new std::string[256]; ///   MaxIndex=0; ///       bool Fix=false; /// - ,    for(int i=0; i<Word.size(); ++i) { ///-   if(Word[i]==' '||Word[i]=='.'||Word[i]==','||Word[i]=='!'||Word[i]=='?'||Word[i]=='='||Word[i]=='/') { ///  ,       Fix=true; continue; } ///     ,      if(Fix) { Fix=false; ++MaxIndex; } PtrWords[MaxIndex]+=Word[i]; } return PtrWords; } ///   void PutAssociation(const std::string &Left, const std::string &Right) { std::ofstream Memory(MemoryPath, std::ios::app); Memory<<Left<<'='<<Right<<std::endl; Memory.close(); } } 


Finally, the function for communicating with the outside world is, of course, outside the namespace, but in the same compilation unit. Accepts a phrase, isolates words, gets associations for each one, when finding a symbol and adds a new association using the previous functions:
const std :: string GetFullAssociation (const std :: string & Word)
 ///      const std::string GetFullAssociation(const std::string &Word) { const std::string *const Words=GetWords(Word); std::string Result=""; for(int i=0; i<=MaxIndex; ++i) { const std::string Buffer=GetAssociation(Words[i]); if(Buffer!="") { Result+=Buffer+' '; } } delete[] Words; if(Word.find('-')!=-1) { std::string NewAssociations[2]; for(int i=0, Index=0; i<Word.size(); ++i) { if(Word[i]=='-') { if(Index==1) { break; } ++Index; continue; } if(Word[i]=='=') { continue; } NewAssociations[Index]+=Word[i]; } PutAssociation(NewAssociations[0], NewAssociations[1]); } return Result; } 



And now let's summarize - the “Bot.cpp” file completely:

Bot.cpp
 /**  , 2015   Cbot */ #include"Bot.h" ///          namespace { ///  -   const char *const MemoryPath="Memory.txt"; ///     int MaxIndex=0; ///     const std::string *const GetWords(const std::string &Word) { ///   256  std::string *const PtrWords=new std::string[256]; ///   MaxIndex=0; ///       bool Fix=false; /// - ,    for(int i=0; i<Word.size(); ++i) { ///-   if(Word[i]==' '||Word[i]=='.'||Word[i]==','||Word[i]=='!'||Word[i]=='?'||Word[i]=='='||Word[i]=='/') { ///  ,       Fix=true; continue; } ///     ,      if(Fix) { Fix=false; ++MaxIndex; } PtrWords[MaxIndex]+=Word[i]; } return PtrWords; } ///   const std::string GetAssociation(const std::string &Word) { std::ifstream Memory(MemoryPath, std::ios::in); if(!Memory) { std::ofstream NewMemory(MemoryPath); NewMemory.close(); Memory.open(MemoryPath); return ""; } while(!Memory.eof()) { std::string Buffer=""; std::getline(Memory, Buffer); if(Buffer.find(Word)!=-1) { std::string Result[2]; for(int i=0, Index=0; i<Buffer.size(); ++i) { if(Buffer[i]=='=') { ///    '=' -  if(Index==1) { break; } ++Index; continue; } Result[Index]+=Buffer[i]; } if(Result[0].find(Word)!=-1) { Memory.close(); return Result[1]; } } } Memory.close(); return ""; } ///   void PutAssociation(const std::string &Left, const std::string &Right) { std::ofstream Memory(MemoryPath, std::ios::app); Memory<<Left<<'='<<Right<<std::endl; Memory.close(); } } ///      const std::string GetFullAssociation(const std::string &Word) { const std::string *const Words=GetWords(Word); std::string Result=""; for(int i=0; i<=MaxIndex; ++i) { const std::string Buffer=GetAssociation(Words[i]); if(Buffer!="") { Result+=Buffer+' '; } } delete[] Words; if(Word.find('-')!=-1) { std::string NewAssociations[2]; for(int i=0, Index=0; i<Word.size(); ++i) { if(Word[i]=='-') { if(Index==1) { break; } ++Index; continue; } if(Word[i]=='=') { continue; } NewAssociations[Index]+=Word[i]; } PutAssociation(NewAssociations[0], NewAssociations[1]); } return Result; } 



That's all with the file "Bot.cpp" we have finished, now we quickly type the header file "Bot.h":

Bot.h
 #ifndef BOT #define BOT ///   ,    iostream #ifndef _GLIBCXX_IOSTREAM #include<iostream> #endif //_GLIBCXX_IOSTREAM /// fstream #ifndef _GLIBCXX_FSTREAM #include<fstream> #endif //_GLIBCXX_FSTREAM ///        extern const std::string GetFullAssociation(const std::string&); #endif //BOT 



We have finished with the main part, the case for smallness is the main () function. It will be located in the Cbot.cpp file. Cbot - sounds incredibly original, right?

Cbot.cpp
 #include"Bot.h" int main() { ///  866 OEM (),  "Memory.txt"      setlocale(LC_ALL, ".866"); std::wcout<<"Cbot 2.0\n:  \nE-Mail: DDemidko1@gmail.com"<<std::endl; while(true) { std::wcout<<": "; std::string Buffer=""; std::getline(std::cin, Buffer); const std::string Association=GetFullAssociation(Buffer); /**   ? ,     - if(Association=="") { Association="Bot:    !"; } std::cout<<Association<<std::endl;     ,    866 OEM-        std::string      -   ( 866 OEM)     . */ if(Association=="") { std::wcout<<"Bot:    !"<<std::endl; } else { std::cout<<"Bot: "<<Association<<std::endl; } } } 



Everything, the bot is ready, we put it together, we get Cbot.exe, we save the Memory.txt file in OEM 866 encoding and put it in one directory with the program. Link to the assembly: spaces.ru/files/?r=main/view&Read=58688510

I expect a constructive stream of criticism showing obvious errors in the code.

Source: https://habr.com/ru/post/266351/


All Articles