diff options
Diffstat (limited to 'usr.bin/learn/lib/C/L12.1b')
-rw-r--r-- | usr.bin/learn/lib/C/L12.1b | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/usr.bin/learn/lib/C/L12.1b b/usr.bin/learn/lib/C/L12.1b new file mode 100644 index 00000000000..3f60b51bdec --- /dev/null +++ b/usr.bin/learn/lib/C/L12.1b @@ -0,0 +1,60 @@ +#print +The function getnum actually returns -1 when it +encounters end of file. (The source is in getnum.c +if you're interested.) +Write, compile and run a program that +reads numbers one per line with getnum +and, for each, prints: + +small if the number is >0 and <=100 +big if the number is >100 and <=1000 +huge if the number is >1000. + +Type "ready" when you're done. +#once cp %s/getnum.o . +#once cp %s/getnum.c . +#once #create Ref +1001 +1000 +999 +101 +100 +1 +#once #create Ref1 +huge +big +big +big +small +small +#user +a.out <Ref >test +#cmp Ref1 test +#succeed +/* One way:*/ + +main() { + int n; + + while ((n = getnum()) >= 0) + if (n > 0 && n <= 100) + printf("small\n"); + else if (n > 100 && n <= 1000) + printf("big\n"); + else if (n > 1000) + printf("huge\n"); +} + +/* Notice that in principle n could be negative, + so we need the last case to say + else if (n > 1000) + instead of just falling into it with a bare + else + + Also it's a good idea to indent the else-if's + exactly the way they are here; otherwise + you'll lose track of what's going on. +**/ +#log +#next +13.1a 10 |