请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。

// test20.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cstring>
#include<string.h>
#include<deque>
#include <forward_list>

using namespace std;

class Solution {
public:
	bool isNumeric(char* str)
	{
		string s = str;
		//如果存在不合法字符
		if (s.find_first_of("eE") > s.size())
		{
			if (s.find_first_of("+-") > 0&&s.find_first_of("+-")<s.size())
				return false;
		}
			
		if (s.find_first_of("abcdfghijklmnopqrstuvwxyz") < s.size())
			return false;
		if (s.find_first_of("e") == s.size() - 1)
			return false;
		//不存在e,但是有两个正号或者符号
		//找到了E,但是eE后面有.
		if(s.find_first_of(".")<s.size())
		  if (s.find_first_of("eE") < s.find_first_of("."))
			return false;
		
		int first = s.find_first_of("+-");
		int last = s.find_last_of("+-");
		if (s.find_first_of("eE")>s.size()&&first != last) return false;
		int first1 = s.find_first_of(".");
		int last1 = s.find_last_of(".");
		if ( first1 != last1) return false;
	//	cout << s << endl;
		return true;
	}

};
int main()
{
	
	Solution so;
//	vector<int> numbers = { 1,2,3,4,5 };
 /*   bool result=so.IsContinuous(numbers);
	cout <<"result:"<< result << endl;*/
	char* str = "1.79769313486232E+308";

	bool result = so.isNumeric(str);
	cout << "result:" << result << endl;

	cout << endl;
	return 0;
}

注意:这个用到了正则表达式!!!!!!!!!!
      我的这个方法不好!!!!!!!!
      要熟悉正则表达式!!!!!!
posted @ 2016-11-04 22:14  wdan2016  阅读(926)  评论(0编辑  收藏  举报