编译课程实验-识别浮点常量问题

时间:2022-09-09 14:50:15

来组测试数据:

1  NO

1 .NO

.2 NO

1.2e NO

1.2e1 YES

e1 NO

1e2.2 NO

+1.2e+44 YES

++1.2 NO

这样应该把所有的情况包括了吧

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main()
{
string s;
while(cin>>s)
{
int len = s.length();
int x = 0, e = 0, n = 0;
for(int i = 0; i < len; i++)
{
if(s[i] == '.')
{
x++;
if(e>0)
x = 2;
}
else if(s[i] == 'e' || s[i] == 'E')
{
e++;
if(i == 0 || i == len)
{
e = 2;
break;
}
else
{
if(s[i-1]>='0' && s[i-1] <='9' && s[i+1] >='0' && s[i+1] <='9')
{

}
else
{
if((s[i+1] == '+' || s[i+1] == '-')&&i+2<len&&s[i+2]>='0' && s[i+2] <='9')
{

}
else
{
e = 2;
break;
}
}
}

}
else if(s[i] == '+' || s[i] == '-')
{
if(i == 0 || (i > 0 && (s[i-1] == 'e' || s[i-1] =='E') ))
{

}
else
n++;
}
}
if((x == 0 && e == 0) || (x > 1) || (e > 1) || n > 0)
{
cout<<"NO"<<endl;
}
else if(x == 1)
{
for(int i = 0; i < len ; i++)
{
if(s[i] == '.')
{
if(i > 0 && s[i-1] >='0' && s[i] <= '9' && i+1 <len && s[i+1] >='0' && s[i+1] <= '9')
{
if(i-1==0)
{
if(s[i-1] >= '0' && s[i-1] <='9')
{
cout<<"YES"<<endl;
}
else
cout<<"NO"<<endl;
}
else
cout<<"YES"<<endl;
}
else
cout<<"NO"<<endl;
}
}
}
else
{
cout<<"YES"<<endl;
}


}
return 0;
}