|
|
1.1 ! root 1: #print ! 2: The function getnum actually returns -1 when it ! 3: encounters end of file. (The source is in getnum.c ! 4: if you're interested.) ! 5: Write, compile and run a program that ! 6: reads numbers one per line with getnum ! 7: and, for each, prints: ! 8: ! 9: small if the number is >0 and <=100 ! 10: big if the number is >100 and <=1000 ! 11: huge if the number is >1000. ! 12: ! 13: Type "ready" when you're done. ! 14: #once cp %s/getnum.o . ! 15: #once cp %s/getnum.c . ! 16: #once #create Ref ! 17: 1001 ! 18: 1000 ! 19: 999 ! 20: 101 ! 21: 100 ! 22: 1 ! 23: #once #create Ref1 ! 24: huge ! 25: big ! 26: big ! 27: big ! 28: small ! 29: small ! 30: #user ! 31: a.out <Ref >test ! 32: #cmp Ref1 test ! 33: #succeed ! 34: /* One way:*/ ! 35: ! 36: main() { ! 37: int n; ! 38: ! 39: while ((n = getnum()) >= 0) ! 40: if (n > 0 && n <= 100) ! 41: printf("small\n"); ! 42: else if (n > 100 && n <= 1000) ! 43: printf("big\n"); ! 44: else if (n > 1000) ! 45: printf("huge\n"); ! 46: } ! 47: ! 48: /* Notice that in principle n could be negative, ! 49: so we need the last case to say ! 50: else if (n > 1000) ! 51: instead of just falling into it with a bare ! 52: else ! 53: ! 54: Also it's a good idea to indent the else-if's ! 55: exactly the way they are here; otherwise ! 56: you'll lose track of what's going on. ! 57: **/ ! 58: #log ! 59: #next ! 60: 13.1a 10
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.