elementar4.c (3181B)
1 /* Exercise 5.44 */ 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <time.h> 6 7 void pok(void); 8 void pko(void); 9 10 int main() 11 { 12 int n1, n2, num; /* random numbers and user input */ 13 int level, mode; /* level and mode */ 14 int mode2; /* mode2 needed for mode 5 */ 15 int correct = 0; /* counter for correct answer */ 16 int i; /* only a counter for loops */ 17 18 do { 19 printf("Choose a level.\n"); 20 printf( 21 "\t1: one number (easy)\n" 22 "\t2: two numbers (medium)\n" 23 ); 24 25 printf(": "); 26 scanf("%d", &level); 27 28 } while( level < 1 || level > 2 ); 29 30 level = level == 1 ? 9 : 99; /* set level */ 31 32 do { 33 printf("\nChoose a mode.\n"); 34 printf( 35 "\t1: addition (easy)\n" 36 "\t2: removal (medium)\n" 37 "\t3: moltiplication (hard)\n" 38 "\t4: division (very hard)\n" 39 "\t5: two operations (random)\n" 40 ); 41 42 printf(": "); 43 scanf("%d", &mode); 44 45 } while( mode < 1 || mode > 5 ); 46 47 printf("\n"); 48 49 for(i = 1; i <= 10; i++) { 50 srand( time(NULL) ); 51 52 n1 = 1 + rand() % level; 53 n2 = 1 + rand() % level; 54 55 if(mode == 5) 56 mode2 = 1 + rand() % 4; 57 else mode2 = mode; 58 59 switch(mode2) { /* set mode */ 60 case 1: /* addition */ 61 printf("How much is %d plus %d?\n", n1, n2); 62 printf("Answer: "); 63 scanf("%d", &num); 64 65 printf("\n"); 66 if(num == n1 + n2) { 67 pok(); 68 ++correct; 69 } 70 else 71 pko(); 72 break; 73 case 2: /* removal */ 74 printf("How much is %d minus %d?\n", n1, n2); 75 printf("Answer: "); 76 scanf("%d", &num); 77 78 printf("\n"); 79 if(num == n1 - n2) { 80 pok(); 81 ++correct; 82 } 83 else 84 pko(); 85 break; 86 case 3: /* moltiplication */ 87 printf("How much is %d times %d?\n", n1, n2); 88 printf("Answer: "); 89 scanf("%d", &num); 90 91 printf("\n"); 92 if(num == n1 * n2) { 93 pok(); 94 ++correct; 95 } 96 else 97 pko(); 98 break; 99 case 4: /* division */ 100 printf("How much is %d divided %d?\n", n1, n2); 101 printf("Answer: "); 102 scanf("%f", &num); 103 104 printf("\n"); 105 if(num == n1 / n2) { 106 pok(); 107 ++correct; 108 } 109 else 110 pko(); 111 } /* end switch (mode) */ 112 113 printf("\n\n"); 114 115 } /* end for (i) */ 116 117 if( (100 * correct) / 10 < 75) 118 printf("Please ask your instructor for extra help\n"); 119 120 printf("Game over!\n"); 121 122 return 0; 123 } /* E0F main */ 124 125 /* print a nice string :-) */ 126 void pok(void) 127 { 128 switch(1 + rand() % 4) { 129 case 1: 130 printf("Very good!"); 131 break; 132 case 2: 133 printf("Excellent!"); 134 break; 135 case 3: 136 printf("Nice work!"); 137 break; 138 case 4: 139 printf("Keep up the good work!"); 140 } 141 } /* eof pok() */ 142 143 /* print a less nice string :| */ 144 void pko(void) 145 { 146 switch(1 + rand() % 4) { 147 case 1: 148 printf("No. Please try again."); 149 break; 150 case 2: 151 printf("Wrong. Try one more."); 152 break; 153 case 3: 154 printf("Don't give up!"); 155 break; 156 case 4: 157 printf("No. Keep trying."); 158 } /* end switch */ 159 } /* eof pko() */ 160