|
| 1 | +#include <algorithm> |
| 2 | +#include <iostream> |
| 3 | +#include <sstream> |
| 4 | +#include <string> |
| 5 | +#include <vector> |
| 6 | +#include <queue> |
| 7 | +#include <set> |
| 8 | +#include <map> |
| 9 | +#include <cstdio> |
| 10 | +#include <cstdlib> |
| 11 | +#include <cctype> |
| 12 | +#include <cmath> |
| 13 | +#include <string> |
| 14 | +#include <cstring> |
| 15 | +using namespace std; |
| 16 | + |
| 17 | +#define REP(i,n) for(int i=0;i<(n);++i) |
| 18 | +#define FOR(i,a,b) for(int i=(a);i<=(b);++i) |
| 19 | +#define RFOR(i,a,b) for(int i=(a);i>=(b);--i) |
| 20 | +#define FOREACH(it,c) for(typeof((c).begin())it=(c).begin();it!=(c).end();++it) |
| 21 | +#define CLR(x) memset((x),0,sizeof((x))) |
| 22 | +#define MP make_pair |
| 23 | +#define MPI make_pair<int, int> |
| 24 | +#define PB push_back |
| 25 | +typedef long long LL; |
| 26 | +typedef vector<int> VI; |
| 27 | +typedef vector<string> VS; |
| 28 | +typedef pair<int, int> PI; |
| 29 | + |
| 30 | +class Solution { |
| 31 | +public: |
| 32 | + bool isok2(string str) { |
| 33 | + if (str.length() == 0) return false; |
| 34 | + REP(i,str.length()) { |
| 35 | + if (!(str[i] >= '0' && str[i] <= '9')) return false; |
| 36 | + } |
| 37 | + return true; |
| 38 | + } |
| 39 | + bool isok(string str) { |
| 40 | + int idx = -1; |
| 41 | + REP(i,str.length()) { |
| 42 | + if (str[i] == '.') { |
| 43 | + if (idx == -1) idx = i; |
| 44 | + else idx = -2; |
| 45 | + } |
| 46 | + } |
| 47 | + if (idx == -2) return false; |
| 48 | + if (idx == -1) return isok2(str); |
| 49 | + if (str.length() == 1) return false; |
| 50 | + return (idx == 0 || isok2(str.substr(0, idx))) && (idx == str.length() - 1 || isok2(str.substr(idx + 1))); |
| 51 | + } |
| 52 | + bool isNumber(const char *s) { |
| 53 | + string str = string(s); |
| 54 | + while (str.length() > 0 && str[0] == ' ') str.erase(str.begin(), str.begin() + 1); |
| 55 | + while (str.length() > 0 && str[str.length() - 1] == ' ') str.erase(str.end() - 1); |
| 56 | + if (str.length() > 0 && (str[0] == '-' || str[0] == '+')) str.erase(str.begin(), str.begin() + 1); |
| 57 | + int idx = -1; |
| 58 | + REP(i,str.length()) { |
| 59 | + if (str[i] == 'e' || str[i] == 'E') { |
| 60 | + if (idx == -1) idx = i; |
| 61 | + else idx = -2; |
| 62 | + } |
| 63 | + } |
| 64 | + if (idx == -2) return false; |
| 65 | + if (idx == -1) return isok(str); |
| 66 | + FOR(i,idx+1,str.length()-1) { |
| 67 | + if (str[i] == '.') return false; |
| 68 | + } |
| 69 | + if (idx + 1 < str.length() && (str[idx + 1] == '+' || str[idx + 1] == '-')) str.erase(str.begin() + idx + 1, str.begin() + idx + 2); |
| 70 | + return isok(str.substr(0, idx)) && isok(str.substr(idx + 1)); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +int main() { |
| 75 | + Solution s = Solution(); |
| 76 | + cout << s.isNumber("0") << endl; |
| 77 | + cout << s.isNumber(" 0.1 ") << endl; |
| 78 | + cout << s.isNumber("abc") << endl; |
| 79 | + cout << s.isNumber("1 a") << endl; |
| 80 | + cout << s.isNumber("2e10") << endl; |
| 81 | + cout << s.isNumber(".1") << endl; |
| 82 | + cout << s.isNumber("3.") << endl; |
| 83 | + cout << s.isNumber(".") << endl; |
| 84 | + cout << s.isNumber("+.8") << endl; |
| 85 | + cout << s.isNumber("6e6.5") << endl; |
| 86 | + cout << s.isNumber(" 005047e+6") << endl; |
| 87 | + return 0; |
| 88 | +} |
0 commit comments