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