Facebook
From Flying Macaque, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 261
  1. #ifndef interface_h
  2. #define interface_h
  3.  
  4. #include <iostream>
  5. #include <string>
  6.  
  7.  
  8. bool interface(const std::string command, const std::string text, int &number1, int &number2);
  9. bool interface(const std::string command, const std::string text, int &number1);
  10. bool interface(const std::string command, const std::string text);
  11. bool is_number(const std::string text);
  12. int to_number(const std::string text);
  13.  
  14. bool interface(const std::string command, const std::string text, int &number1, int &number2) {
  15.         bool result = false;
  16.  
  17.         if (command == text) {
  18.                 std::string param1, param2;
  19.                 std::cin >> param1 >> param2;
  20.  
  21.                 if (is_number(param1) && is_number(param2)) {
  22.                         number1 = to_number(param1);
  23.                         number2 = to_number(param2);
  24.  
  25.                         result = true;
  26.                 }
  27.         }
  28.  
  29.         return result;
  30. }
  31.  
  32. bool interface(const std::string command, const std::string text, int &number1) {
  33.         bool result = false;
  34.  
  35.         if (command == text) {
  36.                 std::string param1;
  37.                 std::cin >> param1;
  38.  
  39.                 if (is_number(param1)) {
  40.                         number1 = to_number(param1);
  41.  
  42.                         result = true;
  43.                 }
  44.         }
  45.  
  46.         return result;
  47. }
  48.  
  49. bool interface(const std::string command, const std::string text) {
  50.         bool result = false;
  51.  
  52.         if (command == text) {
  53.                 result = true;
  54.         }
  55.  
  56.         return result;
  57. }
  58.  
  59. bool is_number(const std::string text) {
  60.         char *num;
  61.         std::strtod(text.c_str(), &num);
  62.  
  63.         return *num == 0;
  64. }
  65.  
  66. int to_number(const std::string text) {
  67.         char *tmp;
  68.  
  69.         return strtol(text.c_str(), &tmp, 10);;
  70. }
  71.  
  72. #endif /* interface_h */
  73.