Sei sulla pagina 1di 8

#include <stdio.

h>
#include <time.h>
#include <stdlib.h>
#define SIZE 10

//time function
void time (int timer) {

time_t startTime = time(NULL);

time_t timerDuration = timer; // input any second timer


time_t endTime = startTime + timerDuration;
while(1)
{
time_t currTime = time(NULL);
time_t timeLeft = endTime - currTime;

if(timeLeft < 0)
{
// timer has finished
break;
}
}
}

//Operator character function


char get_operator(int operator) {
if (operator == 0) { return '+'; }
if (operator == 1) { return '-'; }
if (operator == 2) { return '*'; }
return '/'; }

//Operation function
double do_operation(int operator1, int value1, int value2) {
if (operator1 == '+') { return value1 + value2; }
if (operator1 == '-') { return value1 - value2; }
if (operator1 == '*') { return value1 * value2; }
return value1 / value2;
}

//show result function


void stats() // displays global statistics {
printf ( "\t***Game statistics***");
printf ("\tTotal questions: %d", int questions);
printf ("\tTotal answers:%d", int totalansw);
printf("\tRight answers:%d" int correct);
printf("\tWrong answers:%d ", int wrong);
printf("\Time Out:%d ", int time);
}

// test game function

void test(int choice)


{
int correct= 0; int wrong= 0; int total = 0; int input=0
playing = true;
for (int q = 1; q <=SIZE; q++)
{
int tries = miss;
int solved = false;

//Generate math problem


if (choice == 1) {
{ srand(time(0)); //seed random number generator
int value1 = rand() % 10 + 1; // random number between 1 and 10
int value2 = rand() % 10 + 1;
char operator1 = get_operator(rand() % 4);
double answer = do_operation(operator1, value1, value2);

Printf (“%d %c %d”, value1, operator1, value2);

Value1++;
Value2++;
Operator++;

}
timer=140;
time (int timer)
}

if (choice == 2){
{ srand(time(0)); //seed random number generator
int value1 = rand() % 14 + 1; // random number between 1 and 14
int value2 = rand() % 14 + 1;
char operator1 = get_operator(rand() % 4);
double answer = do_operation(operator1, value1, value2);

Printf (“%d %c %d”, value1, operator1, value2);

Value1++;
Value2++;
Operator++;

}
timer=130;
time (int timer)
}

if (choice == 3) {
{ srand(time(0)); //seed random number generator
int value1 = rand() % 18 + 1; // random number between 1 and 18
int value2 = rand() % 18 + 1;
char operator1 = get_operator(rand() % 4);
double answer = do_operation(operator1, value1, value2);

Printf (“%d %c %d”, value1, operator1, value2);

Value1++;
Value2++;
Operator++;

}
timer=120;
time (int timer)
}

if (choice == 4) {
{ srand(time(0)); //seed random number generator
int value1 = rand() % 22 + 1; // random number between 1 and 18
int value2 = rand() % 22 + 1;
char operator1 = get_operator(rand() % 4);
double answer = do_operation(operator1, value1, value2);

Printf (“%d %c %d”, value1, operator1, value2);

Value1++;
Value2++;
Operator++;
}
timer=110;
time (int timer)
}

if (choice == 5) {
{ srand(time(0)); //seed random number generator
int value1 = rand() % 26 + 1; // random number between 1 and 18
int value2 = rand() % 26 + 1;
char operator1 = get_operator(rand() % 4);
double answer = do_operation(operator1, value1, value2);

Printf (“%d %c %d”, value1, operator1, value2);

Value1++;
Value2++;
Operator++;

}
timer=100;
time (int timer)
}

if (choice == 6) {
{ srand(time(0)); //seed random number generator
int value1 = rand() % 30 + 1; // random number between 1 and 18
int value2 = rand() % 30 + 1;
char operator1 = get_operator(rand() % 4);
double answer = do_operation(operator1, value1, value2);

Printf (“%d %c %d”, value1, operator1, value2);}

Value1++;
Value2++;
Operator++;

}
timer=90;
time (int timer)
}
if (choice == 7) {
{ srand(time(0)); //seed random number generator
int value1 = rand() % 34 + 1; // random number between 1 and 18
int value2 = rand() % 34 + 1;
char operator1 = get_operator(rand() % 4);
double answer = do_operation(operator1, value1, value2);}

Printf (“%d %c %d”, value1, operator1, value2);

Value1++;
Value2++;
Operator++;

}
timer=80;
time (int timer)
}

if (choice == 8) {
{ srand(time(0)); //seed random number generator
int value1 = rand() % 38 + 1; // random number between 1 and 18
int value2 = rand() % 38 + 1;
char operator1 = get_operator(rand() % 4);
double answer = do_operation(operator1, value1, value2);

Printf (“%d %c %d”, value1, operator1, value2);

Value1++;
Value2++;
Operator++;

}
timer=70;
time (int timer)
}

if (choice == 9) {
{ srand(time(0)); //seed random number generator
int value1 = rand() % 42 + 1; // random number between 1 and 18
int value2 = rand() % 42 + 1;
char operator1 = get_operator(rand() % 4);
double answer = do_operation(operator1, value1, value2);
Printf (“%d %c %d”, value1, operator1, value2);

Value1++;
Value2++;
Operator++;

}
timer=60;
time (int timer)
}

if (choice == 10) {
{ srand(time(0)); //seed random number generator
int value1 = rand() % 46 + 1; // random number between 1 and 18
int value2 = rand() % 46 + 1;
char operator1 = get_operator(rand() % 4);
double answer = do_operation(operator1, value1, value2);

Printf (“%d %c %d”, value1, operator1, value2);

Value1++;
Value2++;
Operator++;

}
timer=50;
time (int timer)
}

//Answering loop
while (tries > 0 && !solved)
{

//Get the answer from user


scanf_s( “%d”, &input);
total++

if (input == answer) // right answer


{
correct++;

solved = true;
}
else // wrong answer
{
wrong++;
tries--;
if (tries > 0)
{solved = true;
playing = false;
printf (“The correct answer was: %d”, answer);}
}
}
}
q--; // decrease q by one to hold actual number of passed questions

correct += correct;
wrong += wrong;
stats(); }

//index and main function


int main()
{
srand(time(NULL)); // set random seed from current time
//Get users name:
printf(“Arithmetic Operational game”);
while (1) // main loop:
{

printf ("[1] Stage 1”);


printf ("[2] Stage 2”);
printf ("[3] Stage 3”);
printf ("[4] Stage 4”);
printf ("[5] Stage 5”);
printf ("[6] Stage 6”);
printf ("[7] Stage 7”);
printf ("[8] Stage 8”);
printf ("[9] Stage 9”);
printf ("[10] Stage 10”);
printf ("[11] END”);

//Get input:
scanf (“%d”, int &choice);
if (choice ==11) {break;}
else if (choice>11) {printf (“Incorrect input. Choose one of the visible choices”}
else if (choice>=’A|| choice<=’Z’ ||choice>=’a’ ||choice ‘z’) (“Incorrect input. Choose
one of the visible choices”}
//Cases:
if (choice == 1) test(stage1);
if (choice == 2) test(stage2);
if (choice == 3) test(stage3);
if (choice == 4) test (stage4);
if (choice == 5) test (stage5);
if (choice == 6) test (stage6);
if (choice == 7) test(stage7);
if (choice == 8) test(stage8);
if (choice == 9) test(stage9);
if (choice == 10) test (stage10);

}
return 0;
}

Potrebbero piacerti anche