elementar2.c (1106B)
1 /* Exercise 5.33 */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <time.h> 6 7 int main() 8 { 9 int n1 = 1 + rand() % 9; 10 int n2 = 1 + rand() % 9; 11 int num; 12 13 14 while(1) { 15 srand( time(NULL) ); 16 17 printf("How much is %d times %d?\n", n1, n2); 18 printf("Answer (%d to end): ", EOF); 19 scanf("%d", &num); 20 21 if(num == EOF) 22 break; 23 24 printf("\n"); 25 if(num == n1 * n2) { 26 switch(1 + rand() % 4) { 27 case 1: 28 printf("Very good!"); 29 break; 30 case 2: 31 printf("Excellent!"); 32 break; 33 case 3: 34 printf("Nice work!"); 35 break; 36 case 4: 37 printf("Keep up the good work!"); 38 } 39 n1 = 1 + rand() % 9; 40 n2 = 1 + rand() % 9; 41 } 42 else 43 switch(1 + rand() % 4) { 44 case 1: 45 printf("No. Please try again."); 46 break; 47 case 2: 48 printf("Wrong. Try one more."); 49 break; 50 case 3: 51 printf("Don't give up!"); 52 break; 53 case 4: 54 printf("No. Keep trying."); 55 } /* end switch */ 56 57 printf("\n\n"); 58 } /* end while (num) */ 59 60 return 0; 61 } /* E0F main */ 62