Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.65 KB | None | 0 0
  1. #include <string>
  2.  
  3. bool isValidNumber(std::string a) {
  4.     size_t sz = a.size();
  5.     if (sz == 0) {
  6.         return false;
  7.     }
  8.    
  9.     int cnt = 0;
  10.     int last = 0;
  11.     for (size_t i = 0; i < sz; ++i) {
  12.         if ((a[i] == '.') || (a[i] == ',')) {
  13.             cnt++;
  14.             last = i;
  15.         }
  16.     }
  17.     if (cnt == 0) {
  18.         return (a[0] != '0');
  19.     } else {
  20.         if (cnt > 1) {
  21.             return false;
  22.         } else {
  23.             if ((last == 0) || (last == sz - 1)) {
  24.                 return false;
  25.             } else {
  26.                 return ((a[0] != '0') && (a[sz - 1] != '0'));
  27.             }
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement