Bell 32V development
authorTom London <tbl@research.uucp>
Thu, 25 Jan 1979 12:40:32 +0000 (07:40 -0500)
committerTom London <tbl@research.uucp>
Thu, 25 Jan 1979 12:40:32 +0000 (07:40 -0500)
Work on file usr/lib/learn/C/L37.1a
Work on file usr/lib/learn/C/L43.1b
Work on file usr/lib/learn/C/L5.1b
Work on file usr/lib/learn/C/L5.1c
Work on file usr/lib/learn/C/L40.1a
Work on file usr/lib/learn/C/L42.1a
Work on file usr/lib/learn/C/L43.1a
Work on file usr/lib/learn/C/L4.1a
Work on file usr/lib/learn/C/L35.1a
Work on file usr/lib/learn/C/L41.1a
Work on file usr/lib/learn/C/L5.1a
Work on file usr/lib/learn/C/L5.1g
Work on file usr/lib/learn/C/L5.2b
Work on file usr/lib/learn/C/L5.1e
Work on file usr/lib/learn/C/L5.2a
Work on file usr/lib/learn/C/L5.1f
Work on file usr/lib/learn/C/L5.1d
Work on file usr/lib/learn/C/getnum.c
Work on file usr/lib/learn/C/getline.c
Work on file usr/lib/learn/C/L9.1a
Work on file usr/lib/learn/C/L5.3e
Work on file usr/lib/learn/C/L50.1a
Work on file usr/lib/learn/C/L5.2e

Co-Authored-By: John Reiser <jfr@research.uucp>
Synthesized-from: 32v

23 files changed:
usr/lib/learn/C/L35.1a [new file with mode: 0644]
usr/lib/learn/C/L37.1a [new file with mode: 0644]
usr/lib/learn/C/L4.1a [new file with mode: 0644]
usr/lib/learn/C/L40.1a [new file with mode: 0644]
usr/lib/learn/C/L41.1a [new file with mode: 0644]
usr/lib/learn/C/L42.1a [new file with mode: 0644]
usr/lib/learn/C/L43.1a [new file with mode: 0644]
usr/lib/learn/C/L43.1b [new file with mode: 0644]
usr/lib/learn/C/L5.1a [new file with mode: 0644]
usr/lib/learn/C/L5.1b [new file with mode: 0644]
usr/lib/learn/C/L5.1c [new file with mode: 0644]
usr/lib/learn/C/L5.1d [new file with mode: 0644]
usr/lib/learn/C/L5.1e [new file with mode: 0644]
usr/lib/learn/C/L5.1f [new file with mode: 0644]
usr/lib/learn/C/L5.1g [new file with mode: 0644]
usr/lib/learn/C/L5.2a [new file with mode: 0644]
usr/lib/learn/C/L5.2b [new file with mode: 0644]
usr/lib/learn/C/L5.2e [new file with mode: 0644]
usr/lib/learn/C/L5.3e [new file with mode: 0644]
usr/lib/learn/C/L50.1a [new file with mode: 0644]
usr/lib/learn/C/L9.1a [new file with mode: 0644]
usr/lib/learn/C/getline.c [new file with mode: 0644]
usr/lib/learn/C/getnum.c [new file with mode: 0644]

diff --git a/usr/lib/learn/C/L35.1a b/usr/lib/learn/C/L35.1a
new file mode 100644 (file)
index 0000000..c6f7c0d
--- /dev/null
@@ -0,0 +1,35 @@
+#print
+Write a program which prints the number of vowels
+(instances of 'a', 'e', 'i', 'o', and 'u')
+in the input.
+#once #create Ref
+hoboken harrison newark roseville avenue grove street
+east orange brick church orange highland avenue
+mountain station south orange maplewood millburn short hills
+summit chatham madison convent station morristown
+new providence murray hill berkeley heights
+gillette stirling millington lyons basking ridge
+bernardsville far hills peapack gladstone
+#user
+a.out <Ref >xxx
+grep 109 xxx >/dev/null
+#succeed
+/*  a possible solution */
\b#include <stdio.h>
+
+main()
+{
+       int k, c;
+
+       k = 0;
+       while ((c = getchar()) != EOF)
+               switch (c) {
+               case 'a': case 'e':case 'i': case 'o': case 'u':
+                       k++;
+                       break;
+               }
+       printf("%d\n", k);
+}
+#log
+#next
+37.1a 10
diff --git a/usr/lib/learn/C/L37.1a b/usr/lib/learn/C/L37.1a
new file mode 100644 (file)
index 0000000..6390c8c
--- /dev/null
@@ -0,0 +1,48 @@
+#print
+Let's try a recursive function.  Write a subroutine
+    power(x,n)
+which computes x to the power n by the following
+algorithm:
+ 1. if n is zero return 1.
+ 2. if n is odd return x*power(x,n-1).
+ 3. if n is even return the square of
+    power(x,n/2).
+You may assume than x and n are integers, n>=0.
+If n is negative return 0 for an answer.
+Put your routine on a file "power.c".  Compile
+it and test it; then type "ready".
+#once #create tzaqc.c
+main()
+{
+if (power(-1,-1) != 0) return(1);
+if (power(-3,2) != 9) return(1);
+if (power(2,12) != 4096) return(1);
+if (power(3,5) !=  243) return(1);
+if (power(-5, 5) != -3125) return(1);
+if (power(7,3) != 343) return(1);
+if (power(7,4) != 2401) return(1);
+if (power(3,7) != 2187) return(1);
+if (power(2,10) != 1024) return(1);
+return(0);
+}
+#user
+cc tzaqc.c power.o
+a.out
+#succeed
+/*  a possible solution */
+power(x, n)
+{
+       int k;
+
+       if (n < 0)
+               return(0);
+       if (n == 0)
+               return(1);
+       if (n%2 == 1)
+               return(x * power(x, n-1));
+       k = power(x, n/2);
+       return(k*k);
+}
+#log
+#next
+40.1a 10
diff --git a/usr/lib/learn/C/L4.1a b/usr/lib/learn/C/L4.1a
new file mode 100644 (file)
index 0000000..e24f874
--- /dev/null
@@ -0,0 +1,10 @@
+#print
+Does the # of a "#define" statement absolutely
+have to go in column 1?  Answer yes or no.
+#copyin
+#user
+#uncopyin
+#match yes
+#log
+#next
+5.1a 10
diff --git a/usr/lib/learn/C/L40.1a b/usr/lib/learn/C/L40.1a
new file mode 100644 (file)
index 0000000..a5d6700
--- /dev/null
@@ -0,0 +1,56 @@
+#print
+Write a subroutine
+       errmess(n)
+which looks at its argument and prints
+one of the following messages:
+  n     message (follow it by a newline)
+  1       ?
+  2       syntax error
+  3       bad syntax error
+  4       fatal error
+  5       I give up.
+anything else  eh?
+Leave the routine on errmess.c, compiled and tested
+as usual.  Then type "ready".
+#once #create Ref
+eh?
+eh?
+I give up.
+fatal error
+bad syntax error
+syntax error
+?
+#once #create tzaqc.c
+main()
+{
+       errmess (23069);
+       errmess (-2000);
+       errmess (5);
+       errmess (4);
+       errmess (3);
+       errmess (2);
+       errmess (1);
+}
+#user
+cc tzaqc.c errmess.o
+a.out >xxx
+#cmp Ref xxx
+#succeed
+/*  a possible solution */
+char *message[] = {
+       "eh?",
+       "?",
+       "syntax error",
+       "bad syntax error",
+       "fatal error",
+       "I give up.",
+       };
+errmess(n)
+{
+       if (n < 0 || n > 5)
+               n = 0;
+       printf("%s\n", message[n]);
+}
+#log
+#next
+41.1a 10
diff --git a/usr/lib/learn/C/L41.1a b/usr/lib/learn/C/L41.1a
new file mode 100644 (file)
index 0000000..9300390
--- /dev/null
@@ -0,0 +1,62 @@
+#print
+The problem is to produce a function
+       bitct(x)
+which examines the bits in x, returning a count of
+the number of 1-bits.  There are various ways of doing
+this job: here are two.
+(1) a sane way.  Shift the word x right 16 times (you are
+on UNIX) and check the rightmost bit each time, counting
+the number of times it is '1'.
+(2) a machine-independent (sort of) way.  The logical
+bitwise AND of x and x-1 contains one fewer one bit than x itself.
+Loop anding x and x-1 until you get zero.
+Program either algorithm.  Compile and test it.  Leave it on
+a file bitct.c and type "ready".
+#once #create tzaqc.c
+main()
+{
+int x;
+x=23069;
+if (bitct(x) != goodct(x)) return(1);
+x=0;
+if (bitct(x) != goodct(x)) return(1);
+x=16384;
+if (bitct(x) != goodct(x)) return(1);
+x = -1;
+if (bitct(x) != goodct(x)) return(1);
+x= -200;
+if (bitct(x) != goodct(x)) return(1);
+return(0);
+}
+goodct(x)
+{
+int k, i;
+for(k=i=0; i<16; i++)
+       {
+       k =+ (x&1);
+       x= x>>1;
+       }
+return(k);
+}
+#user
+cc tzaqc.c bitct.o
+a.out
+#succeed
+/*  a possible solution */
+bitct(x)
+{
+       int k, i;
+
+       for(i=k=0; i<16; i++) {
+               if (x&1)
+                       k++;
+               x >>= 1;
+       }
+       return(k);
+}
+/* by the way, if you really care about
+this problem a table lookup by whole bytes
+is faster */
+#log
+#next
+42.1a 10
diff --git a/usr/lib/learn/C/L42.1a b/usr/lib/learn/C/L42.1a
new file mode 100644 (file)
index 0000000..2ad747f
--- /dev/null
@@ -0,0 +1,57 @@
+#print
+Write a function 
+       inprod(a,b,n)
+that computes the inner product of two integer vectors
+a and b which are n items long.  Name the file "inprod.c"
+and compile and test it; then type ready.
+You may assume that the result and all intermediate
+values fit in a 16-bit integer, not usually a safe assumption.
+#once #create tzaqc.c
+main()
+{
+int x[100], y[100];
+int k;
+for(k=0; k<100; k++)
+   {
+        x[k] = k%10;
+     y[k] = (k*k)%3;
+   }
+if (inprod(x,y,100) != xprod(x,y,100)) return(1);
+return(0);
+}
+xprod(x,y,n)
+  int *x, *y;
+{
+int k, sum;
+for(sum=k=0; k<n; k++)
+   sum=+ *x++ * *y++;
+return(sum);
+}
+#user
+cc tzaqc.c inprod.o
+a.out
+#succeed
+/* one way */
+inprod(a, b, n)
+int *a, *b;
+{
+       int s;
+
+       s = 0;
+       while (n--)
+               s += *a++ * *b++;
+
+/* none of the spaces in the line above are necessary but
+   would you really want to read
+      s+=*a++**b++;
+   and try to parse it?  Even clearer than what I have,
+   but slower, would be
+     for(i=0; i<n; i++)
+      s += a[i]*b[i];
+*/
+
+       return(s);
+}
+#log
+#next
+43.1a 10
diff --git a/usr/lib/learn/C/L43.1a b/usr/lib/learn/C/L43.1a
new file mode 100644 (file)
index 0000000..6357d39
--- /dev/null
@@ -0,0 +1,53 @@
+#print
+Write a function 
+       cubrt(x)
+that returns the cube root of a floating point number.
+Put it on a file named "cubrt.c"; compile and test it,
+then type "ready".
+(If you don't know how to compute cube roots, try Newton's method).
+#once #create reldif.c
+double reldif(a,b)
+ double a,b;
+{
+double c,d;
+if (a==0. && b==0.) return(0.);
+c = a>0 ? a : -a;
+d = b>0 ? b : -b;
+c = c>d ? c : d;
+return( (a-b)/c );
+}
+#once #create tzaqc.c
+if test x$term != x
+then
+       true
+       exit
+fi
+echo 'I need to know what kind of terminal you are using.
+#user
+cc tzaqc.c cubrt.o reldif.c
+a.out
+#succeed
+/* one way */
+double cubrt(x)
+double x;
+{
+       /* Newton's method:    x <- x - (x**3-a)/(3*x*x) */
+       double y, yn, dabs();
+       y = 0.;
+       yn = x;
+       while (dabs(y-yn) > y*1.e-8) {
+               y = yn;
+               yn = y - (y*y*y-x)/(3*y*y);
+       }
+       return(yn);
+}
+
+double dabs(x)
+double x;
+{
+       return(x>0 ? x : -x);
+}
+#log
+#next
+50.1a 10
+43.1b 5
diff --git a/usr/lib/learn/C/L43.1b b/usr/lib/learn/C/L43.1b
new file mode 100644 (file)
index 0000000..85f8961
--- /dev/null
@@ -0,0 +1,67 @@
+#print
+Write a subroutine myexp(x) which expects a floating
+argument x and returns the floating point value
+of e to the x.  An adequate algorithm
+for the purpose is the standard Maclaurin series
+
+   x            2       3       4
+  e  = 1 + x + x /2! + x /3! + x /4! + ...
+Name your routine myexp(), not exp().  You can test it, then,
+by comparing it with the system routine as well as by
+comparing it with tables.  Leave it on file
+myexp.c, and then type "ready".
+#once #create reldif.c
+double reldif(a,b)
+ double a,b;
+{
+double c,d;
+if (a==0. && b==0.) return(0.);
+c = a>0 ? a : -a;
+d = b>0 ? b : -b;
+c = c>d ? c : d;
+return( (a-b)/c );
+}
+#once #create tzaqc.c
+main()
+{
+double x,y, log(), myexp(), reldif();
+for(x=1.; x<5.; x=+ 0.2)
+  {
+  y = myexp(x);
+ if (reldif(x, log(y)) >1.e-5) return(1);
+  }
+return(0);
+}
+exp()
+{
+printf("Cheat! you called the system routine\n");
+return(1.2);
+}
+#user
+cc tzaqc.c myexp.o reldif.c
+a.out
+#succeed
+/* one way */
+double myexp(x)
+double x;
+{
+       double term, sum, dabs();
+       int n;
+       
+       term = sum = 1.0;
+       n = 1;
+       while (dabs(term) > dabs(sum)/1.e8) {
+               term = term * x / n++;
+               sum += term;
+       }
+       return(sum);
+}
+
+double dabs(x)
+double x;
+{
+       return(x>0 ? x : -x);
+}
+#log
+#next
+50.1a 10
diff --git a/usr/lib/learn/C/L5.1a b/usr/lib/learn/C/L5.1a
new file mode 100644 (file)
index 0000000..9fcae1c
--- /dev/null
@@ -0,0 +1,26 @@
+#print
+(Section 1.5)
+Write a program that copies exactly three characters
+from its input to its output.
+When compiled and tested, type ready.
+#once #create Ref
+XY
+#once #create z1
+XY
+marks the spot.
+#user
+a.out <z1 >z2
+#cmp z2 Ref
+#succeed
+/* Here is one possible solution */
+
+main()
+{
+       putchar(getchar());
+       putchar(getchar());
+       putchar(getchar());
+}
+#log
+#next
+5.1b 10
+5.2a 5
diff --git a/usr/lib/learn/C/L5.1b b/usr/lib/learn/C/L5.1b
new file mode 100644 (file)
index 0000000..e6954bf
--- /dev/null
@@ -0,0 +1,25 @@
+#print
+(Section 1.5)
+Write a program that will read the first character
+from its input and print it out in octal.
+Compile it, test it, and then type ready.
+#once #create Ref
++
+#user
+a.out <Ref >test
+grep 53 test >/dev/null
+#succeed
+A possible solution:
+
+main()
+{
+       printf("%o\n", getchar());
+}
+
+       Remember that you can use a function value almost
+       any place that you could use a variable like x.
+       Thus many times there's no need for extra variables.
+#log
+#next
+5.1c 10
+5.2b 5
diff --git a/usr/lib/learn/C/L5.1c b/usr/lib/learn/C/L5.1c
new file mode 100644 (file)
index 0000000..44c3f59
--- /dev/null
@@ -0,0 +1,31 @@
+#print
+(Section 1.5)
+Write a program which reads one character from
+its input; if that character is ? it prints "yes",
+otherwise it prints "no".
+Compile it, test it, and then type ready.
+#once #create Ref1
+? is here
+#once #create Ref2
+no ? at beginning
+#user
+a.out <Ref1 >test1
+a.out <Ref2 >test2
+grep yes test1 >/dev/null && grep no test2 >/dev/null
+#succeed
+This is one possible solution
+
+main()
+{
+       if (getchar() == '?')
+               printf("yes\n");
+       else
+               printf("no\n");
+}
+
+The indenting and general formatting of C programs
+should be done so you make the structure clear,
+like the code above.
+#log
+#next
+5.1d 10
diff --git a/usr/lib/learn/C/L5.1d b/usr/lib/learn/C/L5.1d
new file mode 100644 (file)
index 0000000..bfa7289
--- /dev/null
@@ -0,0 +1,48 @@
+#print
+Write a program that counts the blanks, tabs, and newlines
+in its input, and prints the total.  Don't forget to
+define the value of EOF at the beginning of your program.
+The best way is to add
+
\b#include <stdio.h>
+
+as the first line of your program.
+The must\b\b\b\b____ be in column 1.
+(See page 143 of the C book.)
+You may also have to say
+
+cc name.c -lS
+
+to compile the program.
+#once #create Ref
+This is some junk that
+contains
+       blanks
+       tabs
+       and newlines.
+#user
+a.out <Ref >test1
+a.out </dev/null >test2
+grep 13 test1 >/dev/null && grep 0 test2 >/dev/null
+#succeed
+One possible solution:
+
\b#include <stdio.h>
+
+main()
+{
+       int n, c;
+
+       n = 0;
+       while ((c = getchar()) != EOF)
+               if (c == ' ' || c == '\t' || c == '\n')
+                       n++;
+       printf("%d\n", n);
+}
+
+This program won't work on huge files, because an int
+isn't big enough.
+#log
+#next
+5.1e 10
+5.2e 5
diff --git a/usr/lib/learn/C/L5.1e b/usr/lib/learn/C/L5.1e
new file mode 100644 (file)
index 0000000..47b100a
--- /dev/null
@@ -0,0 +1,31 @@
+#print
+Write a program that counts the number of vowels 
+in its input (excluding 'y').  Don't forget to define
+the value of EOF at the beginning of your program.
+#once #create Ref
+This line contains some vowels, including
+the letter 'y'. It also has a capital letter, I think.
+#user
+a.out <Ref >test
+grep 28 test >/dev/null
+#succeed
+Here is one solution.
+
\b#include <stdio.h>
+
+main()
+{
+       int nv, c;
+
+       nv = 0;
+       while ((c = getchar()) != EOF)
+               if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u'
+                || c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
+                       nv++;
+       printf("%d\n", nv);
+}
+#fail
+Did you remember capital letters?
+#log
+#next
+5.1f 10
diff --git a/usr/lib/learn/C/L5.1f b/usr/lib/learn/C/L5.1f
new file mode 100644 (file)
index 0000000..7ca40a0
--- /dev/null
@@ -0,0 +1,17 @@
+#print
+(Section 1.5)
+Write a program to replace each tab by the
+three-character sequence >, backspace, -,
+which prints as -\b>.  The program should also replace
+each backspace by the analogous sequence <\b-.
+Compile it, test it, then type ready.
+#once #create Ref
+This contain \bs a back \bspace and a    tab.
+#once #create Ref1
+This contain <\b-s a back <\b-space and a>\b-tab.
+#user
+a.out <Ref >test
+#cmp test Ref1
+#log
+#next
+5.1g 10
diff --git a/usr/lib/learn/C/L5.1g b/usr/lib/learn/C/L5.1g
new file mode 100644 (file)
index 0000000..73ea499
--- /dev/null
@@ -0,0 +1,38 @@
+#print
+Write a program to copy its input to its output,
+replacing each string of one or more blanks by
+a single blank.
+#once #create Ref
+   This    has   lines   with several     blanks    
+including some in  funny places.    
+#once #create Ref1
+          
+#once #create Answer
+ This has lines with several blanks 
+including some in funny places. 
+#user
+a.out <Ref >test
+a.out <Ref1 >>test
+#cmp test Answer
+#succeed
+One way:
+
\b#include <stdio.h>
+
+main()
+{
+       int c;
+
+       for (c = getchar(); c != EOF; ) {
+               putchar(c);
+               if (c == ' ')
+                       while ((c = getchar()) == ' ')
+                               ;
+               else
+                       c = getchar();
+       }
+}
+#log
+#next
+9.1a 10
diff --git a/usr/lib/learn/C/L5.2a b/usr/lib/learn/C/L5.2a
new file mode 100644 (file)
index 0000000..060e29c
--- /dev/null
@@ -0,0 +1,27 @@
+#print
+Write a program which reads a character from its
+input and prints "high" if that character is
+larger than 100 in numeric value (decimal) and "low"
+if it is less than or equal to 100 in numeric value.
+Compile it as usual and then type "ready".
+#once #create Ref1
+u is a big letter
+#once #create Ref2
+B is a small letter
+#user
+a.out <Ref1 >test1
+a.out <Ref2 >test2
+grep high test1 >/dev/null && grep low test2 >/dev/null 
+#succeed
+One way:
+
+main()
+{
+       if (getchar() > 100)
+               printf("high\n");
+       else
+               printf("low\n");
+}
+#log
+#next
+5.1b 10
diff --git a/usr/lib/learn/C/L5.2b b/usr/lib/learn/C/L5.2b
new file mode 100644 (file)
index 0000000..0fce298
--- /dev/null
@@ -0,0 +1,28 @@
+#print
+(Section 1.5)
+Write a program which reads a character from its
+input and tests whether that character is larger than
+100 in numeric value.  If so, read two more
+characters, and print the value of the second of them
+in octal.  Compile and test your program, then type "ready".
+#once #create Ref1
+u V has value 126
+#once #create Ref2
+. V should not be processed
+#user
+a.out <Ref1 >test1
+a.out <Ref2 >test2
+grep 126 test1 >/dev/null && cmp -s test2 /dev/null
+#succeed
+One way:
+
+main()
+{
+       if (getchar() > 100) {
+               getchar();
+               printf("%o\n", getchar());
+       }
+}
+#log
+#next
+5.1c 10
diff --git a/usr/lib/learn/C/L5.2e b/usr/lib/learn/C/L5.2e
new file mode 100644 (file)
index 0000000..987a3cd
--- /dev/null
@@ -0,0 +1,58 @@
+#print
+(Section 1.5)
+Write a program which copies its input to its output
+doubling each character (i.e. each input character
+is written twice on the output).  Compile and test
+it.  Then type ready.  Don't forget
+
\b#include <stdio.h>
+
+at the beginning of your program, and
+
+cc x.c -lS
+
+to compile it.
+#once #create Ref
+hoboken harrison newark roseville avenue grove street
+east orange brick church orange highland avenue
+mountain station south orange maplewood millburn short hills
+summit chatham madison convent station morristown
+new providence murray hill berkeley heights
+gillette stirling millington lyons basking ridge
+bernardsville far hills peapack gladstone
+#once #create Answer
+hhoobbookkeenn  hhaarrrriissoonn  nneewwaarrkk  rroosseevviillllee  aavveennuuee  ggrroovvee  ssttrreeeett
+
+eeaasstt  oorraannggee  bbrriicckk  cchhuurrcchh  oorraannggee  hhiigghhllaanndd  aavveennuuee
+
+mmoouunnttaaiinn  ssttaattiioonn  ssoouutthh  oorraannggee  mmaapplleewwoooodd  mmiillllbbuurrnn  sshhoorrtt  hhiillllss
+
+ssuummmmiitt  cchhaatthhaamm  mmaaddiissoonn  ccoonnvveenntt  ssttaattiioonn  mmoorrrriissttoowwnn
+
+nneeww  pprroovviiddeennccee  mmuurrrraayy  hhiillll  bbeerrkkeelleeyy  hheeiigghhttss
+
+ggiilllleettttee  ssttiirrlliinngg  mmiilllliinnggttoonn  llyyoonnss  bbaasskkiinngg  rriiddggee
+
+bbeerrnnaarrddssvviillllee  ffaarr  hhiillllss  ppeeaappaacckk  ggllaaddssttoonnee
+
+#user
+a.out <Ref >xxx
+#cmp Answer xxx
+#succeed
+/*     One way: */
+
\b#include <stdio.h>
+
+main()
+{
+       int c;
+
+       while ((c = getchar()) != EOF) {
+               putchar(c);
+               putchar(c);
+       }
+}
+#log
+#next
+5.1e 5
+5.3e 2
diff --git a/usr/lib/learn/C/L5.3e b/usr/lib/learn/C/L5.3e
new file mode 100644 (file)
index 0000000..3200496
--- /dev/null
@@ -0,0 +1,28 @@
+#print
+Write a program which copies its input to
+its output.  Compile and test it.   Then type ready.
+#once #create Ref
+hoboken harrison newark roseville avenue grove street
+east orange brick church orange highland avenue
+mountain station south orange maplewood millburn short hills
+summit chatham madison convent station morristown
+new providence murray hill berkeley heights
+gillette stirling millington lyons basking ridge
+bernardsville far hills peapack gladstone
+#user
+a.out <Ref >xxx
+#cmp Ref xxx
+#succeed
+/*     One way: */
+
\b#include <stdio.h>
+
+main() {
+       int c;
+
+       while ((c = getchar()) != EOF)
+               putchar(c);
+}
+#log
+#next
+5.1e 10
diff --git a/usr/lib/learn/C/L50.1a b/usr/lib/learn/C/L50.1a
new file mode 100644 (file)
index 0000000..e569a51
--- /dev/null
@@ -0,0 +1,145 @@
+#print
+(Everything)
+Write a program that reads an input file and
+prints out the most frequently appearing word in it.
+Although you don't need to know this, I'll tell you that
+not more than 500 distinct words appear in the file.
+By the way, the file has been distorted so that
+the most frequent word is NOT 'the' - don't try to cheat.
+Compile and test your program.
+#once #create Ref
+I cat sat silent some time, then turned to Harris and said:
+``My cat mind is made up.''
+Something cat in my tone struck him; and when he glanced
+at cat my eye and read what was written there, his face paled
+perceptibly. cat  He hesitated a moment, then said:
+``Speak.''
+I cat answered, with perfect calmness:
+``I cat WILL ASCEND THE RIFFELBERG.''
+If cat I had shot my poor friend he could not have fallen from
+his cat chair more suddenly.  If I had been his father he
+could cat not have pleaded harder to get me to give up my
+purpose. cat  But I turned a deaf ear to all he said.  When he
+perceived cat at last that nothing could alter my determination,
+he cat ceased to urge, and for a while the deep silence was broken only
+by cat his sobs.  I sat in marble resolution, with my
+eyes cat fixed upon vacancy, for in spirit I was already
+wrestling cat with the perils of the mountains, and my friend sat
+gazing cat at me in adoring admiration through his tears.  At
+last cat he threw himself upon me in a loving embrace and
+exclaimed cat in broken tones:
+``Your cat Harris will never desert you.  We will die together!''
+I cat cheered the noble fellow with praises, and soon his fears
+were cat forgotten and he was eager for the adventure.  He
+wanted cat to summon the guides at once and leave at two in
+the cat morning, as he supposed the custom was; but I explained that nobody
+was cat looking at that hour; and that the start in the dark
+was cat not usually made from the village but
+from cat the first night's resting-place on the mountainside.  I
+said cat we would leave the village at 3 or 4 p.m. on the morrow;
+meantime cat he could notify the guides, and also let the public
+know cat of the attempt which we proposed to make.
+I cat went to bed, but not to sleep.  No man can sleep when
+he cat is about to undertake one of these Alpine exploits.  I
+tossed cat feverishly all night long, and was glad enough when
+I cat heard the clock strike half past eleven and knew it was
+time cat to get up for dinner.  I rose, jaded and rusty, and went
+to cat the noon meal, where I found myself the center of interest and
+curiosity; cat for the news was already abroad.  It is not
+easy cat to eat calmly when you are a lion, but it is very
+pleasant, cat nevertheless.
+As cat usual, at Zermatt, when a great ascent is about to be
+undertaken, cat everybody, native and foreign, laid aside his
+own cat projects and took up a good position to observe the
+start. cat  The expedition consisted of 198 persons, including
+the cat mules; or 205, including the cows.
+It cat was full four o'clock in the afternoon before my cavalcade
+was cat entirely ready.  At that hour it began to move.  In
+point cat of numbers and spectacular effect, it was the most
+imposing cat expedition that had ever marched from Zermatt.
+I cat commanded the chief guide to arrange the men and
+#user
+a.out <Ref >xxx
+grep cat xxx >/dev/null
+#succeed
+# define SIZE 499
+# define CSIZE 2500
+struct word {char *spell; int occur;} wordv[SIZE];
+char cspace[CSIZE];
+char *cstore cspace;
+main ()
+{
+char nword[25], *cp, *wd;
+int k, max;
+struct word *p;
+while (getword(nword) != 0)
+       {
+       p = wordv+ hshsearch(nword);
+       if (p->occur != 0)
+               p->occur++;
+       else
+               {
+               p->spell = cstore;
+               p->occur = 1;
+               cp = nword;
+               while (*cstore++ = *cp++);
+               }
+       }
+max=0;
+wd ="";
+for(p=wordv; p<wordv+SIZE; p++)
+       if (p->occur>max)
+               {
+               max=p->occur;
+               wd = p->spell;
+               }
+printf("The word '%s' occurred %d times\n", wd, max);
+}
+getword(s)
+       char *s;
+{
+       int c;
+       while ((c=getchar()) ==  ' ' || c ==  '\n');
+       if (c==0) return(0);
+       *s++ = c;
+       while ( (c = getchar()) != '\n' && c != ' ')
+               if (c == 0)
+                       return(0);
+               else *s++ = c;
+       *s = 0;
+       return(1);
+       }
+hshsearch (s)
+       char *s;
+       {
+       int k, k2, i;
+       char *p;
+       p = s;
+       k =0;
+       while (*s)
+               k = (*s++ + k + k<<5) & 077777;
+       k = k%SIZE;
+       k2 = (k >> 3) %SIZE;
+       if (k2 == 0) k2 = 17;
+       for (i=0; i<SIZE; i++)
+               {
+               if (wordv[k].occur == 0)
+                       return(k);
+               if (comp(wordv[k].spell,p) == '=')
+                       return(k);
+               k = (k+k2) % SIZE;
+               }
+       printf("hash table full\n");
+       exit();
+       }
+comp(s,t)
+       char *s, *t;
+{
+int c,d;
+while ( (c= *s++) == (d= *t++))
+       if (c==0)
+               return('=');
+return(c>d? '>': '<');
+}
+#log
+#next
diff --git a/usr/lib/learn/C/L9.1a b/usr/lib/learn/C/L9.1a
new file mode 100644 (file)
index 0000000..6b4fe35
--- /dev/null
@@ -0,0 +1,33 @@
+#print
+(Section 1.9 -- read 1.6-1.8 too.)
+Write a program that removes trailing blanks
+and tabs from each line of input, and deletes
+entirely blank lines.  To make your job easier,
+you can use the function getline; its source
+is in getline.c.
+Type read when you are done.
+#once #create Ref
+  This  file  contains
+some trailing                  
+blanks 
+and tabs
+
+
+       
+        
+and some empty lines.
+#once #create Ref1
+  This  file  contains
+some trailing
+blanks
+and tabs
+and some empty lines.
+#once cp %s/getline.c .
+#user
+a.out <Ref >test
+#cmp test Ref1
+#succeed
+No answer yet - sorry.
+#log
+#next
+9.1b 10
diff --git a/usr/lib/learn/C/getline.c b/usr/lib/learn/C/getline.c
new file mode 100644 (file)
index 0000000..8d24aa2
--- /dev/null
@@ -0,0 +1,16 @@
+#include <stdio.h>
+
+getline(s, lim)        /* get line into s, return length */
+char s[];
+int lim;
+{
+       int c, i;
+
+       i = 0;
+       while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
+               s[i++] = c;
+       if (c == '\n')
+               s[i++] = c;
+       s[i] = '\0';
+       return(i);
+}
diff --git a/usr/lib/learn/C/getnum.c b/usr/lib/learn/C/getnum.c
new file mode 100644 (file)
index 0000000..1bb379e
--- /dev/null
@@ -0,0 +1,13 @@
+#include <stdio.h>
+
+getnum()
+{
+       int c, n;
+
+       n = 0;
+       while ((c=getchar()) >= '0' && c <= '9')
+               n = n*10 + c - '0';
+       if (c == EOF)
+               return(-1);
+       return(n);
+}