mylib.c |
| #include <stdlib.h> #include "mylib.h" void *emalloc(size_t s){ void *result = malloc(s); if(NULL == result){ fprintf(stderr, "memory allocation failed.\n"); exit(EXIT_FAILURE); } return result; } void *erealloc(void *p, size_t s){ void *result = realloc(p,s); if(NULL == result){ fprintf(stderr, "memory allocation failed.\n"); exit(EXIT_FAILURE); } return result; } int getword(char *s, int limit, FILE *stream){ int c; char *w = s; while(!isalnum( c = getc(stream)) && c != EOF); if(c == EOF) return EOF; else *w++ = tolower(c); while(--limit > 0){ if(isalnum(c = getc(stream))) *w++ = tolower(c); else if('\'' == c) continue; else break; } *w = '\0'; return s[0]; } int stoi(char *s){ int i,j,r; i=1; r=0; for(j=my_strlen(s)-1; j>=0; j--){ r+=((s[j]-'0')*i); i*=10; } return r; } int my_strlen(char *s){ int i=0; while(s[i]!='\0') i++; return i; } static int factors(int x){ int f = 2; while(f*f < x){ if(x % f == 0){ return 0; } else { f++; } } return 1; } static int prime_gt(int s){ int bound = s; while(bound > 0){ if(factors(bound)) break; else bound++; } return bound; } static int prime_lt(int s){ int bound = s; while(bound > 0){ if(factors(bound)) break; else bound--; } return bound; } unsigned int relative_prime(int s){ return prime_gt(s); } |
James Little |