fork(1) download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. bool isNumber(const string& s) {
  9. long double t;
  10. istringstream r(s);
  11.  
  12. return r >> t >> ws && r.eof();
  13. }
  14.  
  15. int main() {
  16. std::vector<string> test{
  17. "3", "-3", // ints
  18. "1 3", " 13", " 1 3 ", "13 ", // weird spacing
  19. "0.1", "-100.123123", "1000.12312", // fixed point
  20. "1e5", "1e-5", "1.5e-10", "1a.512e4", // floating point
  21. "a", "1a", "baaa", "1a", // invalid
  22. "1e--5", "1e-", //invalid
  23. };
  24.  
  25. for (auto t : test) {
  26. cout.width(20); cout << t << " " << isNumber(t) << endl;
  27. }
  28. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
                   3 1
                  -3 1
                 1 3 0
                  13 1
                1 3  0
                 13  1
                 0.1 1
         -100.123123 1
          1000.12312 1
                 1e5 1
                1e-5 1
             1.5e-10 1
            1a.512e4 0
                   a 0
                  1a 0
                baaa 0
                  1a 0
               1e--5 0
                 1e- 0