|
|
1.1 ! root 1: #include <stdio.h> ! 2: #include <assert.h> ! 3: #include <ctype.h> ! 4: #include "mprec.h" ! 5: ! 6: ! 7: /* ! 8: * Ibase is an int which contains the input base used by min. ! 9: * It should be between 2 and 16. Regardless of ibase, the ! 10: * legal digits are 0 - 9 and A - F. ! 11: */ ! 12: ! 13: int ibase = 10; ! 14: ! 15: ! 16: /* ! 17: * Min reads in a number in base ibase from stdin and sets "a" ! 18: * to that number. Note that a leading minus sign is allowed as ! 19: * are leading blanks. ! 20: */ ! 21: ! 22: void ! 23: min(a) ! 24: register mint *a; ! 25: { ! 26: register int ch; ! 27: register char mifl; /* leading minus flag */ ! 28: char cval; ! 29: mint c = {1, &cval}; ! 30: ! 31: assert(2 <= ibase && ibase <= 16); ! 32: /* throw away old value, and set to zero */ ! 33: mcopy(mzero, a); ! 34: ! 35: /* skip leading blanks */ ! 36: do { ! 37: ch = getchar(); ! 38: } while (isascii(ch) && isspace(ch)); ! 39: ! 40: /* check for leading minus */ ! 41: mifl = ch == '-'; ! 42: if (mifl) ! 43: do { ! 44: ch = getchar(); ! 45: } while (isascii(ch) && isspace(ch)); ! 46: ! 47: /* scan thru actual number, building result in a */ ! 48: while (okdigit(ch)) { ! 49: cval = valdigit(ch); ! 50: smult(a, ibase, a); ! 51: madd(a, &c, a); ! 52: ch = getchar(); ! 53: } ! 54: ungetc(ch, stdin); ! 55: ! 56: /* adjust sign of a */ ! 57: if (mifl) ! 58: mneg(a, a); ! 59: } ! 60: ! 61: ! 62: /* ! 63: * Okdigit returns true iff "ch" is a valid digit. This means ! 64: * iff it is a normal digit or one of the letters 'A' thru 'F'. ! 65: */ ! 66: ! 67: static ! 68: okdigit(ch) ! 69: register int ch; ! 70: { ! 71: return (isascii(ch) && isdigit(ch) || 'A'<=ch && ch<='F'); ! 72: } ! 73: ! 74: ! 75: /* ! 76: * Valdigit returns the numerical value for the character "ch" ! 77: * when it is interpreted as a digit. Note that valdigit assumes ! 78: * that "ch" has already been successfully tested by okdigit. ! 79: */ ! 80: ! 81: static ! 82: valdigit(ch) ! 83: register int ch; ! 84: { ! 85: return (isdigit(ch) ? ch+0-'0' : ch+0xA-'A'); ! 86: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.