Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cctype>
- #include <stdio.h>
- class FILEinput {
- public:
- typedef unsigned char char_t;
- private:
- FILE *_f;
- char_t _c;
- public:
- FILEinput(FILE *f): _f(f) { nextc(); }
- char_t curc() const { return _c; }
- void nextc() { _c=fgetc(_f); }
- bool eof() const { return feof(_f); }
- };
- class lineinput: public FILEinput {
- private:
- typedef FILEinput parent;
- unsigned _l;
- void cknl() { if('\n'==parent::curc()) ++_l; }
- public:
- lineinput(FILE *f): parent(f), _l(1) { cknl(); }
- void nextc() { parent::nextc(); cknl(); }
- struct error { const char *m; unsigned l;
- error(const char *s, unsigned n): m(s), l(n) { } };
- void expect(bool b,const char *m) { if(!b) throw error(m,_l); }
- void match(char_t c,const char *m) { expect(curc()==c,m); nextc(); }
- };
- class parseinput: public lineinput {
- private:
- typedef lineinput parent;
- public:
- parseinput(FILE *f): parent(f) { }
- void skipwhite() { while(isspace(curc())) nextc(); }
- unsigned readnumber() {
- unsigned u=0, c=0;
- expect(isdigit(curc()),"number");
- while(isdigit(curc())) {
- u=u*10+(curc()-'0'); nextc();
- expect(++c<9,"overlong number not");
- }
- return u;
- }
- };
- int run() {
- parseinput p(stdin);
- for(;;) {
- p.skipwhite();
- if(p.eof()) break;
- printf("%u\n",p.readnumber());
- }
- return 0;
- }
- int main(int,char **argv) {
- int r=-1;
- try {
- r=run();
- } catch(const lineinput::error &e) {
- fprintf(stderr,"%s: error on line %u: %s expected\n",argv[0],e.l,e.m);
- } catch(const char *e) {
- fprintf(stderr,"%s: error: %s\n",argv[0],e);
- } catch(...) {
- fprintf(stderr,"%s: uncaught exception\n",argv[0]);
- }
- return r;
- }
Add Comment
Please, Sign In to add comment