diff --git a/miscmaths.cc b/miscmaths.cc index e83d17d5ca7342da708b9b0e0e2d04b1d2796b68..6f303fa2ec2f7391104d5d768c36eb3f85d5512e 100644 --- a/miscmaths.cc +++ b/miscmaths.cc @@ -56,17 +56,15 @@ namespace MISCMATHS { } } - // General string/IO functions - - bool isnum(const string& str) + bool isNumber( const string& input) { - // assumes that initial whitespace has been removed - if (isdigit(str[0])) return true; - if ( (str[0]=='-') || (str[0]=='+') || (str[0]=='.') ) return true; - return false; - } - + char *pend; + strtod(input.c_str(),&pend); + if (*pend!='\0') return false; + return true; + } + string skip_alpha(ifstream& fs) { string cline; @@ -77,7 +75,7 @@ namespace MISCMATHS { istringstream ss(cline.c_str()); string cc=""; ss >> cc; - if (isnum(cc)) { + if (isNumber(cc)) { if (!fs.eof()) { fs.seekg(curpos); } else { fs.clear(); fs.seekg(0,ios::beg); } return cline; } @@ -124,7 +122,7 @@ namespace MISCMATHS { for (int c=1; c<=ncols; c++) { if (!fs.eof()) { fs >> ss; - while ( !isnum(ss) && !fs.eof() ) { + while ( !isNumber(ss) && !fs.eof() ) { fs >> ss; } mat(r,c) = atof(ss.c_str()); @@ -177,7 +175,7 @@ namespace MISCMATHS { istringstream ss(cline.c_str()); string cc=""; ss >> cc; - if (!isnum(cc)) break; // stop processing when non-numeric line found + if (!isNumber(cc)) break; // stop processing when non-numeric line found rcount++; // add new row to matrix } while (!fs.eof()); diff --git a/miscmaths.h b/miscmaths.h index 234dbbad5af1ec19f4369fa5424fde7396a7a98a..d3e44ec016880547904b27550a3e6b64fd433d16 100644 --- a/miscmaths.h +++ b/miscmaths.h @@ -49,7 +49,7 @@ namespace MISCMATHS { #endif string size(const Matrix& mat); - bool isnum(const string& str); + bool isNumber(const string& str); ReturnMatrix read_ascii_matrix(const string& filename, int nrows, int ncols); ReturnMatrix read_ascii_matrix(int nrows, int ncols, const string& filename); ReturnMatrix read_ascii_matrix(const string& filename);