86 lines
2.3 KiB
C
86 lines
2.3 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
int main(void) {
|
|
int lives = 7;
|
|
int questions = 20;
|
|
int number_range = 10;
|
|
int operator_range = 10;
|
|
int num_right = 0;
|
|
int counter = 1;
|
|
char operators[] = {'+', '-', '*', '/', '%', '=', '<', '>', '<=', '>=', '!='};
|
|
char question_visual[questions];
|
|
char lives_visual[lives];
|
|
|
|
for (int i = 0; i < questions; i++) {
|
|
question_visual[i] = '-';
|
|
}
|
|
|
|
for (int i = 0; i < lives; i++) {
|
|
lives_visual[i] = '+';
|
|
}
|
|
|
|
while (num_right <=20) {
|
|
int num1 = rand() % number_range;
|
|
int num2 = rand() % number_range;
|
|
int operator = rand() % operator_range;
|
|
int answer;
|
|
int correct_answer;
|
|
switch (operator) {
|
|
case 0:
|
|
correct_answer = num1 + num2;
|
|
break;
|
|
case 1:
|
|
correct_answer = num1 - num2;
|
|
break;
|
|
case 2:
|
|
correct_answer = num1 * num2;
|
|
break;
|
|
case 3:
|
|
correct_answer = num1 / num2;
|
|
break;
|
|
case 4:
|
|
correct_answer = num1 % num2;
|
|
break;
|
|
case 5:
|
|
correct_answer = num1 == num2;
|
|
break;
|
|
case 6:
|
|
correct_answer = num1 < num2;
|
|
break;
|
|
case 7:
|
|
correct_answer = num1 > num2;
|
|
break;
|
|
case 8:
|
|
correct_answer = num1 <= num2;
|
|
break;
|
|
case 9:
|
|
correct_answer = num1 >= num2;
|
|
break;
|
|
case 10:
|
|
correct_answer = num1 != num2;
|
|
break;
|
|
}
|
|
printf("You have %d lives left\n", lives);
|
|
printf("Question %d: What is %d %c %d?\n", counter ,num1, operators[operator], num2);
|
|
scanf("%d", &answer);
|
|
if (answer == correct_answer) {
|
|
printf("Correct!\n");
|
|
num_right++;
|
|
question_visual[num_right] = '+';
|
|
} else {
|
|
printf("Incorrect!\nAnswer: %d", correct_answer);
|
|
lives--;
|
|
lives_visual[lives] = ' ';
|
|
}
|
|
if (lives == 0) {
|
|
printf("Game Over!\n");
|
|
break;
|
|
}
|
|
counter++;
|
|
}
|
|
|
|
printf("Questions: %s\nLives: %s\n", question_visual, lives_visual);
|
|
|
|
}
|