Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
SayHi
//g++ 5.4.0 #include <iostream> #include <string> #include <vector> #include <algorithm> #include <stdlib.h> #include <time.h> using std::string; class DataMapper { private: std::vector<string> _responses; std::vector<string> _greetings; void generateResponses(); void generateGreetings(); public: DataMapper() { generateResponses(); generateGreetings(); } ~DataMapper() {} string mapGreetingToResponse(string greeting); string convertStringToLower(string s); bool greetingExists(string toFind); int generateRandomIndex(); int getResponsesVectorSize() { return _responses.size(); } }; inline void DataMapper::generateResponses(){ _responses.push_back("Hi there!"); _responses.push_back("Hello!"); _responses.push_back("Hey!"); _responses.push_back("Hi, nice to meet you!"); _responses.push_back("How are you!"); } inline void DataMapper::generateGreetings() { _greetings.push_back("hi"); _greetings.push_back("hello"); _greetings.push_back("hey there"); _greetings.push_back("hi there"); } string DataMapper::mapGreetingToResponse(string greeting) { greeting = convertStringToLower(greeting); if(!greetingExists(greeting)) { return "I'm sorry I didn't understand."; } int index = generateRandomIndex(); return _responses[index]; } string DataMapper::convertStringToLower(string s) { std::transform(s.begin(), s.end(), s.begin(), ::tolower); return s; } bool DataMapper::greetingExists(string toFind) { if(std::find(_greetings.begin(), _greetings.end(), toFind) != _greetings.end()) { return true; } return false; } int DataMapper::generateRandomIndex() { srand(time(0)); int max = getResponsesVectorSize(); return rand() % max; } int main() { string userGreeting; std::getline(std::cin, userGreeting); DataMapper mapper; string botResponse = mapper.mapGreetingToResponse(userGreeting); std::cout << botResponse << std::endl; }
run
|
edit
|
history
|
help
0
TraceMarrix
ONP without brackets
Test 12(2021)
StrStrPbrk
Wipro Problem 2: determine count
Dar
Microsoft - MaxEmployeeAttendence (R repititions - DP solution bitmask)
ContainerVector
a
¡Arriba, Papalotes, Arriba!