Answer to Exercise 70

| ANSWER TO EXERCISE 70
| First we must decide how to store floating-point numbers internally: in this case we'll simply store 100x to represent x, so that "5.46'' will be parsed as 546.
    [ DigitNumber n type x;
      x = NextWordStopped(); if (x==-1) return -1; wn--;
      if (type==0)
      {   x = WordAddress(wn);
          if (x->n>='0' && x->n<='9') return (x->n) - '0';
          return -1;
      }
      if (x=='nought' or 'oh') { wn++; return 0; }
      x = TryNumber(wn++); if (x==-1000  x>=10) x=-1; return x;
    ];
    [ FloatingPoint a x b w d1 d2 d3 type;
      a = TryNumber(wn++);
      if (a==-1000) return -1;
      w = NextWordStopped(wn); if (w==-1) return a*100;
      x = NextWordStopped(wn); if (x==-1) return -1; wn--;
      if (w=='point') type=1;
      else
      {   if (WordAddress(wn-1)->0~='.'  WordLength(wn-1)~=1)
              return -1;
      }
      d1 = DigitNumber(0,type);
      if (d1==-1) return -1;
      d2 = DigitNumber(1,type); d3 = DigitNumber(2,type);
      b=d1*10; if (d2>=0) b=b+d2; else d3=0;
      if (type==1)
      {   x=1; while (DigitNumber(x,type)>=0) x++; wn--;
      }
      else wn++;
      parsed_number = a*100 + b;
      if (d3>=5) parsed_number++;
      return 1;
    ];

Back to the exercise in section 27

Mechanically translated to HTML from third edition as revised 16 May 1997. Copyright © Graham Nelson 1993, 1994, 1995, 1996, 1997: all rights reserved.