Guest User

Untitled

a guest
Jul 17th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include <cctype>
  2. #include <stdio.h>
  3.  
  4. class FILEinput {
  5. public:
  6. typedef unsigned char char_t;
  7. private:
  8. FILE *_f;
  9. char_t _c;
  10. public:
  11. FILEinput(FILE *f): _f(f) { nextc(); }
  12. char_t curc() const { return _c; }
  13. void nextc() { _c=fgetc(_f); }
  14. bool eof() const { return feof(_f); }
  15. };
  16.  
  17. class lineinput: public FILEinput {
  18. private:
  19. typedef FILEinput parent;
  20. unsigned _l;
  21. void cknl() { if('\n'==parent::curc()) ++_l; }
  22. public:
  23.  
  24. lineinput(FILE *f): parent(f), _l(1) { cknl(); }
  25. void nextc() { parent::nextc(); cknl(); }
  26.  
  27. struct error { const char *m; unsigned l;
  28. error(const char *s, unsigned n): m(s), l(n) { } };
  29. void expect(bool b,const char *m) { if(!b) throw error(m,_l); }
  30. void match(char_t c,const char *m) { expect(curc()==c,m); nextc(); }
  31. };
  32.  
  33. class parseinput: public lineinput {
  34. private:
  35. typedef lineinput parent;
  36. public:
  37. parseinput(FILE *f): parent(f) { }
  38.  
  39. void skipwhite() { while(isspace(curc())) nextc(); }
  40. unsigned readnumber() {
  41. unsigned u=0, c=0;
  42. expect(isdigit(curc()),"number");
  43. while(isdigit(curc())) {
  44. u=u*10+(curc()-'0'); nextc();
  45. expect(++c<9,"overlong number not");
  46. }
  47. return u;
  48. }
  49. };
  50.  
  51. int run() {
  52. parseinput p(stdin);
  53. for(;;) {
  54. p.skipwhite();
  55. if(p.eof()) break;
  56. printf("%u\n",p.readnumber());
  57. }
  58. return 0;
  59. }
  60.  
  61. int main(int,char **argv) {
  62. int r=-1;
  63. try {
  64. r=run();
  65. } catch(const lineinput::error &e) {
  66. fprintf(stderr,"%s: error on line %u: %s expected\n",argv[0],e.l,e.m);
  67. } catch(const char *e) {
  68. fprintf(stderr,"%s: error: %s\n",argv[0],e);
  69. } catch(...) {
  70. fprintf(stderr,"%s: uncaught exception\n",argv[0]);
  71. }
  72. return r;
  73. }
Add Comment
Please, Sign In to add comment