这个题目很过分,情况太多了。。。
心累。。。
#include<bits/stdc++.h>
using namespace std;
bool isNumber(string s)
{
s.erase(0,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ")+1);
if(s[0]!='-'&&!(s[0]>='0'&&s[0]<='9')&&s[0]!='.'&&s[0]!='+')
return false;
else
{
int type = 0;
int count = 0;
if(s[0]=='.'&&s.length()==1)
return false;
if(s[0] == '.')
count++;
for(int i=1; i<s.length(); i++)
{
if(s[i]!='e'&&s[i]!='.'&&s[i]!='-'&&!(s[i]>='0'&&s[i]<='9'))
return false;
if(s[i]=='e')
{
if(type==1)
return false;
type = 1;
if(i==s.length()-1)
return false;
if(s[i-1]=='0'&&i==s.length()-1)
return false;
if(((s[i-1]>='0'&&s[i-1]<='9')||(s[i-1]=='.'&&i-1!=0))&&(s[i+1]>='0'&&s[i+1]<='9'))
i++;
else if(((s[i-1]>='0'&&s[i-1]<='9')||s[i-1]=='.'&&i-1!=0)&&s[i+1]=='-')
i++;
else if((s[i-1]>='0'&&s[i-1]<='9')&&s[i+1]=='+'&&i+1!=s.length()-1)
i++;
else
return false;
}
if(s[i]=='-')
{
if(s[i-1]!='e'||i==s.length()-1||!(s[i+1]>='0'&&s[i+1]<='9'))
return false;
}
if(s[i]=='.')
{
count++;
if(count!=1||type == 1)
return false;
if((s[i-1]>='0'&&s[i-1]<='9'))
{
if(s[0]=='.')
return false;
else
continue;
}else if(s[i-1]=='+'&&i-1==0&&i!=s.length()-1)
continue;
else if(s[i-1]=='-'&&i-1==0&&i!=s.length()-1)
continue;
else
return false;
}
}
return true;
}
}
int main()
{
string s;
cin>>s;
cout<<isNumber(s)<<endl;
}