/** , 2015 Cbot */ #include"Bot.h"
/// - 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(); }
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 "
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(); } }
/// 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; }
/** , 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; }
#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
#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; } } }
Source: https://habr.com/ru/post/266351/
All Articles