zoukankan      html  css  js  c++  java
  • 请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+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;
    }
    
    注意:这个用到了正则表达式!!!!!!!!!!
          我的这个方法不好!!!!!!!!
          要熟悉正则表达式!!!!!!
  • 相关阅读:
    P1966 火柴排队
    其实,我可以假装这篇随笔的名字很长,很长
    通用技术课的准备
    浅谈高压线除冰技术
    浅谈发电厂的环保问题
    本地MD搬运
    高考作文专题
    奇奇怪怪的错误收集 (鸽)
    2019CSP-S2专题
    模拟赛 ——“与” 小象涂色 行动!行动!
  • 原文地址:https://www.cnblogs.com/wdan2016/p/6031880.html
Copyright © 2011-2022 走看看