Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. static const std::string FIREFIGHTER = "FIREFIGHTER_SECRET_KEY";
  8.  
  9. bool isCommand(std::string command, bool checkWholeCommand){
  10. static const std::string acceptedCommands[] = {"INSERT KEY", "TURN KEY", "WHO'S INSIDE?", "CHANGE LOCKS", "ENTER HOUSE", "LEAVE HOUSE"};
  11. int arrSize = sizeof(acceptedCommands)/sizeof(acceptedCommands[0]);
  12. for(int i = 0; i < arrSize; i++){
  13. if(checkWholeCommand){
  14. if(acceptedCommands[i] == command)
  15. return true;
  16. }else{
  17. if(acceptedCommands[i].find(command) != std::string::npos)
  18. return true;
  19. }
  20.  
  21. }
  22. return false;
  23. }
  24.  
  25. int countWords(std::string input, int counter = 0){
  26. std::string temp;
  27. char buf[10000];
  28. char buf2[13000];
  29. char result[14000];
  30. std::size_t found = input.find(' ');
  31.  
  32. if (found == std::string::npos)
  33. {
  34. return counter + 1;
  35. }
  36. else
  37. {
  38. return countWords(input.substr(found+1), counter + 1);
  39. }
  40. }
  41.  
  42. void secure_house(int argc, char *argv[]){
  43.  
  44. std::string input;
  45. std::string owner;
  46. std::string currentUsername;
  47. std::string currentKey;
  48. bool userCanEnter;
  49. std::vector<std::string> insideHouse;
  50. std::vector<std::string> currentCommand;
  51. std::vector<std::string> keys;
  52.  
  53. if(argc != 1){
  54.  
  55. owner = argv[1];
  56.  
  57. for(int i = 2; i < argc; i++)
  58. keys.push_back(argv[i]);
  59. }
  60.  
  61. while(std::getline(std::cin, input)){
  62.  
  63. std::stringstream ss(input);
  64. std::string temp;
  65.  
  66. std::string command;
  67. int commandCounter = 0;
  68. currentCommand.clear();
  69.  
  70. while(std::getline(ss, temp, ' ')){
  71.  
  72. if(commandCounter < 2){
  73. currentCommand.push_back(temp);
  74.  
  75. if(commandCounter == 0){
  76. int wordCount = countWords(input);
  77. if(wordCount == 1){
  78. std::cout << "\tERROR" << std::endl;
  79. continue;
  80. }
  81. }
  82.  
  83. if(commandCounter == 1){
  84.  
  85. command = "";
  86.  
  87. std::vector<std::string>::iterator counter = currentCommand.begin();
  88. while(counter != currentCommand.end()){
  89. if(counter == currentCommand.begin()){
  90. command += *counter;
  91. command += ' ';
  92. }else
  93. command += *counter;
  94. counter++;
  95. }
  96.  
  97. currentCommand.clear();
  98.  
  99. if(isCommand(command, true)){
  100. int wordCount = countWords(input);
  101. if(wordCount < 4 && command == "INSERT KEY"){
  102. std::cout << "\tERROR" << std::endl;
  103. continue;
  104. }else if(wordCount < 3 && (command == "TURN KEY" || command == "ENTER HOUSE" || command == "LEAVE HOUSE" || command == "CHANGE LOCKS")){
  105. std::cout << "\tERROR" << std::endl;
  106. continue;
  107. }
  108. if(command == "WHO'S INSIDE?"){
  109. if(insideHouse.size() == 0)
  110. std::cout << "\tNOBODY HOME" << std::endl;
  111. else{
  112. std::vector<std::string>::iterator counter = insideHouse.begin();
  113. std::cout << "\t";
  114. while(counter != insideHouse.end()){
  115. if(*counter == insideHouse.rbegin()[0])
  116. std::cout << *counter << std::endl;
  117. else
  118. std::cout << *counter << ", ";
  119. counter++;
  120. }
  121. }
  122. }
  123. }else{
  124. std::cout << "\tERROR" << std::endl;
  125. break;
  126. }
  127.  
  128. }
  129.  
  130. commandCounter++;
  131.  
  132. }else{
  133.  
  134. std::vector<std::string>::iterator first = keys.begin();
  135. std::vector<std::string>::iterator last = keys.end();
  136.  
  137. if(command == "INSERT KEY"){
  138.  
  139. currentCommand.push_back(temp);
  140.  
  141. if(commandCounter == 3){
  142. currentKey = currentCommand.rbegin()[0];
  143. currentUsername = *currentCommand.begin();
  144. std::cout << "\tKEY " << currentKey << " INSERTED BY " << currentUsername << std::endl;
  145. break;
  146. }
  147.  
  148. }else if(command == "TURN KEY"){
  149.  
  150. currentCommand.push_back(temp);
  151.  
  152. if(commandCounter == 2){
  153. if((std::find(first, last, currentKey) != last || currentKey == FIREFIGHTER) && currentUsername == *currentCommand.begin()){
  154. userCanEnter = true;
  155. std::cout << "\tSUCCESS " << currentUsername << " TURNS KEY " << currentKey << std::endl;
  156. break;
  157. }else{
  158. std::cout << "\tFAILURE " << *currentCommand.begin() << " UNABLE TO TURN KEY " << currentKey << std::endl;
  159. break;
  160. }
  161. }
  162. }else if(command == "ENTER HOUSE"){
  163.  
  164. currentCommand.push_back(temp);
  165.  
  166. if(commandCounter == 2){
  167.  
  168. if(currentUsername != *currentCommand.begin()){
  169. currentUsername = *currentCommand.begin();
  170. userCanEnter = false;
  171. }
  172.  
  173. if(userCanEnter){
  174. insideHouse.push_back(currentUsername);
  175. userCanEnter = false;
  176. std::cout << "\tACCESS ALLOWED" << std::endl;
  177. break;
  178. }else{
  179. std::cout << "\tACCESS DENIED" << std::endl;
  180. break;
  181. }
  182. }
  183. }else if(command == "LEAVE HOUSE"){
  184.  
  185. currentCommand.push_back(temp);
  186.  
  187. if(commandCounter == 2){
  188. std::vector<std::string>::iterator remove = std::find(insideHouse.begin(), insideHouse.end(), *currentCommand.begin());
  189. if(remove != insideHouse.end()){
  190. insideHouse.erase(remove);
  191. std::cout << "\tOK" << std::endl;
  192. break;
  193. }else{
  194. std::cout << *currentCommand.begin() << " \tNOT HERE" << std::endl;
  195. break;
  196. }
  197. }
  198. }else if(command == "CHANGE LOCKS"){
  199.  
  200. currentCommand.push_back(temp);
  201.  
  202. std::vector<std::string>::iterator checkForOwner = std::find(insideHouse.begin(), insideHouse.end(), owner);
  203.  
  204. if(owner == *currentCommand.begin() && checkForOwner != insideHouse.end()){
  205.  
  206. if(commandCounter > 2)
  207. keys.push_back(temp);
  208. else{
  209. std::cout << "\tOK" << std::endl;
  210. keys.clear();
  211. }
  212.  
  213. }else{
  214. if(commandCounter == 2)
  215. std::cout << "\tACCESS DENIED" << std::endl;
  216. break;
  217. }
  218.  
  219. }
  220.  
  221. commandCounter++;
  222.  
  223. }
  224.  
  225. }
  226.  
  227. }
  228. }
  229.  
  230. int main(int argc, char *argv[]){
  231.  
  232. secure_house(argc, argv);
  233.  
  234. return 0;
  235.  
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement