/** * ints - extract all the integers from standard input and print them * one per line. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include // For printf, getchar, and putchar #include // For isdigit // +-------+---------------------------------------------------------- // | Types | // +-------+ /** * The states of our program. We can either be inside our outside * of a number. */ enum state { IN, OUT }; // +------+----------------------------------------------------------- // | Main | // +------+ int main () { int ch; // An input character int sign = 0; // The sign, if there is one. enum state state = OUT; // The state of input; initially outside. // Repeatedly read and process characters while ((ch = getchar ()) != EOF) { // Is it a digit? If so, print it out and note that we're now // in a number. if (isdigit (ch)) { // Print out the sign, if one is waiting if (sign) { putchar (sign); sign = 0; } // Print it out putchar (ch); // And note that we're in a number. state = IN; } // if isdigit(ch) // Is it a plus or minus sign? If so, remember it. else if ((ch == '+') || (ch == '-')) sign = ch; // Otherwise, it's neither a digit or a sign. If we were in a // number, we should print a newline. else if (state == IN) { putchar ('\n'); state = OUT; sign = 0; } // leaving a number // It's not a digit or a sign, and we're not in a number. Clear // any signs that are set. else sign = 0; } // while // And we're done return 0; } // main