#include #include typedef std::string Word; typedef std::list Z; typedef bool (Predicate) (const Word&); enum IOState { Norm, Abnorm }; #include class X { std::istream &stream; public: X (std::istream &stream_); void Open (); IOState Read(char &c); }; X::X(std::istream &stream_): stream(stream_) { } void X::Open() { } IOState X::Read(char &dx) { stream.get(dx); return stream.eof() ? Abnorm : Norm; } class Y { IOState sx; char dx; X &x; public: Y(X &x); void Open (); IOState Read(Word &dy); private: static bool Whitespace(char c); }; Y::Y(X &x_): x(x_) { sx = x.Read(dx); while (sx == Norm && Whitespace(dx)) sx = x.Read(dx); } void Y::Open() { x.Open (); } IOState Y::Read(Word &dy) { if (sx != Norm) return Abnorm; dy = ""; while (sx == Norm && !Whitespace(dx)) { dy += dx; sx = x.Read(dx); } while (sx == Norm && Whitespace(dx)) { sx = x.Read(dx); } return Norm; } bool Y::Whitespace(char c) { return (c == ' ') || (c == '\t') || (c == '\n'); } void Filter(Y &y, Predicate pi, Z &z) { z.clear (); IOState sy; Word dy; y.Open (); sy = y.Read(dy); while (sy == Norm) { if (pi(dy)) z.push_back (dy); sy = y.Read(dy); } } bool Begins_with_G(const Word &word) { return word[0] == 'G'; } void Process (std::istream &stream) { X x(stream); Y y(x); Z z; Filter(y, Begins_with_G, z); for (Z::const_iterator i = z.begin(); i != z.end(); ++i) std::cout << *i << std::endl; } #include void Process (const std::string &filename) { if (filename == "-") { Process (std::cin); } else { std::ifstream stream (filename.c_str ()); if (stream.fail ()) std::cerr << "Opening " << filename << " failed." << std::endl; else Process (stream); } } int main (int argc, char **argv) { if (argc > 1) for (int i = 1; i != argc; ++i) Process (argv[i]); else Process (std::cin); return 0; }