|
|
1.1 ! root 1: /* ! 2: * Factor prints out the prime factorization of numbers. ! 3: * If there are any arguments, then it factors these. If ! 4: * there are no arguments, then it reads stdin until ! 5: * either EOF or the number zero or a non-numeric ! 6: * non-white-space character. Since factor does all of ! 7: * its calculations in double format, the largest number ! 8: * which can be handled is quite large. ! 9: */ ! 10: #include <stdio.h> ! 11: #include <math.h> ! 12: #include <ctype.h> ! 13: ! 14: ! 15: #define NUL '\0' ! 16: #define ERROR 0x10 /* largest input base */ ! 17: #define MAXNUM 200 /* max number of chars in number */ ! 18: ! 19: ! 20: main(argc, argv) ! 21: int argc; ! 22: register char *argv[]; ! 23: { ! 24: register char *chp; ! 25: double n; ! 26: double atod(); ! 27: char *getnum(); ! 28: ! 29: if (argc != 1) ! 30: while ((chp=*++argv) != NULL && ! 31: (n=atod(chp)) != 0) ! 32: factor(n); ! 33: else ! 34: while ((chp=getnum()) != NULL && ! 35: (n=atod(chp)) != 0) ! 36: factor(n); ! 37: return (0); ! 38: } ! 39: die(str) ! 40: char *str; ! 41: { ! 42: fprintf(stderr, "%r\en", &str); ! 43: exit(1); ! 44: } ! 45: ! 46: usage() ! 47: { ! 48: die("usage: factor [number number ...]"); ! 49: } ! 50: ! 51: ! 52: char * ! 53: getnum() ! 54: { ! 55: register char *chp, ! 56: ch; ! 57: static char res[MAXNUM+1]; ! 58: ! 59: do { ! 60: ch = getchar(); ! 61: } while (isascii(ch) && isspace(ch)); ! 62: if (!isascii(ch) || todigit(ch) == ERROR) ! 63: return (NULL); ! 64: for (chp=res; isascii(ch) && !isspace(ch); ! 65: ch=getchar()) ! 66: if (chp < &res[MAXNUM]) ! 67: *chp++ = ch; ! 68: if (chp >= &res[MAXNUM]) ! 69: die("Number too big"); ! 70: *chp++ = NUL; ! 71: return (res); ! 72: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.