/* ** Read a number: first reads a valid prefix of a numeral into a buffer. ** Then it calls 'lua_stringtonumber' to check whether the format is ** correct and to convert it to a Lua number */ static int read_number (lua_State *L, FILE *f) { RN rn; int count = 0; int hex = 0; char decp[2]; rn.f = f; rn.n = 0; decp[0] = lua_getlocaledecpoint(); /* get decimal point from locale */ decp[1] = '.'; /* always accept a dot */ l_lockfile(rn.f); do { rn.c = l_getc(rn.f); } while (isspace(rn.c)); /* skip spaces */ test2(&rn, "-+"); /* optional signal */ if (test2(&rn, "00")) { if (test2(&rn, "xX")) hex = 1; /* numeral is hexadecimal */ else count = 1; /* count initial '0' as a valid digit */ } count += readdigits(&rn, hex); /* integral part */ if (test2(&rn, decp)) /* decimal point? */ count += readdigits(&rn, hex); /* fractional part */ if (count > 0 && test2(&rn, (hex ? "pP" : "eE"))) { /* exponent mark? */ test2(&rn, "-+"); /* exponent signal */ readdigits(&rn, 0); /* exponent digits */ } ungetc(rn.c, rn.f); /* unread look-ahead char */ l_unlockfile(rn.f); rn.buff[rn.n] = '\0'; /* finish string */ if (lua_stringtonumber(L, rn.buff)) /* is this a valid number? */ return 1; /* ok */ else { /* invalid format */ lua_pushnil(L); /* "result" to be removed */ return 0; /* read fails */ } }
void main(int argc, char *argv[]) { FILE *fp_infile = fopen(argv[1], "r"), *fp_outfile = fopen(argv[2], "w"); int digits[MAXLENGTH]; getdigits(digits, fp_infile); readdigits(digits); insertsort(digits); readdigits(digits); savedigits(digits, fp_outfile); }
bool FuzzyStringComparator::StreamElement_::tryExtractDouble(InputLine& input_line, double& target) { char c_peek; std::string buffer; if (input_line.line_.eof()) return false; c_peek = input_line.line_.peek(); // .99 bool have_seen_decimal_separator = false; // read sign or first number, otherwise abort if ((c_peek == '+' || c_peek == '-' || isdigit(c_peek) || c_peek == '.') && c_peek != EOF) { buffer.push_back(c_peek); c_peek = input_line.line_.get(); have_seen_decimal_separator = (c_peek == '.'); } else { return false; } // try to accumulate more numbers readdigits(input_line, buffer, c_peek); // if we have no decimal separator, return what we already accumulated if (c_peek != '.' && !have_seen_decimal_separator) { // have we accumulated more then +/- if (buffer.size() > 1 || isdigit(buffer[0])) { target = boost::lexical_cast<double>(buffer); return true; } else { return false; } } else if (!have_seen_decimal_separator) { buffer.push_back(c_peek); c_peek = input_line.line_.get(); // try to read the rest of the float readdigits(input_line, buffer, c_peek); } // check if the number is followed by an e+/- something if (c_peek == 'e' || c_peek == 'E') { // add char to buffer buffer.push_back(c_peek); input_line.line_.get(); // get next char c_peek = input_line.line_.peek(); // check if we have a sign if (c_peek == '+' || c_peek == '-') { buffer.push_back(c_peek); c_peek = input_line.line_.get(); } // try to accumulate the rest of scientific notation if (readdigits(input_line, buffer, c_peek)) { target = boost::lexical_cast<double>(buffer); return true; } else { return false; } } else // just a simple floating point number, convert { // have we accumulated more then +/- if (buffer.size() > 1 || isdigit(buffer[0])) { target = boost::lexical_cast<double>(buffer); return true; } else { // not enough content for cast return false; } } }