perso, je le ferai avec ma lib BString,
fonction
checkIsNbr
appel pour ton cas
BString plomp(petibuf);
bool ok=plomp.checkIsNbr(true,false,true);
corps de la fonction
bool BString::checkIsNbr(const bool uniquenmentDecimal,const bool nombreAvecSuffixeC,const bool bNegatifOk)const
{
/// --------------------------------------------------------------------------------------------------------------------------------------------------
/// ---------------- BString::checkIsNbr(const bool uniquenmentDecimal ,const bool nombreAvecSuffixeC ,const bool bNegatifOk) -> bool ----------------
/// --------------------------------------------------------------------------------------------------------------------------------------------------
/// ----- Objectif : indique si une chaîne est un nombre (quelque soit son format)
/// ----- Auteur(s) : Magic Nono 23/09/03
/// ----- PreCond : /
/// ----- PostCond : /
/// ----- Etat : 2 (-1<0<1<2)
/// ----- MaJ 11/10/03 : reco hexa
/// ----- MaJ 03/11/03 : reco octal
/// ----- MaJ 21/06/04 : reco optionnel nb avec suffixe éventuel(1 lettre : 'f' , 'l' ou 'U')
/// ----- MaJ 13/12/04 : reco optionnelle des négatifs
/// ----- MaJ 29/01/05 : corr BUG : 'e1' est une variable !! => un nb ne peut commencer par 'e'
/// ----- MaJ 05/04/05 : corr BUG : '.' doit etre unique... ou 2* max avec notation scientifique
/// ----- TODO : binaire
/// ----- TODO 2 : scientifique avec négatif...
/// ----- TOCHECK : "1..2" , "1.2.3"
/// --------------------------------------------------------------------------------------------------------------------------------------------------
/// ----- const bool uniquenmentDecimal(par défaut : 'true') : uniquement les décimaux tolérés?
/// ----- const bool nombreAvecSuffixeC(par défaut : 'false') : reco optionnel nb avec suffixe éventuel(1 lettre : 'f' , 'l' ou 'U')
///+ f: float, U: unsigned, l:long
/// ----- const bool bNegatifOk(par défaut : 'false') : valider les nombres négatifs
/// --------------------------------------------------------------------------------------------------------------------------------------------------
/// ----- retour (bool) : cf.obj
/// --------------------------------------------------------------------------------------------------------------------------------------------------
/// ----- Var Utilisées de la classe (2) : m_iTaille, m_str
/// ----- Var Utilisées par adresse (1) : m_str
/// ----- Var In (5) : bNegatifOk, m_iTaille, m_str, nombreAvecSuffixeC, uniquenmentDecimal
size_t i;
char c='²';
bool ePresent=false; // 'e' present : exposant...
bool ptPresent=false; // '.' present : virgule...
//si pas hexa
if(!(m_iTaille>2 && m_str[0]=='0'&&m_str[1]=='x'))
{
//si octal
if(m_iTaille>=1 && m_str[0]=='O')
{
if(uniquenmentDecimal)
return false;
//si 'O' => faux
if(m_iTaille==1)
return false;
/// algo : retourne nb bien octal (*)
for(i=1;i<m_iTaille;i++)
{
c=m_str[i];
if(c<'0'||c>'7')
return false;
}
return true;
}
/// algo : retourne bien un nombre (exp OK) (*)
i=0;
if(bNegatifOk && 2<=m_iTaille && m_str[0]=='-')
i++;
for(;i<m_iTaille;i++)
{
c=m_str[i];
if(c=='e' && !ePresent)
{
if(i==0) //cas ou on commence par e nok
break;
ePresent=true;
ptPresent=false;
continue; //goto 'for'
}
//rq : (!isdigit(c)) <=> (c<'0'||c>'9')
if(c<'0'||c>'9')
if(c=='.' && !ptPresent)
ptPresent=true;
else
return(nombreAvecSuffixeC && i==m_iTaille-1 && m_iTaille>1 && (c=='U' || c=='f' || c=='l' ));
}
return(c!='e'); // normalement true, & on ne commence pas ni ne finit avec un 'e' !!!
}
else
{
if(uniquenmentDecimal)
return false;
/// algo : {hexa}
/// algo : retourne bien un nombre Hexa (*)
for(i=2;i<m_iTaille;i++)
{
c=m_str[i];
if((c<'0'||c>'9') && (c<'A'||c>'F') && (c<'a'||c>'f'))
return false;
}
return true;
}
}
___________________________________________________________
MagicalementNono