Advertisement
Yurry

Сигал, задача №4, 09.2009

Jul 25th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. typedef set<int> result_type;
  2.  
  3. template <typename T, typename F>
  4. T ConvertTo(F f)
  5. {
  6.     std::stringstream st;
  7.     st << f;
  8.     T t;
  9.     st >> t;
  10.     return t;
  11. }
  12.  
  13. result_type parse(const string &rsSource)
  14. {
  15.     result_type xResult;
  16.     string::size_type i = 0, iSourceSize = rsSource.size();
  17.    
  18.     int iRangeBegin;
  19.    
  20.     enum
  21.     {
  22.         wmSingle,
  23.         wmRange
  24.     } eWorkMode = wmSingle;
  25.    
  26.     while (i < uiSourceSize)
  27.     {
  28.         string::size_type j = i;
  29.        
  30.         for ( ; (i < uiSourceSize) && (rsSource[i] >= '0') && (rsSource[i] <= '9') ; i++ ) {} // this is intent
  31.        
  32.         if (i == uiSourceSize || rsSource[i] == ',')
  33.         {
  34.             if (eWorkMode == wmSingle)
  35.                 xResult.insert(ConvertTo<int>(rsSource.substr(j, i-j)));
  36.             else
  37.             {
  38.                 int iRangeEnd = ConvertTo<int>(rsSource.substr(j, i-j));
  39.                
  40.                 for (int n = iRangeBegin; n <= iRangeEnd; n++)
  41.                     xResult.insert(n);
  42.             }
  43.         }
  44.         else if (rsSource[i] == '-')
  45.         {
  46.             eWorkMode = wmRange;
  47.             iRangeBegin = ConvertTo<int>(rsSource.substr(j, i-j));
  48.         }
  49.     }
  50.    
  51.     if (eWorkMode == wmRange)
  52.         return result_type(); // what shit is it?!
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement