home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 160_01 / sources < prev    next >
C/C++ Source or Header  |  1985-11-26  |  35KB  |  1,745 lines

  1. ###asst.c
  2. /* EXAMPLES OF ASSIGNMENT
  3.  * (No output is produced)
  4.   */
  5.  main()
  6.      {
  7.      char c;
  8.      short i;
  9.  
  10.      c = 'A';
  11.     i = 65;
  12.     c = 'X';
  13.     i = -4;
  14.     }
  15. ###asst2.c
  16. /* asst2 - print assigned values
  17.  */
  18. main()
  19.     {
  20.     char c;
  21.     short i;
  22.  
  23.     c = 'A';
  24.     i = 65;
  25.     printf("c: dec=%d oct=%o hex=%x ASCII=%c\n",
  26.          c, c, c, c);
  27.     printf("i: dec=%d oct=%o hex=%x unsigned=%u\n",
  28.          i, i, i, i);
  29.     c = 'X';
  30.     i = -4;
  31.     printf("c: dec=%d oct=%o hex=%x ASCII=%c\n",
  32.          c, c, c, c);
  33.     printf("i: dec=%d oct=%o hex=%x unsigned=%u\n",
  34.          i, i, i, i);
  35.     }
  36. ###badpow.c
  37. /* badpow - demonstrate power function (argument mismatch error)
  38.  */
  39. #include "local.h"
  40. #include <math.h>
  41. main()
  42.     {
  43.     short i;
  44.  
  45.     for (i = 0; i < 10; ++i)
  46.         printf("2 to the power %d equals %.0f\n",
  47.             i, pow(2, i));
  48.     exit(SUCCEED);
  49.     }
  50. ###bdrill.c
  51. /* bdrill - binary arithmetic practice
  52.  */
  53. #include "local.h"
  54. #include <ctype.h>
  55. main()
  56.     {
  57.     short a, b;
  58.     short getbin();
  59.     void putbin();
  60.  
  61.     while ((a = getbin()) != 0 && (b = getbin()) != 0)
  62.         {
  63.         printf("\n    a = ");
  64.         putbin(a);
  65.         printf("\n    b = ");
  66.         putbin(b);
  67.         printf("\na + b = ");
  68.         putbin(a + b);
  69.         printf("\na & b = ");
  70.         putbin(a & b);
  71.         printf("\na | b = ");
  72.         putbin(a | b);
  73.         printf("\na ^ b = ");
  74.         putbin(a ^ b);
  75.         printf("\n");
  76.         }
  77.     }
  78. <:IT:>(<:fC:>getbin<:IT:> function goes here)<:fC:>
  79. <:IT:>(<:fC:>putbin<:IT:> function goes here)<:fC:>
  80. ###bits.c
  81. /* bits - examples of bitwise operations
  82.  */
  83. #include "local.h"
  84. main()
  85.     {
  86.     bits b1, b2;
  87.  
  88.     b1 = 0xF0F0 & 0x1234;
  89.     b2 = b1 | 0x60;
  90.     printf("b1=0x%04x, b2=0x%04x\n", b1, b2);
  91.     b1 = ~1 & 0307;
  92.     b2 = (bits)b1 >> 2;
  93.     printf("b1=0%03o, b2=0%03o\n", b1, b2);
  94.     b1 = 0xF001 | 0x8801;
  95.     b2 = b1 & 0xB800;
  96.     printf("b1=0x%04x, b2=0x%04x\n", b1, b2);
  97.     }
  98. ###blast.c
  99. /* blast - print countdown
  100.  */
  101. main()
  102.     {
  103.     short n;
  104.  
  105.     for (n = 10; n >= 0; n = n - 1)
  106.         printf("%d\n", n);
  107.     printf("Blast off!\n");
  108.     }
  109. ###blast2.c
  110. /* blast2 - print countdown
  111.  */
  112. main()
  113.     {
  114.     short n;
  115.  
  116.     for (n = 10; n >= 0; n = n - 1)
  117.         {
  118.         printf("%d\n", n);
  119.         if (n == 3)
  120.             printf("We have ignition!\n");
  121.         }
  122.     printf("Blast off!\n");
  123.     }
  124. ###cadd.c
  125. /* cadd - add two COMPLEX numbers
  126.  */
  127. #include "complex.h"
  128. COMPLEX cadd(x, y)
  129.     COMPLEX x, y;
  130.     {
  131.     COMPLEX z;
  132.  
  133.     z.real = x.real + y.real;
  134.     z.imag = x.imag + y.imag;
  135.     return (z);
  136.     }
  137. ###cap.c
  138. /* cap - capitalize initial letters
  139.  */
  140. #include "local.h"
  141. #include <ctype.h>
  142. main()
  143.     {
  144.     metachar c;
  145.  
  146.     while (isspace(c = getchar()))
  147.         putchar(c);
  148.     while (c != EOF)
  149.         {
  150.         putchar(toupper(c));
  151.         while (!isspace(c = getchar()))
  152.             putchar(c);
  153.         while (isspace(c))
  154.             {
  155.             putchar(c);
  156.             c = getchar();
  157.             }
  158.         }
  159.     }
  160. /* toupper - convert lower-case letter to lower case
  161.  */
  162. metachar toupper(c)
  163. metachar c;
  164.     {
  165.     return (islower(c) ? c + 'A' - 'a' : c);
  166.     }
  167. ###cap2.c
  168. /* cap - capitalize initial letters
  169.  */
  170. #include "local.h"
  171. #include <ctype.h>
  172. main()
  173.     {
  174.     tbool waswhite;    /* was preceding character a whitespace? */
  175.     metachar c;
  176.  
  177.     for (waswhite = YES; (c = getchar()) != EOF; waswhite = isspace(c))
  178.         {
  179.         if (!isspace(c) && waswhite)
  180.             putchar(toupper(c));
  181.         else
  182.             putchar(c);
  183.         }
  184.     }
  185. /* toupper - convert lower-case letter to lower case
  186.  */
  187. metachar toupper(c)
  188. metachar c;
  189.     {
  190.     return (islower(c) ? c + 'A' - 'a' : c);
  191.     }
  192. ###codes1.c
  193. /* codes1 - print ASCII codes
  194.  */
  195. main()
  196.     {
  197.     short c;
  198.  
  199.     for (c = 0; c <= 127; c = c + 1)
  200.         {
  201.         printf("%3d 0x%02x 0%03o", c, c, c);
  202.         if (' ' <= c && c <= '~')
  203.             printf(" '%c'", c);
  204.         if ('0' <= c && c <= '9')
  205.             printf(" digit");
  206.         if ('A' <= c && c <= 'Z')
  207.             printf(" uppercase");
  208.         if ('a' <= c && c <= 'z')
  209.             printf(" lowercase");
  210.         printf("\n");
  211.         }
  212.     }
  213. ###codes2.c
  214. /* codes2 - print ASCII codes
  215.  */
  216. #include "local.h"
  217. main()
  218.     {
  219.     short c;
  220.  
  221.     while ((c = getchar()) != EOF)
  222.         {
  223.         printf("%3d 0x%02x 0%03o", c, c, c);
  224.         if (' ' <= c && c <= '~')
  225.             printf(" '%c'", c);
  226.         if ('0' <= c && c <= '9')
  227.             printf(" digit");
  228.         if ('A' <= c && c <= 'Z')
  229.             printf(" uppercase");
  230.         if ('a' <= c && c <= 'z')
  231.             printf(" lowercase");
  232.         printf("\n");
  233.         }
  234.     }
  235. ###codes3.c
  236. /* codes3 - print ASCII codes
  237.  */
  238. #include "local.h"
  239. main()
  240.     {
  241.     metachar c;    /* return from getchar: char or EOF */
  242.  
  243.     while ((c = getchar()) != EOF)
  244.         {
  245.         printf("%3d 0x%02x 0%03o", c, c, c);
  246.         if (' ' <= c && c <= '~')
  247.             printf(" '%c'", c);
  248.         if ('0' <= c && c <= '9')
  249.             printf(" digit");
  250.         if ('A' <= c && c <= 'Z')
  251.             printf(" uppercase");
  252.         if ('a' <= c && c <= 'z')
  253.             printf(" lowercase");
  254.         printf("\n");
  255.         }
  256.     }
  257. ###codes4.c
  258. /* codes1 - print ASCII codes
  259.  */
  260. #include "local.h"
  261. #include <ctype.h>
  262. main()
  263.     {
  264.     metachar c;
  265.  
  266.     for (c = 0; c <= 127; c = c + 1)
  267.         {
  268.         printf("%3d 0x%02x 0%03o", c, c, c);
  269.         if (isprint(c))
  270.             printf(" '%c'", c);
  271.         if (isdigit(c))
  272.             printf(" D");
  273.         if (isupper(c))
  274.             printf(" UC");
  275.         if (islower(c))
  276.             printf(" LC");
  277.         if (isalpha(c))
  278.             printf(" L");
  279.         if (isalnum(c))
  280.             printf(" AN");
  281.         if (isspace(c))
  282.             printf(" S");
  283.         if (ispunct(c))
  284.             printf(" P");
  285.         if (iscntrl(c))
  286.             printf(" C");
  287.         printf("\n");
  288.         }
  289.     }
  290. ###collect.c
  291. #include <std.h>
  292. #define MAXL 512
  293.  
  294. /* collect line numbers on common index entries
  295.  */
  296. main(ac, av)
  297.     COUNT ac;
  298.     TEXT **av;
  299.         {
  300.     TEXT c, *s;
  301.     TEXT this[MAXL], next[MAXL];
  302.  
  303.     c = (ac <= 1) ? '\t' : av[1][0];
  304.     s = (ac <= 2) ? "\n\t" : av[2];
  305.     if (getfmt("%512p", this) <= 0)
  306.         this[0] = '\0';
  307.     putfmt("%p", this);
  308.     while (this[0])
  309.             {
  310.         if (getfmt("%512p", next) <= 0)
  311.             next[0] = '\0';
  312.         if (mthru(this, next, c))
  313.                 {
  314.             putfmt("%p", s);
  315.             putfmt("%p", suf(next, c));
  316.                 }
  317.         else
  318.                 {
  319.             putfmt("\n");
  320.             cpystr(this, next, NULL);
  321.             putfmt("%p", this);
  322.                 }
  323.             }
  324.         }
  325.  
  326. /* match strings thru null or c
  327.  */
  328. mthru(pa, pb, c)
  329.     TEXT *pa, *pb, c;
  330.         {
  331.     for ( ; *pa == *pb && *pa != c; pa++)
  332.         if (*pb == '\0')
  333.             return(1);
  334.         else
  335.             pb++;
  336.     return (*pa == *pb || *pa == '\0' && *pb == c);
  337.         }
  338.  
  339. /* locate suffix of s following c
  340.  */
  341. suf(s, c)
  342.     TEXT *s, c;
  343.         {
  344.     while (*s && *s++ != c)
  345.         ;
  346.     return (s);
  347.         }
  348. ###copy.c
  349. /* copy - copy input to output
  350.  */
  351. #include "local.h"
  352. main()
  353.     {
  354.     metachar c;
  355.  
  356.     while ((c = getchar()) != EOF)
  357.         putchar(c);
  358.     exit(SUCCEED);
  359.     }
  360. ###copy2.c
  361. /* copy2 - copy input to output 
  362.  */
  363. #include "local.h"
  364. main()
  365.     {
  366.     char s[BUFSIZ];
  367.  
  368.     while (getln(s, BUFSIZ) != EOF)
  369.         printf("%s", s);
  370.     }
  371. ###copy3.c
  372. /* copy3 - most efficient file copy
  373.  */
  374. #include "local.h"
  375. main()
  376.     {
  377.     char s[BUFSIZ];    /* array for characters */
  378.     short i;        /* number of characters read */
  379.  
  380.     while (0 != (i = read(STDIN, s, BUFSIZ)))
  381.         {
  382.         if (i < 0)
  383.             error("I/O error on read\n", "");
  384.         else if (i != write(STDOUT, s, i))
  385.             error("I/O error on write\n", "");
  386.         }
  387.     exit(SUCCEED);
  388.     }
  389. /* error - print fatal error message
  390.  */
  391. void error(s1, s2)
  392.     char s1[], s2[];
  393.     {
  394.     write(STDERR, s1, strlen(s1));
  395.     write(STDERR, " ", 1);
  396.     write(STDERR, s2, strlen(s2));
  397.     write(STDERR, "\n", 1);
  398.     exit(FAIL);
  399.     }
  400. ###dmpdem.c
  401. /* dump - print memory bytes
  402.  *        (Warning - some usages may be non-portable)
  403.  */
  404. #include "local.h"
  405. #define LINESIZE 16
  406. #define BYTEMASK 0xFF
  407. void dump(s, n)
  408.     char s[];        /* byte address to be dumped */
  409.     unsigned n;        /* number of bytes to dump */
  410.     {
  411.     unsigned i;        /* byte counter */
  412.  
  413.     for (i = 0; i < n; ++i)
  414.         {
  415.         if (i % LINESIZE == 0)
  416.             printf("\n%08x: ", &s[i]);
  417.         printf(" %02x", s[i] & BYTEMASK);
  418.         }
  419.     printf("\n");
  420.     }
  421. /* dmpdem - demonstrate dump function
  422.  */
  423. main()
  424.     {
  425.     char msg[16];
  426.     double d = 100.;
  427.  
  428.     strncpy(msg, "testing 1 2 3\n", sizeof(msg));
  429.  
  430.     /* case 1 - quite proper */
  431.     dump(msg, sizeof(msg));
  432.  
  433.     /* case 2 - ok, but output will vary with machine */
  434.     dump(&d, sizeof(d));
  435.  
  436.     /* case 3 - non-portable, may cause hardware error */
  437.     dump(0x40, 4);
  438.     exit(SUCCEED);
  439.     }
  440. ###dump.c
  441. /* dump - print memory bytes
  442.  *        (Warning - some usages may be non-portable)
  443.  */
  444. #include "local.h"
  445. #define LINESIZE 16
  446. void dump(s, n)
  447.     char s[];            /* byte address to be dumped */
  448.     unsigned n;            /* number of bytes to dump */
  449.     {
  450.     unsigned i;        /* byte counter */
  451.  
  452.     for (i = 0; i < n; ++i)
  453.         {
  454.         if (i % LINESIZE == 0)
  455.             printf("\n%08x: ", &s[i]);
  456.         printf(" %02x", s[i]);
  457.         }
  458.     printf("\n");
  459.     }
  460. ###echo.c
  461. /* echo - print command-line arguments
  462.  */
  463. #include "local.h"
  464. main(ac, av)
  465.     unsigned ac;
  466.     char *av[];
  467.     {
  468.     unsigned i;
  469.  
  470.     for (i = 1; i < ac; ++i)
  471.         printf(i < ac-1 ? "%s " : "%s\n", av[i]);
  472.     exit(SUCCEED);
  473.     }
  474. ###error.c
  475. /* error - print fatal error message
  476.  */
  477. #include "local.h"
  478. void error(s1, s2)
  479.     char s1[], s2[];
  480.     {
  481.     write(STDERR, s1, strlen(s1));
  482.     write(STDERR, " ", 1);
  483.     write(STDERR, s2, strlen(s2));
  484.     write(STDERR, "\n", 1);
  485.     exit(FAIL);
  486.     }
  487. ###fact.c
  488. /* factl - return n! (n factorial)
  489.  */
  490. #include "local.h"
  491. long factl(n)
  492.     long n;
  493.     {
  494.     if (n <= 1)
  495.         return (1);
  496.     else
  497.         return (n * factl(n - 1));
  498.     }
  499. /* fact - demonstrate factl function
  500.  */
  501. main()
  502.     {
  503.     long factl();
  504.     long result;
  505.  
  506.     result = factl((long)3);
  507.     printf("3! = %ld\n", result);
  508.     exit(SUCCEED);
  509.     }
  510. ###factl.c
  511. /* factl - return n! (n factorial)
  512.  */
  513. #include "local.h"
  514. long factl(n)
  515.     long n;
  516.     {
  517.     if (n <= 1)
  518.         return (1);
  519.     else
  520.         return (n * factl(n - 1));
  521.     }
  522. #ifdef TRYMAIN
  523. main()
  524.     {
  525.     long factl();
  526.  
  527.     if (factl((long)3) != (long)6)
  528.         error("failed 3", "");
  529.     if (factl((long)13) != 1932053504)
  530.         error("failed 13", "");
  531.     exit(SUCCEED);
  532.     }
  533. #endif
  534. ###fast.c
  535. /* fast - count to one million
  536.  */
  537. #include "local.h"
  538. main()
  539.     {
  540.     register short units;
  541.     register short thous;
  542.  
  543.     thous = 0;
  544.     while (++thous <= 1000)
  545.         {
  546.         units = 0;
  547.         while (++units <= 1000)
  548.            ;
  549.         }
  550.     }
  551. ###getbn.c
  552. /* getbn - get a binary number and print it
  553.  */
  554. #include "local.h"
  555. #include <ctype.h>
  556. main()
  557.     {
  558.     metachar c;
  559.     short n;
  560.  
  561.     n = 0;
  562.     c = getchar();
  563.     while (c != EOF && isspace(c))
  564.         c = getchar();
  565.     while (c == '0' || c == '1')
  566.         {
  567.         n = ((n << 1) | (c - '0'));
  568.         c = getchar();
  569.         }
  570.     printf("%5u 0x%04x 0%06o\n", n, n, n);
  571.     }
  572. ###getbn2.c
  573. /* getbn2 - get and print binary numbers
  574.  */
  575. #include "local.h"
  576. #include <ctype.h>
  577. main()
  578.     {
  579.     short getbin();
  580.     short number;
  581.  
  582.     while ((number = getbin()) != 0)
  583.         printf("%5u 0x%04x 0%06o\n", number, number, number);
  584.     }
  585. /* getbin - get binary input number
  586.  */
  587. short getbin()
  588.     {
  589.     metachar c;
  590.     short n;
  591.  
  592.     n = 0;
  593.     c = getchar();
  594.     while (c != EOF && isspace(c))
  595.         c = getchar();
  596.     while (c == '0' || c == '1')
  597.         {
  598.         n = ((n << 1) | (c - '0'));
  599.         c = getchar();
  600.         }
  601.     return (n);
  602.     }
  603. ###getpns.c
  604. /* getpns - get pennies, in floating double
  605.  */
  606. #include "local.h"
  607. double getpns()
  608.     {
  609.     long dols;
  610.     short cents;
  611.  
  612.     if (scanf("%ld.%hd", &dols, ¢s) == EOF)
  613.         return (0.);
  614.     return (100 * dols + cents);
  615.     }
  616. ###gettt.c
  617. /* gettt - read data into the task table (using gettask function)
  618.  */
  619. #include "local.h"
  620. #include "task3.h"    /* revised to include storage for desc */
  621. #define TSIZE 5
  622. main()
  623.     {
  624.     TASK tt[TSIZE];                /* task table */
  625.     short gettask();            /* function to get one TASK */
  626.     short i;                    /* index for printing */
  627.     short n;                    /* number of successful reads */
  628.  
  629.     n = 0;
  630.     while (n < TSIZE && gettask(&tt[n]) == 4)
  631.         ++n;
  632.     for (i = 0; i < n; ++i)
  633.         printf("%20s %8ld %8ld %8ld\n",
  634.             tt[i].desc, tt[i].plan, tt[i].start, tt[i].finish);
  635.     }
  636. /* gettask - get one TASK
  637.  */
  638. short gettask(ptask)
  639.     TASK *ptask;
  640.     {
  641.     short ret;                    /* returned value from scanf */
  642.  
  643.     ret = scanf("%20s%ld%ld%ld",
  644.         ptask->desc, &ptask->plan, &ptask->start, &ptask->finish);
  645.     return (ret);
  646.     }
  647. ###guess.c
  648. /* guess - guess a hidden number between 1 and 15, in 3 guesses
  649.  */
  650. #include "local.h"
  651. main()
  652.     {
  653.     char line[BUFSIZ];    /* input line */
  654.     tbool found;        /* have I found it? */
  655.     short n;            /* how many guesses left */
  656.     short range;        /* how much to ajust next guess */
  657.     short try;            /* next number to try */
  658.     metachar reply;        /* the user's reply */
  659.  
  660.     found = NO;
  661.     n = 3;
  662.     range = 4;
  663.     try = 8;
  664.     printf("Each time I guess, please answer\n");
  665.     printf(" H if I'm high\n L if I'm low\n E if I guessed it\n");
  666.     while (n > 0 && !found)
  667.         {
  668.         printf("I guess %d\n", try);
  669.         if (getln(line, BUFSIZ) == EOF)
  670.             error("Bye!", "");
  671.         reply = line[0];
  672.         if (reply == 'H' || reply == 'h')
  673.             {
  674.             try -= range;
  675.             range /= 2;
  676.             --n;
  677.             }
  678.         else if (reply == 'L' || reply == 'l')
  679.             {
  680.             try += range;
  681.             range /= 2;
  682.             --n;
  683.             }
  684.         else if (reply == 'E' || reply == 'e')
  685.             found = YES;
  686.         else
  687.             printf("Please type H, L, or E\n");
  688.         }
  689.     printf("Your number is %d\nThanks for the game\n", try);
  690.     exit(SUCCEED);
  691.     }
  692. ###guess2.c
  693. #include "local.h"
  694. /* guess2 - guess a hidden number between 1 and 15, in 3 guesses
  695.  */
  696. main()
  697.     {
  698.     tbool found = NO;    /* have I found it? */
  699.     short n = 3;        /* how many guesses left */
  700.     short range = 4;    /* how much to ajust next guess */
  701.     short try = 8;        /* next number to try */
  702.     metachar reply;        /* the user's reply */
  703.  
  704.     printf("Each time I guess, please answer\n");
  705.     printf("H if I'm high\nL if I'm low\E if I guessed it\n");
  706.     while (n > 0 && !found)
  707.         {
  708.         printf("I guess %d\n", try);
  709.         reply = getchar();
  710.         while (getchar() != '\n')
  711.             ;
  712.         switch (reply)
  713.             {
  714.         case 'H':
  715.         case 'h':
  716.             try -= range;
  717.             range /= 2;
  718.             --n;
  719.             break;
  720.         case 'L':
  721.         case 'l':
  722.             try += range;
  723.             range /= 2;
  724.             --n;
  725.             break;
  726.         case 'E':
  727.         case 'e':
  728.             found = YES;
  729.             break;
  730.         default:
  731.             printf("Please type H, L, or E\n");
  732.             break;
  733.             }
  734.         }
  735.     printf("Your number is %d\nThanks for the game\n", try);
  736.     exit(SUCCEED);
  737.     }
  738. ###helbad.c
  739. main()
  740.     {
  741.     write(1, "hello, world\n", 13)
  742.     }
  743. ###helerr.c
  744. main
  745.     {
  746.     write(1, "hello, world\n", 13);
  747.     }
  748. ###hello.c
  749. main()
  750.     {
  751.     write(1, "hello, world\n", 13);
  752.     }
  753. ###hello2.c
  754. /* hello2 - print greeting
  755.  */
  756. main()
  757.     {
  758.     write(1, "hello, world\n", 13);
  759.     }
  760. ###hello3.c
  761. /* hello3 - print greeting */
  762. main
  763. (
  764. )
  765. {
  766. write
  767. (
  768. 1
  769. ,
  770. "hello, world\n"
  771. ,
  772. 13
  773. )
  774. ;
  775. }
  776. ###hello4.c
  777. /* hello4 - print greeting
  778.  */
  779. #define STDOUT  1
  780. #define MESSAGE "hello, world\n"
  781. #define LENGTH  13
  782. main()
  783.     {
  784.     write(STDOUT, MESSAGE, LENGTH);
  785.     }
  786. ###hello5.c
  787. /* hello5 - print greeting
  788.  */
  789. #include "local.h"
  790. #define MESSAGE "hello, world\n"
  791. #define LENGTH  13
  792. main()
  793.     {
  794.     write(STDOUT, MESSAGE, LENGTH);
  795.     }
  796. ###index.c
  797. /* index - return index of first occurrence of char c in string s
  798.  *    pointer version
  799.  */
  800. #include "local.h"
  801. char *index(s, c)
  802.     char s[], c;
  803.     {
  804.     while (*s != '\0' && *s != c)
  805.         ++s;
  806.     return (*s == c ? s : NULL);
  807.     }
  808. ###index0.c
  809. /* index0 - return index of first occurrence of char c in string s
  810.  *    subscripted version
  811.  */
  812. #include "local.h"
  813. char *index0(s, c)
  814.     char s[], c;
  815.     {
  816.     unsigned i = 0;
  817.  
  818.     while (s[i] != '\0' && s[i] != c)
  819.         ++i;
  820.     return (s[i] == c ? &s[i] : NULL);
  821.     }
  822. ###inits.c
  823. /* inits - initialization examples
  824.  */
  825. #include "local.h"
  826. main()
  827.     {
  828.     char c = 'x';
  829.     short i = 1;
  830.     short j = i * 2;
  831.  
  832.     printf("%d %d %c\n", i, j, c);
  833.     exit(SUCCEED);
  834.     }
  835. ###loadtt.c
  836. /* loadtt - read data into the task table
  837.  */
  838. #include "local.h"
  839. #include "task.h"    /* the original include-file: TIME == long */
  840. #define TSIZE 5
  841. main()
  842.     {
  843.     TASK tt[TSIZE];                /* task table */
  844.     char tstring[TSIZE][21];    /* string storage */
  845.     short i;                    /* index for printing */
  846.     short n;                    /* number of successful reads */
  847.     short ret;                    /* returned value from scanf */
  848.  
  849.     n = 0;
  850.     FOREVER
  851.         {
  852.         tt[n].desc = tstring[n];
  853.         ret = scanf("%20s%ld%ld%ld",
  854.             tt[n].desc, &tt[n].plan, &tt[n].start, &tt[n].finish);
  855.         if (ret == EOF)
  856.             break;
  857.         else if (ret != 4)
  858.             error("Data error", "");
  859.         else if (++n >= TSIZE)
  860.             break;
  861.         }
  862.     for (i = 0; i < n; ++i)
  863.         printf("%20s %8ld %8ld %8ld\n",
  864.             tt[i].desc, tt[i].plan, tt[i].start, tt[i].finish);
  865.     }
  866. ###lpow.c
  867. /* lpow - power function (for long data)
  868.  */
  869. long lpow(lnum, n)
  870.     long lnum;        /* base */
  871.     long n;            /* exponent */
  872.     {
  873.     long p;        /* local ("auto") result */
  874.  
  875.     p = 1;
  876.     for ( ; n > 0; --n)
  877.         p *= lnum;
  878.     return (p);
  879.     }
  880. ###maxmin.c
  881. /* maxmin - print the max and min of two double inputs
  882.  */
  883. #include "local.h"
  884. main()
  885.     {
  886.     double a, b;
  887.  
  888.     if (scanf("%lf %lf", &a, &b) == 2)
  889.         {
  890.         printf("  a = %12.6e\n", a);
  891.         printf("  b = %12.6e\n", b);
  892.         printf("max = %12.6e\n", a < b ? a : b);
  893.         printf("min = %12.6e\n", a < b ? b : a);
  894.         }
  895.     }
  896. ###mortg.c
  897. /* mortg - compute table of payments on mortgage
  898.  */
  899. #include "local.h"
  900. #include <math.h>
  901. main()
  902.     {
  903.     double intmo;    /* monthly interest */
  904.     double intyr;    /* annual interest */
  905.     double bal;        /* balance remaining */
  906.     double pmt;        /* monthly payment */
  907.     double prinpmt;    /* payment allocated to principal */
  908.     double intpmt;    /* payment allocated to interest */
  909.     double dnpmts;    /* number of payments, in double */
  910.     short i;        /* loop index */
  911.     short npmts;    /* number of payments */
  912.     short nyears;    /* number of years */
  913.  
  914.     printf("Enter principal (e.g. 82500.00): ");
  915.     scanf("%lf", &bal);
  916.     printf("Enter annual interest rate (e.g. 16.25): ");
  917.     scanf("%lf", &intyr);
  918.     printf("Enter number of years: ");
  919.     scanf("%hd", &nyears);
  920.     printf("\nprincipal=%.2f  interest=%.4f%%  years=%d\n\n",
  921.         bal, intyr, nyears);
  922.     intyr /= 100.;
  923.     intmo = intyr / 12.;
  924.     npmts = nyears * 12;
  925.     dnpmts = npmts;
  926.     pmt = bal * (intmo / (1. - pow(1. + intmo, -dnpmts)));
  927.     printf("%8s %10s %10s %10s %10s\n",
  928.         "payment", "total", "interest", "principal", "balance");
  929.     printf("%8s %10s %10s %10s\n",
  930.         "number", "payment", "payment", "payment");
  931.     printf("%8s %10s %10s %10s %10.2f\n",
  932.         "", "", "", "", bal);
  933.     for (i = 1; i <= npmts; ++i)
  934.         {
  935.         intpmt = bal * intmo;
  936.         if (i < npmts)
  937.             prinpmt = pmt - intpmt;
  938.         else
  939.             prinpmt = bal;
  940.         bal -= prinpmt;
  941.         printf("%8d %10.2f %10.2f %10.2f %10.2f\n",
  942.             i, intpmt + prinpmt, intpmt, prinpmt, bal);
  943.         }
  944.     }
  945. ###mortg2.c
  946. /* mortg2 - compute table of payments on mortgage
  947.  */
  948. #include "local.h"
  949. #include <math.h>
  950. #define ROUNDING .5    /* on machines that do rounding, make it 0 */
  951. main()
  952.     {
  953.     double dbal;    /* balance, in double - dollars */
  954.     double dnpmts;    /* number of payments, in double */
  955.     double intmo;    /* monthly interest */
  956.     double intyr;    /* annual interest */
  957.     long bal;        /* balance remaining - pennies */
  958.     long pmt;        /* monthly payment - pennies */
  959.     long prinpmt;    /* payment allocated to principal - pennies */
  960.     long intpmt;    /* payment allocated to interest - pennies */
  961.     short i;        /* loop index */
  962.     short npmts;    /* number of payments */
  963.     short nyears;    /* number of years */
  964.  
  965.     printf("Enter principal (e.g. 82500.00): ");
  966.     scanf("%lf", &dbal);
  967.     bal = 100. * dbal;
  968.     printf("Enter annual interest rate (e.g. 16.25): ");
  969.     scanf("%lf", &intyr);
  970.     printf("Enter number of years: ");
  971.     scanf("%hd", &nyears);
  972.     printf("\nprincipal=%10.2f", dbal);
  973.     printf("  interest=%.4f%%  years=%d\n\n", intyr, nyears);
  974.     intyr /= 100.;
  975.     intmo = intyr / 12.;
  976.     npmts = nyears * 12;
  977.     dnpmts = npmts;
  978.     pmt = ROUNDING + bal * (intmo / (1. - pow(1. + intmo, -dnpmts)));
  979.     printf("%8s %10s %10s %10s %10s\n",
  980.         "payment", "total", "interest", "principal", "balance");
  981.     printf("%8s %10s %10s %10s\n",
  982.         "number", "payment", "payment", "payment");
  983.     printf("%8s %10s %10s %10s %10ld\n",
  984.         "", "", "", "", bal);
  985.     for (i = 1; i <= npmts; ++i)
  986.         {
  987.         intpmt = ROUNDING + bal * intmo;
  988.         if (i < npmts)
  989.             prinpmt = pmt - intpmt;
  990.         else
  991.             prinpmt = bal;
  992.         bal -= prinpmt;
  993.         printf("%8d %10ld %10ld %10ld %10ld\n",
  994.             i, intpmt + prinpmt, intpmt, prinpmt, bal);
  995.         }
  996.     }
  997. ###nfrom.c
  998. /* nfrom - return a number between low and high, inclusive
  999.  */
  1000. #include "local.h"
  1001. short nfrom(low, high)
  1002.     short low, high;
  1003.     {
  1004.     short rand();
  1005.     short nb = high - low + 1;
  1006.  
  1007.     return (rand() % nb + low);
  1008.     }
  1009. ###noinit.c
  1010. /* noinit - some illegal initializers
  1011.  */
  1012. #include "local.h"
  1013. short a = 0;
  1014. short b = a + 1;
  1015. short c[5] = {4, 3, 2, 1};
  1016. main()
  1017.     {
  1018.     short d = a + 2;
  1019.     short e[3] = {1, 2, 3};
  1020.     static short f = d + 1;
  1021.     static short g[2] = {4, 5, 6};
  1022.  
  1023.     printf("initializers\n");
  1024.     }
  1025. ###onebox.c
  1026. /* onebox - draw a box around input file
  1027.  */
  1028. #include "local.h"
  1029. main(ac, av)
  1030.     unsigned ac;
  1031.     char *av[];
  1032.     {
  1033.     short max = 0;
  1034.     char s[BUFSIZ];
  1035.     char *index();
  1036.     static char rule[] =
  1037.         " ____________________________________________________________";
  1038.     FILE *fp;
  1039.  
  1040.     fp = fopen(av[1], "r") || error("can't open ", av[1]);
  1041.     while (fgets(s, BUFSIZ, fp))
  1042.         max = MAX(max, strlen(s) - 1);
  1043.     fclose(fp);
  1044.     fp = fopen(av[1], "r") || error("can't open ", av[1]);
  1045.     printf("%*s\n", max+3, rule);
  1046.     while (fgets(s, BUFSIZ, fp))
  1047.         {
  1048.         *index(s, '\n') = '\0';
  1049.         printf("| %*s |\n", max, s);
  1050.         }
  1051.     printf("%*s\n", max+3, rule);
  1052.     fclose(fp);
  1053.     }
  1054. ###pegs.c
  1055. /* pegs.c - three functions (push, pop, dumppg) for Towers of Hanoi
  1056.  */
  1057. #include "local.h"
  1058. #include "pegs.h"
  1059. static short pegs[3][NDISKS] = 0;
  1060. static short ndisks[3] = 0;
  1061. /* push - put disk onto peg
  1062.  */
  1063. void push(peg, disk)
  1064.     short peg;    /* which peg: 0, 1, ... */
  1065.     short disk;    /* which disk: 1, 2, ... */
  1066.     {
  1067.     if (peg < 0 || 3 <= peg)
  1068.         {
  1069.         printf("Cannot push onto peg %d\n", peg);
  1070.         exit(FAIL);
  1071.         }
  1072.     else
  1073.         pegs[peg][ndisks[peg]++] = disk;
  1074.     }
  1075. /* pop - remove disk from peg
  1076.  */
  1077. short pop(peg)
  1078.     short peg;
  1079.     {
  1080.     if (peg < 0 || 3 <= peg)
  1081.         {
  1082.         printf("Cannot pop peg %d\n", peg);
  1083.         exit(FAIL);
  1084.         }
  1085.     else if (ndisks[peg] < 1)
  1086.         {
  1087.         printf("Cannot pop peg %d (it has %d disks)\n",
  1088.             peg, ndisks[peg]);
  1089.         exit(FAIL);
  1090.         }
  1091.     else
  1092.         return (pegs[peg][--ndisks[peg]]);
  1093.     }
  1094. /* dumppg - print status of disks and pegs
  1095.  */
  1096. void dumppg()
  1097.     {
  1098.     short i;    /* index over pegs */
  1099.     short j;    /* index over disks */
  1100.  
  1101.     for (i = 0; i < 3; ++i)
  1102.         {
  1103.         printf("Peg %d:", i);
  1104.         for (j = 0; j < ndisks[i]; ++j)
  1105.             printf(" %d", pegs[i][j]);
  1106.         printf("\n");
  1107.         }
  1108.     }
  1109. ###pgm123.c
  1110. #include <stdio.h>
  1111. #include "local.h"
  1112. /* guesser - guess a hidden number between 1 and 15, in 3 guesses
  1113.  */
  1114. main()
  1115.     {
  1116.     bool found = NO;    /* have I found it? */
  1117.     short n = 3;        /* how many guesses left */
  1118.     short range = 4;    /* how much to ajust next guess */
  1119.     short try = 8;        /* next number to try */
  1120.     char reply;            /* the user's reply */
  1121.  
  1122.     printf("Each time I guess, please answer\n");
  1123.     printf("H if I'm high\nL if I'm low\E if I guessed it\n");
  1124.     while (n > 0 && !found)
  1125.         {
  1126.         printf("I guess %d\n", try);
  1127.         scanf("%c", &reply);
  1128.         if (reply == 'H')
  1129.             {
  1130.             try -= range;
  1131.             range /= 2;
  1132.             --n;
  1133.             }
  1134.         else if (reply == 'L')
  1135.             {
  1136.             try += range;
  1137.             range /= 2;
  1138.             --n;
  1139.             }
  1140.         else if (reply == 'E')
  1141.             found = YES;
  1142.         else
  1143.             printf("Please type H, L, or E\n");
  1144.         }
  1145.     printf("Your number is %d\nThanks for the game\n", try);
  1146.     }
  1147. ###pow.c
  1148. /* pow - return (positive) x to the power y
  1149.  */
  1150. double pow(x, y)
  1151.     double x;    /* base */
  1152.     double y;    /* exponent */
  1153.     {
  1154.     double exp();    /* exponential function */
  1155.     double log();    /* natural log function */
  1156.  
  1157.     return (exp(log(x) * y));
  1158.     }
  1159. ###powdem.c
  1160. /* powdem - demonstrate power function
  1161.  */
  1162. #include "local.h"
  1163. #include <math.h>
  1164. main()
  1165.     {
  1166.     short i;
  1167.  
  1168.     for (i = 0; i < 10; ++i)
  1169.         printf("2 to the power %d equals %.0f\n",
  1170.             i, pow(2., (double)i));
  1171.     exit(SUCCEED);
  1172.     }
  1173. /* pow - return (positive) x to the power y
  1174.  */
  1175. double pow(x, y)
  1176.     double x;    /* base */
  1177.     double y;    /* exponent */
  1178.     {
  1179.     return (exp(log(x) * y));
  1180.     }
  1181. ###pr2a.c
  1182. /* pr2a - print the sum of two long inputs
  1183.  */
  1184. #include "local.h"
  1185. main()
  1186.     {
  1187.     long a, b;
  1188.  
  1189.     scanf("%lx %lx", &a, &b);
  1190.     printf("    a = %8lx\n", a);
  1191.     printf("    b = %8lx\n", b);
  1192.     printf("a + b = %8lx\n", a + b);
  1193.     }
  1194. ###pr2b.c
  1195. /* pr2b - print the sum of two double inputs
  1196.  */
  1197. #include "local.h"
  1198. main()
  1199.     {
  1200.     double a, b;
  1201.  
  1202.     scanf("%lf %lf", &a, &b);
  1203.     printf("    a = %12.6e\n", a);
  1204.     printf("    b = %12.6e\n", b);
  1205.     printf("a + b = %12.6e\n", a + b);
  1206.     }
  1207. ###prsam.c
  1208. /* prsam - print sampler
  1209.  */
  1210. #include "local.h"
  1211. main()
  1212.     {
  1213.     static char sampler[35][61] =
  1214.         {
  1215.         "LOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVE",
  1216.         "L            OVELOVELOVELOVELOVELOVELOV         LOVELOVELOVE",
  1217.         "LOV        ELOVELOVELOVELOVELOVELOV                 LOVELOVE",
  1218.         "LOVE      VELOVELOVELOVELOVELOVEL                     VELOVE",
  1219.         "LOVE      VELOVELOVELOVELOVELOVE            LOVEL      ELOVE",
  1220.         "LOVE      VELOVELOVELOVELOVELOV           VELOVELO      LOVE",
  1221.         "LOVE      VELOVELOVELOVELOVELOV          OVELOVELOV     LOVE",
  1222.         "LOVE      VELOVELOVELOVELOVELOV         LOVELOVELOV     LOVE",
  1223.         "LOVE      VELOVELOVELOVELOVELOV        ELOVELOVELO      LOVE",
  1224.         "LOVE      VELOVELOVELOVELOVELOV       VELOVELOVEL       LOVE",
  1225.         "LOVE      VELOVELOVELOVELOVELOV      OVELOVELOVE        LOVE",
  1226.         "LOVE      VELOVELOVELOVELOVEL V     LOVELOVELOV         LOVE",
  1227.         "LOVE      VELOVELOVELOVELOVE  V     LOVELOVELO          LOVE",
  1228.         "LOVE      VELOVELOVELOVELOV   V      OVELOVEL           LOVE",
  1229.         "LOVE      VELOVELOVELOVEL     VE                       ELOVE",
  1230.         "L                             VELOV                 LOVELOVE",
  1231.         "L                             VELOVELOV         LOVELOVELOVE",
  1232.         "L             VELOV                                        E",
  1233.         "L             VELOV                                        E",
  1234.         "LOVE      VELOVELOVELOV   VELOVELOVE      VELOVELOVELO     E",
  1235.         "LOVEL      ELOVELOVELO   OVELOVELOVE      VELOVELOVELOVE   E",
  1236.         "LOVEL      ELOVELOVELO   OVELOVELOVE      VELOVELOVELOVEL  E",
  1237.         "LOVELO      LOVELOVEL   LOVELOVELOVE      VELOVELOVELOVELO E",
  1238.         "LOVELO      LOVELOVEL   LOVELOVELOVE      VELOVELOVELOVELOVE",
  1239.         "LOVELOV      OVELOVE   ELOVELOVELOVE      VELOVEL VELOVELOVE",
  1240.         "LOVELOV      OVELOVE   ELOVELOVELOVE              VELOVELOVE",
  1241.         "LOVELOVE      VELOV   VELOVELOVELOVE      VELOVE  VELOVELOVE",
  1242.         "LOVELOVE      VELOV   VELOVELOVELOVE      VELOVEL VELOVELOVE",
  1243.         "LOVELOVEL      ELO   OVELOVELOVELOVE      VELOVELOVELOVELO E",
  1244.         "LOVELOVEL      ELO   OVELOVELOVELOVE      VELOVELOVELOVEL  E",
  1245.         "LOVELOVELO      L   LOVELOVELOVELOVE      VELOVELOVELOVE   E",
  1246.         "LOVELOVELO          LOVELOVELOVELOVE      VELOVELOVELO     E",
  1247.         "LOVELOVELOV        ELOVELOVELOVE                           E",
  1248.         "LOVELOVELOV        ELOVELOVELOVE                           E",
  1249.         "LOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVELOVE"
  1250.         };
  1251.     short i;            /* row index for sampler */
  1252.     short j;            /* column index for sampler */
  1253.     short len;            /* length of s */
  1254.     char s[BUFSIZ];        /* user's message */
  1255.  
  1256.     printf("Enter a string:");
  1257.     if ((len = getln(s, BUFSIZ)) == EOF)
  1258.         error("Bye!", "");
  1259.     printf("\n");
  1260.     s[--len] = '\0';
  1261.     len = len < 60 ? len : 60;
  1262.     for (i = 0; i < 35; ++i)
  1263.         {
  1264.         for (j = 0; j < 60; ++j)
  1265.             if (sampler[i][j] != ' ')
  1266.                 sampler[i][j] = s[j % len];
  1267.         printf("%s\n", sampler[i]);
  1268.         }
  1269.     }
  1270. ###putbin.c
  1271. /* putbin - print number in binary format
  1272.  */
  1273. void putbin(n)
  1274.     short n;
  1275.     {
  1276.     short i;
  1277.  
  1278.     for (i = 15; i >= 0; i = i - 1)
  1279.         {
  1280.         if ((n & (1 << i)) == 0)
  1281.             printf("0");
  1282.         else
  1283.             printf("1");
  1284.         }
  1285.     }
  1286. ###rand.c
  1287. /* rand, srand - generate random numbers
  1288.  */
  1289. #include "local.h"
  1290. static long rnum = 0;
  1291. void srand(n)
  1292.     short n;
  1293.     {
  1294.     rnum = (long)n;
  1295.     }
  1296. short rand()
  1297.     {
  1298.     rnum = rnum * 0x41C64E6D + 0x3039;
  1299.     return ((short)(rnum >> 16) & 0x7FFF);
  1300.     }
  1301. ###receip.c
  1302. /* receip - deliver a unique receipt number
  1303.  */
  1304. #include "local.h"
  1305. short receip()
  1306.     {
  1307.     static short number = 1;
  1308.  
  1309.     return (number++);
  1310.     }
  1311. ###recpt1.c
  1312. /* recpt1 - receipt example #1
  1313.  */
  1314. #include "local.h"
  1315. main()
  1316.     {
  1317.     short receip();
  1318.  
  1319.     printf("First = %d\n", receip());
  1320.     printf("Second = %d\n", receip());
  1321.     exit(SUCCEED);
  1322.     }
  1323. short receip()
  1324.     {
  1325.     short number = 1;
  1326.  
  1327.     return (number++);
  1328.     }
  1329. ###recpt2.c
  1330. /* recpt2 - print two receipt numbers
  1331.  */
  1332. #include "local.h"
  1333. main()
  1334.     {
  1335.     short receip();
  1336.  
  1337.     printf("First = %d\n", receip());
  1338.     printf("Second = %d\n", receip());
  1339.     exit(SUCCEED);
  1340.     }
  1341. short receip()
  1342.     {
  1343.     static short number = 1;
  1344.  
  1345.     return (number++);
  1346.     }
  1347. ###recpt3.c
  1348. /* recpt3 - print two receipt numbers
  1349.  */
  1350. #include "local.h"
  1351. main()
  1352.     {
  1353.     short receip();
  1354.  
  1355.     printf("First = %d\n", receip());
  1356.     printf("Second = %d\n", receip());
  1357.     }
  1358. ###remark.c
  1359. /* remark - print non-fatal error message
  1360.  */
  1361. #include "local.h"
  1362. void remark(s1, s2)
  1363.     char s1[], s2[];
  1364.     {
  1365.     write(STDERR, s1, strlen(s1));
  1366.     write(STDERR, " ", 1);
  1367.     write(STDERR, s2, strlen(s2));
  1368.     write(STDERR, "\n", 1);
  1369.     }
  1370. ###revers.c
  1371. /* revers - print input lines reversed
  1372.  */
  1373. #include "local.h"
  1374. main()
  1375.     {
  1376.     char line[BUFSIZ];    /* the line of input text */
  1377.     short len;            /* length of line */
  1378.  
  1379.     while ((len = getln(line, BUFSIZ)) != EOF)
  1380.         {
  1381.         if (line[len - 1] == '\n')
  1382.             line[--len] = '\0';
  1383.         reverse(line);
  1384.         printf("%s\n", line);
  1385.         }
  1386.     }
  1387. void reverse(s)
  1388.     char s[];
  1389.     {
  1390.     char t;
  1391.     short i, j;
  1392.  
  1393.     for (i = 0, j = strlen(s) - 1; i < j; ++i, --j)
  1394.         t = s[i], s[i] = s[j], s[j] = t;
  1395.     }
  1396. ###shuf52.c
  1397. /* shuf52 - shuffle a deck of 52 cards and print result
  1398.  */
  1399. #include "local.h"
  1400. #define NCARDS 52
  1401. main()
  1402.     {
  1403.     short cards[NCARDS];
  1404.     short i;
  1405.  
  1406.     for (i = 0; i < NCARDS; ++i)
  1407.         cards[i] = i;
  1408.     shuffl(cards);
  1409.     for (i = 0; i < NCARDS; ++i)
  1410.         {
  1411.         printf("%2d ", cards[i]);
  1412.         if (i % 13 == 12)
  1413.             putchar('\n');
  1414.         }
  1415.     putchar('\n');
  1416.     }
  1417. /* shuffl - permute the cards
  1418.  */
  1419. void shuffl(deck)
  1420.     short deck[];
  1421.     {
  1422.     short t;        /* temporary for swap */
  1423.     short i;        /* index for loop over cards */
  1424.     short j;        /* index for swap */
  1425.     short nfrom();    /* fn to produce random no. */
  1426.  
  1427.     for (i = 0; i < NCARDS - 1; ++i)
  1428.         {
  1429.         j = nfrom(i, NCARDS - 1);
  1430.         t = deck[j], deck[j] = deck[i], deck[i] = t;
  1431.         }
  1432.     }
  1433. ###sizes.c
  1434. /* sizes - report the size of some types and expressions
  1435.  */
  1436. #include "local.h"
  1437. main()
  1438.     {
  1439.     char c;
  1440.     char s[512];
  1441.     short n;
  1442.     short m[40];
  1443.  
  1444.     printf("%3d %3d\n", sizeof(c), sizeof(char));
  1445.     printf("%3d %3d\n", sizeof(s), sizeof(char[512]));
  1446.     printf("%3d %3d\n", sizeof(n), sizeof(short));
  1447.     printf("%3d %3d\n", sizeof(m), sizeof(short[40]));
  1448.     }
  1449. ###slow.c
  1450. /* slow - count to one million
  1451.  */
  1452. #include "local.h"
  1453. main()
  1454.     {
  1455.     short units;
  1456.     short thous;
  1457.  
  1458.     thous = 0;
  1459.     while (++thous <= 1000)
  1460.         {
  1461.         units = 0;
  1462.         while (++units <= 1000)
  1463.             ;
  1464.         }
  1465.     }
  1466. ###stack1.c
  1467. /* stack1 - stack example 1
  1468.  */
  1469. #include "local.h"
  1470. main()
  1471.     {
  1472.     f1(1);
  1473.     }
  1474. f1(n)
  1475.     short n;
  1476.     {
  1477.     f2(n + 1);
  1478.     }
  1479. f2(n)
  1480.     short n;
  1481.     {
  1482.     printf("%d\n", n);
  1483.     }
  1484. ###strcpy.c
  1485. /* strcpy - copy characters from s2 to s1 
  1486.  */
  1487. void strcpy(s1, s2)
  1488.     char s1[], s2[];
  1489.     {
  1490.     unsigned i;
  1491.  
  1492.     i = 0;
  1493.     while (s2[i] != '\0')
  1494.         {
  1495.         s1[i] = s2[i];
  1496.         ++i;
  1497.         }
  1498.     s1[i] = '\0';
  1499.     }
  1500. ###strcpy2.c
  1501. /* strcpy2 - copy characters from s2 to s1 
  1502.  */
  1503. #include "local.h"
  1504. char *strcpy2(s1, s2)
  1505.     register char s1[], s2[];
  1506.     {
  1507.     char *s0 = s1;
  1508.  
  1509.     while ((*s1++ = *s2++) != '\0')
  1510.         ;
  1511.     return (s0);
  1512.     }
  1513. ###string.c
  1514. /* string - practice with character arrays
  1515.  */
  1516. #include "local.h"
  1517. main()
  1518.     {
  1519.     char a1[BUFSIZ];
  1520.     char a2[BUFSIZ];
  1521.  
  1522.     strcpy(a1, "every ");
  1523.     strcpy(a2, "good boy ");
  1524.     strcat(a2, "does ");
  1525.     if (strlen(a1) < strlen(a2))
  1526.         strcat(a2, "fine ");
  1527.     else
  1528.         strcat(a1, "very ");
  1529.     if (strcmp(a1, a2) < 0)
  1530.         {
  1531.         strcat(a1, a2);
  1532.         printf("%s\n", a1);
  1533.         }
  1534.     else
  1535.         {
  1536.         strcat(a2, a1);
  1537.         printf("%s\n", a2);
  1538.         }
  1539.     }
  1540. ###strlen.c
  1541. /* strlen - return length of string s
  1542.  */
  1543. unsigned strlen(s)
  1544.     char s[];
  1545.     {
  1546.     unsigned i;
  1547.  
  1548.     for (i = 0; s[i] != '\0'; ++i)
  1549.         ;
  1550.     return (i);
  1551.     }
  1552. ###strncpy.c
  1553. /* strncpy - copy n bytes from s2 to s1 (using while)
  1554.  */
  1555. void strncpy(s1, s2, n)
  1556.     char s1[], s2[];
  1557.     unsigned n;
  1558.     {
  1559.     unsigned i;
  1560.  
  1561.     i = 0;
  1562.     while (i < n && s2[i] != '\0')
  1563.         {
  1564.         s1[i] = s2[i];
  1565.         ++i;
  1566.         }
  1567.     while (i < n)
  1568.         {
  1569.         s1[i] = '\0';
  1570.         ++i;
  1571.         }
  1572.     }
  1573. ###strncpy2.c
  1574. /* strncpy - copy n bytes from s2 to s1 (using for)
  1575.  */
  1576. void strncpy(s1, s2, n)
  1577.     char s1[], s2[];
  1578.     unsigned n;
  1579.     {
  1580.     unsigned i;
  1581.  
  1582.     for (i = 0; i < n && s2[i] != '\0'; ++i)
  1583.         s1[i] = s2[i];
  1584.     for ( ; i < n; ++i)
  1585.         s1[i] = '\0';
  1586.     }
  1587. ###strscn.c
  1588. /* strscn - return the index of c in string s
  1589.  */
  1590. unsigned strscn(s, c)
  1591.     char s[];    /* string to be scanned */
  1592.     char c;        /* char to be matched */
  1593.     {
  1594.     unsigned i;
  1595.  
  1596.     for (i = 0; s[i] != c && s[i] != '\0'; ++i)
  1597.         ;
  1598.     return (i);
  1599.     }
  1600. ###swap.c
  1601. /* swap - interchange two short integers
  1602.  */
  1603. #include "local.h"
  1604. void swap(pi, pj)
  1605.     short *pi, *pj;
  1606.     {
  1607.     short t;
  1608.  
  1609.     t = *pi, *pi = *pj, *pj = t;
  1610.     }
  1611. ###testrand.c
  1612. #include "local.h"
  1613. short seed = 0;
  1614. short n = 0;
  1615. long rnum = 0;
  1616. short mysrand(n)
  1617. short n;
  1618.     {
  1619.     rnum = n;
  1620.     }
  1621. short myrand()
  1622.     {
  1623.     rnum = rnum * 0x41c64e6d + 0x3039;
  1624.     return ((rnum >> 16) & ~0x8000);
  1625.     }
  1626. main()
  1627.     {
  1628.     srand(seed);
  1629.     mysrand(seed);
  1630.     while (myrand() == rand())
  1631.         putchar('x');
  1632.     }
  1633. ###tolower.c
  1634. /* tolower - convert upper-case letter to lower case
  1635.  */
  1636. metachar tolower(c)
  1637.     metachar c;
  1638.     {
  1639.     return (isupper(c) ? c + 'a' - 'A' : c);
  1640.     }
  1641. ###toupper.c
  1642. /* toupper - convert lower-case letter to upper case
  1643.  */
  1644. metachar toupper(c)
  1645.     metachar c;
  1646.     {
  1647.     return (islower(c) ? c + 'A' - 'a' : c);
  1648.     }
  1649. ###tower.c
  1650. /* tower - solve Tower of Hanoi
  1651.  */
  1652. #include <stdio.h>
  1653. #include "local.h"
  1654. #include "pegs.h"
  1655. main()
  1656.     {
  1657.     short i;    /* index over disks */
  1658.  
  1659.     for (i = 1; i <= NDISKS; ++i)
  1660.         push(0, NDISKS + 1 - i);
  1661.     printf("Start:\n");
  1662.     dumppg();
  1663.     move(NDISKS, 0, 2);
  1664.     printf("Finish:\n");
  1665.     dumppg();
  1666.     }
  1667. /* move - move n disks from peg p0 to peg p1
  1668.  */
  1669. void move(n, p0, p1)
  1670. short n;    /* how many disks to move */
  1671. short p0;    /* from which peg */
  1672. short p1;    /* to which peg */
  1673.     {
  1674.     short last;        /* the last disk to move */
  1675.     short other();
  1676.  
  1677.     if (1 < n)
  1678.         move(n - 1, p0, other(p0, p1));
  1679.     last = pop(p0);
  1680.     push(p1, last);
  1681.     printf("Move disk %d from %d to %d\n", last, p0, p1);
  1682.     if (1 < n)
  1683.         move(n - 1, other(p0, p1), p1);
  1684.     }
  1685. /* other - return the number of peg other than p0 and p1
  1686.  */
  1687. short other(p0, p1)
  1688. short p0;
  1689. short p1;
  1690.     {
  1691.     short n;
  1692.     static short otherpeg[8] =     {9, 9, 9, 2, 9, 1, 0, 9};
  1693.  
  1694.     n = otherpeg[(1 << p0) + (1 << p1)];
  1695.     if (n == 9)
  1696.         {
  1697.         printf("No other peg for %d, %d\n", p0, p1);
  1698.         exit(FAIL);
  1699.         }
  1700.     else
  1701.         return (n);
  1702.     }
  1703. ###vacat.c
  1704. /* vacat - structure practice
  1705.  */
  1706. #include "local.h"
  1707. #include "task.h"
  1708. main()
  1709.     {
  1710.     static TASK vacation =     {"leave for Hawaii", 1984, 0, 0};
  1711.  
  1712.     vacation.start = vacation.finish = (long)1983;
  1713.     printf("vacation.desc = %s\n", vacation.desc);
  1714.     printf("vacation.plan = %ld\n", vacation.plan);
  1715.     printf("vacation.start = %ld\n", vacation.start);
  1716.     printf("vacation.finish = %ld\n", vacation.finish);
  1717.     }
  1718. ###x.c
  1719. #include "local.h"
  1720. short a = 2;
  1721. static short b = 3;
  1722. main()
  1723.     {
  1724.     short c = a + b;
  1725.  
  1726.     xsub(c);
  1727.     }
  1728. xsub(d)
  1729.     short d;
  1730.     {
  1731.     short e = 7 * d;
  1732.  
  1733.     ysub(e);
  1734.     }
  1735. ###ysub.c
  1736. #include "local.h"
  1737. ysub(f)
  1738.     short f;
  1739.     {
  1740.     extern short a;
  1741.  
  1742.     printf("%d\n", a + f);
  1743.     }
  1744. ###EOF
  1745.