1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
#include <cstdlib>
#include <stdio.h>
#include <time.h>
#define title "MathTest"
#define quests 9 // questions in one game\set
#define miss 6 // wrong answers allowed before gameover
//Addition test params (number range):
#define add_min 1
#define add_max 9
//Subtraction test params (number range):
#define sub_min 1
#define sub_max 9
//Multiplication test params (number range):
#define mul_min 2
#define mul_max 9
//Division test params (number range):
#define div_min 2
#define div_max 9
char uname[16]; // users name
int key = -1; // for main menu, keyboard input
int one, two, sol; // variables for test
int i, q;
int tries; // tries left (to give right answer)
//statistics:
int st_right, st_wrong, st_total, st_questions;
int sl_right, sl_wrong, sl_total;
bool solved, playing; // booleans
int random(int min, int max) //returns random in range min..max
{
return rand() % (max - min + 1) + min;
}
int random(int max) //returns a random in range 1..max
{
return rand() % max + 1;
}
void comment(char con) // returns a random comment\taunt for a specified situation
{
if (con == '~') // wrong answer
{
switch (random(3))
{
case 1: printf("Wrong answer (>_<)"); break;
case 2: printf("You should try again."); break;
case 3: printf("Be carefull."); break;
}
}
if (con == '@') // lost
{
switch (random(3))
{
case 1: printf("You lost the game (;_;)"); break;
case 2: printf("Game over."); break;
case 3: printf("How could you let this happen? (T_T)"); break;
}
}
if (con == '!') // right answer
{
printf("Right!");
// todo: add a switch statement here
}
if (con == 'v')
{
printf("Victory!");
// todo: add a switch statement here
}
if (con == '<') // lower!
{
printf("Try smaller number.");
// todo: add a switch statement here
}
if (con == '>') // higher!
{
printf("Try higher number.");
// todo: add a switch statement here
}
if (con == '+') // just a bit higher
{
printf("A bit higher...");
// todo: add a switch statement here
}
if (con == '-') // just a bit lower
{
printf("A bit lower...");
// todo: add a switch statement here
}
}
void stats() // displays global statistics
{
printf("\n\t***Game statistics***\n");
printf("\tTotal questions: %d\n", st_questions);
printf("\tTotal answers: %d\n", st_total);
printf("\tRight answers: %d\n", st_right);
printf("\tWrong answers: %d\n", st_wrong);
if (st_total != 0)printf("\tAccuracy: %d%c\n", st_right * 100 / st_total, '%');
}
void stats_local() // displays local statistics
{
printf("\n\t***Round statistics***\n");
printf("\tRound result: ");
if (playing) printf("Victory\n"); else printf("Defeat\n");
printf("\tTotal questions: %d\n", q);
printf("\tTotal answers: %d\n", sl_total);
printf("\tRight answers: %d\n", sl_right);
printf("\tWrong answers: %d\n", sl_wrong);
printf("\tAccuracy: %d%c\n", sl_right * 100 / sl_total, '%');
}
void test(char type) // test\game function.
{
sl_right = 0; sl_wrong = 0; sl_total = 0;
playing = true;
for (q = 1; q <= quests && playing; q++)
{
tries = miss;
solved = false;
//Generate math problem
if (type == '+')
{
one = random(add_min, add_max);
two = random(add_min, add_max);
sol = one + two;
printf("\n%d. %d + %d = ?\n", q, one, two);
}
if (type == '-')
{
sol = random(sub_min, sub_max);
two = random(sub_min, sub_max);
one = sol + two;
printf("\n%d. %d - %d = ?\n", q, one, two);
}
if (type == '/')
{
sol = random(div_min, div_max);
two = random(div_min, div_max);
one = sol * two;
printf("\n%d. %d / %d = ?\n", q, one, two);
}
if (type == '*')
{
one = random(mul_min, mul_max);
two = random(mul_min, mul_max);
sol = one * two;
printf("\n\n%d. %d * %d = ?\n", q, one, two);
}
//Answering loop
while (tries > 0 && !solved)
{
//Get the answer from user
printf(" Your answer > "); scanf("%d", &i);
sl_total++;
if (i == sol) // right answer
{
sl_right++;
comment('!');
solved = true;
}
else // wrong answer
{
sl_wrong++;
tries--;
if (tries > 0)
{
// Provide different comments, depending on how near the answer was.
if (i < sol + 4 && i > sol) comment('-');
else if (i > sol - 4 && i < sol) comment('+');
else if (i < sol + 16 && i > sol) comment('<');
else if (i > sol - 16 && i < sol) comment('>');
else comment('~');
printf("\nYou have %d tries remaining!\n", tries);
}
else // no tries\lives remaining - game over
{
solved = true;
playing = false;
comment('@');
printf("\nThe correct answer was... %d!", sol);
}
}
}
}
q--; // decrease q by one to hold actual number of passed questions
st_questions += q;
st_total += sl_total;
st_right += sl_right;
st_wrong += sl_wrong;
if (playing) comment('v');
stats_local();
}
int main()
{
srand(time(NULL)); // set random seed from current time
//Get users name:
printf("What is your name?\n > "); gets(uname);
while (1) // main loop:
{
printf("\n\n");
printf("Welcome, %s, to %s!\n", uname, title);
printf("Please choose one of the following options: \n");
printf(" [1] Addition\n");
printf(" [2] Subtraction\n");
printf(" [3] Division\n");
printf(" [4] Multiplication\n");
printf(" [5] Statistics\n");
printf(" [0] Exit\n");
//Get input:
printf(" > "); scanf("%d", &key);
//Cases:
if (key == 1) test('+');
if (key == 2) test('-');
if (key == 3) test('/');
if (key == 4) test('*');
if (key == 5) stats();
if (key == 0) return 1;
}
return 0;
}
|