home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / croutes.zip / LEJ_LIB.C < prev    next >
Text File  |  1984-07-29  |  6KB  |  302 lines

  1. /* Larry Jordan's sample c program functions */
  2.  
  3. #include <stdio.h>                /* std I/O library on c disk */
  4.  
  5. #define EOF -1                    /* logical ^Z EOF ends copy */
  6. #define YES 1
  7. #define NO 0
  8. #define YES 1
  9. #define NO 0
  10. #define NL '\n'
  11. #define EOS '\0'
  12. #define MAXLINE 1024
  13. #define FF '\012'                               /* ASCII Form feed */
  14. #define DIGMAX 10
  15.  
  16. int inword;
  17. int nl;
  18. int nw;
  19. int nc;
  20. int ndigits[DIGMAX];
  21. int c;
  22.  
  23.  
  24.  
  25.  
  26. /* ---------------------------------------------------------------------- */
  27.  
  28.  
  29. main()                      /*                    */
  30. {
  31.  
  32.     int len;            /* current line length */
  33.     int max;            /* max length so far */
  34.     char line[MAXLINE];
  35.     char save[MAXLINE];
  36.     char lineupper[MAXLINE];
  37.     char linelower[MAXLINE];
  38.  
  39.     max = 0;
  40.  
  41.     printf("\n");
  42.     printf("\n");
  43.     printf("Jordan's function test ...\n");
  44.     printf("\n");
  45.     printf("Input text for test.\n");
  46.     printf("\n");
  47.  
  48.  
  49.     while ((len = getline(line, MAXLINE)) > 0)   /* getline defined below */
  50.         printf("\nLine = %s\n", line);
  51.  
  52.  
  53.  
  54. }
  55.  
  56.  
  57.  
  58. /* ---------------------------------------------------------------------- */
  59.  
  60.  
  61. redirect()                       /* redirect keybd to CRT */
  62. {
  63.     int c;
  64.  
  65.     printf("\n");
  66.     printf("\n");
  67.     printf("Jordan's redirect function...\n");
  68.     printf("\n");
  69.     printf("Input numbers, space characters, etc...\n");
  70.     printf("?");
  71.  
  72.  
  73.     while ((c = getchar()) != EOF)           /* get char until EOF found */
  74.         putchar(c);               /* send char to scrn: */
  75. }
  76.  
  77.  
  78. /* ---------------------------------------------------------------------- */
  79.  
  80.  
  81.  
  82. wordcnt()                  /* count lines, words, chars in input */
  83. {
  84.  
  85.     double c, nc, nl, nw;                  /* counters are float */
  86.     int inword;                      /* logical is interger */
  87.  
  88.     inword = NO;
  89.     nl = nw = nc = 0.0;                  /* initialize variables */
  90.  
  91.     printf("\n");
  92.     printf("\n");
  93.     printf("Jordan's word count function ...\n");
  94.     printf("\n");
  95.     printf("Input text for character, word and line count search.\n");
  96.     printf("\n");
  97.  
  98.  
  99.     while ((c = getchar()) != EOF) {
  100.         nc = nc + 1;
  101.  
  102.         if (c == '\n')                         /* if c=LF then inc nl */
  103.              nl = nl + 1;
  104.  
  105.         if (c == ' ' || c == '\n' || c == '\t')  /* if char is space, */
  106.              inword = NO;            /* LF or TAB then it */
  107.                             /* cannot be a word */
  108.  
  109.         else if (inword == NO) {        /* otherwise it is a */
  110.              inword = YES;            /* word */
  111.              nw = nw + 1;
  112.         }
  113.     }
  114.     printf("\n");
  115.     printf("\n");
  116.     printf("Number of characters = ");
  117.     printf("%.0f\n",nc);                       /* prt float # with no dec */
  118.     printf("Number of words = ");
  119.     printf("%.0f\n",nw);
  120.     printf("Number of lines = ");
  121.     printf("%.0f\n",nl);
  122.  
  123.     printf("\n");
  124. }
  125.  
  126.  
  127. /* ---------------------------------------------------------------------- */
  128.  
  129.  
  130. maxline()            /* gets maximum length line in file */
  131. {
  132.     int len;            /* current line length */
  133.     int max;            /* max length so far */
  134.     char line[MAXLINE];
  135.     char save[MAXLINE];
  136.  
  137.     max = 0;
  138.  
  139.     printf("\n");
  140.     printf("\n");
  141.     printf("Jordan's get maximum length line function ...\n");
  142.     printf("\n");
  143.     printf("Input text for maximum line length search.\n");
  144.     printf("\n");
  145.  
  146.  
  147.     while ((len = getline(line, MAXLINE)) > 0)   /* getline defined below */
  148.         if (len > max){
  149.             max = len;
  150.             copy(line, save);           /* function from below */
  151.         }
  152.     if (max > 0)
  153.         printf("%s", save);
  154. }
  155.  
  156.  
  157. /* ---------------------------------------------------------------------- */
  158.  
  159.  
  160. getline(s, lim)         /* getline into s, return length of s */
  161. char s[];
  162. int lim;
  163. {
  164.     int c,i;
  165.  
  166.     for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
  167.         s[i] = c;
  168.     if (c == '\n'){
  169.         s[i] = c;
  170.         ++i;
  171.     }
  172.     s[i] = '\0';
  173.     return(i);
  174. }
  175.  
  176.  
  177.  
  178. /* ---------------------------------------------------------------------- */
  179.  
  180.  
  181. copy(s1, s2)            /* copy s1 to s2; assume s2 big enough */
  182. char s1[], s2[];
  183. {
  184.     int i;
  185.  
  186.     i = 0;
  187.     while (s2[i] = s1[i] != EOS)
  188.         ++i;
  189. }
  190.  
  191.  
  192.  
  193.  
  194. /* ---------------------------------------------------------------------- */
  195.  
  196.  
  197. power(x, n)                /* raise x to n-th power; n > 0 */
  198. int x, n;
  199. {
  200.     int i, p;
  201.  
  202.     p = 1;
  203.     for (i=1; i<=n; ++i)
  204.         p=p*x;
  205.     return(p);
  206. }
  207.  
  208.  
  209.  
  210. /* ---------------------------------------------------------------------- */
  211.  
  212.  
  213. digicnt()                 /* count digits, spaces, others */
  214. {
  215.     int c, i, nspaces, nother;     /* integers */
  216.     int ndigits[DIGMAX];         /* integer array */
  217.  
  218.     nspaces = nother = 0;
  219.     for (i= 0; i < 10; ++i)
  220.         ndigits[i] = 0;      /* initialize arry to 0 */
  221.  
  222.  
  223.     printf("\n");
  224.     printf("\n");
  225.     printf("Jordan's number counter...\n");
  226.     printf("\n");
  227.     printf("Input numbers, space characters, etc...\n");
  228.     printf("?");
  229.  
  230.     while ((c = getchar()) != EOF)
  231.         if (c >= '0' && c <= '9')
  232.               ++ndigits[c];
  233.         else if (c == ' ' || c == '\n' || c == '\t')
  234.             ++nspaces;
  235.         else
  236.             ++nother;
  237.  
  238.  
  239.  
  240.     printf("\n");
  241.     printf("\n");
  242.     printf("Jordan's number counter results...\n");
  243.     printf("\n");
  244.     printf("Number of digits = ");
  245.     for (i=0; i < 10; ++i)
  246.            printf(" %d", ndigits[i]);
  247.     printf("\nNumber of spaces = %d,  Other = %d\n",
  248.            nspaces, nother);
  249.     printf("\n");
  250. }
  251.  
  252.  
  253.  
  254. /* ---------------------------------------------------------------------- */
  255.  
  256. strlen(s)                /*  return length of string s */
  257. char s[];
  258. {
  259.     int i;
  260.  
  261.     i = 0;
  262.     while (s[i] != '\0')
  263.         ++i;
  264.     return(i);
  265. }
  266.  
  267.  
  268. /* ---------------------------------------------------------------------- */
  269.  
  270. atoi(s)             /* convert numeric string into numeric equiv */
  271. char s[];
  272. {
  273.  
  274.     int i;
  275.     int n;              /* need double precision for no > 32K */
  276.  
  277.     n = 0;
  278.     for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
  279.           n = 10 * n + s[i] - '0';
  280.  
  281.     return(n);
  282. }
  283.  
  284.  
  285. /* ---------------------------------------------------------------------- */
  286.  
  287.  
  288.  
  289.  
  290. isdigit(c)            /* test character to see if it is a digit */
  291. int c;
  292. {
  293.     int isdigit;
  294.  
  295.     return(isdigit = c >= '0' && c<= '9');  /* isdigit true = 1 */
  296.                         /* isdigit false = 0 */
  297.  
  298. }
  299.  
  300.  
  301. /* ---------------------------------------------------------------------- */
  302.