剑指offer20

#include<iostream>
using namespace std;

class Solution {
public:
	bool checkInt(char **s) {
		if (**s == '-' || **s == '+') (*s)++;
		char *p = *s;
		while (*s!=NULL&&**s>='0'&& **s <= '9'){
			(*s)++;
		}
		return *s > p;
	}
	bool checkUnsignedInt(char **s) {
		char *p = *s;
		while (*s != NULL&&**s >= '0'&& **s <= '9') {
			(*s)++;
		}
		return *s > p;
	}
	bool isNumber(char *s) {
		if (s == NULL) return false;
		bool temp = false;
		temp = checkInt(&s);
		if (*s == '.') {
			s++;
			temp = temp || checkUnsignedInt(&s);
		}
		if (*s == 'E' || *s == 'e') {
			s++;
			temp = checkInt(&s)&&temp;
		}
		return temp&&*s=='\0';
	}
};

int main()
{
	Solution s;
	cout << (s.isNumber("-1e-16") == true) << endl;
	cout << (s.isNumber("-100") == true) << endl;
	cout << (s.isNumber(".122e") == true) << endl;
	cout << (s.isNumber("1.2.3") == true) << endl;
	return 0;
}



阅读更多
个人分类: 剑指offer
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭