Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<time.h>
- #include<stdlib.h>
- #include<conio.h>
- #include<ctype.h>
- #include<stdio.h>
- #include<Windows.h>
- /*Your program will allow your brother to play a games where he has to answer series of mathematical questions.
- The program will have 3 rounds based on the following level of difficulty with 10 questions per round :
- a.Easy – simple mathematical expressions with only one operator and uses only subtraction and addition(e.g. 4 + 5)
- b.Average – simple mathematical expressions with only one operator and uses only multiplication and division(e.g 5 * 2)
- c.Difficult – mathematical expression that uses 2 operator and can be combination of any of the 4 operators(e.g 4 * 5 + 2)
- The program should generate 2 random integers between 1 and 10 and randomly picks an operator.
- Then it prompts the user for an answer.After the user inputs the answer, your program should tell him whether the answer is too high or too low.
- Your program should keep track of how much time he takes for these problems.
- In addition, 5 penalty seconds should be added for each incorrect response.
- The total time he takes(taking into account the penalty seconds) will be used to determine his score for each question.
- The score your brother earns is equal to the total amount of time(in seconds) it took him to finish the problems(including penalty seconds) divided by the number of problems.
- The score in seconds then must be converted to an integer number of points in between 0 and 10. In particular, the conversion works as shown in the formula below :
- time 1 = score 10. for every + 1 seconds -1 score, if incorrect answer+5 sec. eg. 2 sec 8 score, 9 seconds 1 score. Anything over 10 seconds is automatically 0.
- */
- void gui(int input)
- {
- if (input)
- {
- printf("\n\t\t _ _ _\n");
- printf("\t\t%c %c %c ___ _ _ %c %c\n", 179, 179, 179, 179, 179);
- printf("\t\t\\ /<_> %c%c %c %c%c_/\n", 179, 179, 179, 179, 179);
- printf("\t\t %c_%c <___%c`_. %c<_>\n", 179, 179, 179, 179);
- printf("\t\t <___' \n");
- printf("\t _ _ _ _ _ ___ _ \n");
- printf("\t%c %c %c %c ___ %c %c%c %c %c . \\ ___ ._ _ ___ %c %c\n", 179, 179, 179, 179, 179, 179, 179, 179, 179);
- printf("\t%c %c %c %c/ ._>%c %c%c %c %c %c %c/ . \\%c ' %c/ ._>%c_/\n", 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179);
- printf("\t%c__/_/ \\___.%c_%c%c_%c %c___/\\___/%c_%c_%c\\___.<_>\n", 179, 179, 179, 179, 179, 179, 179, 179, 179);
- }
- else
- {
- printf("\n\n\t___ ___ _ _ _ _ _ \n");
- printf("\t%c \\/ %c %c %c %c %c %c %c %c (_) \n", 179, 179, 179, 179, 179, 179, 179, 179, 179);
- printf("\t%c . . %c __ _%c %c_%c %c__ %c %c %c %c_ ____\n", 179, 179, 179, 179, 179, 179, 179, 179, 179, 179);
- printf("\t%c %c\\/%c %c/ _` %c __%c '_ \\%c %c/\\%c %c %c_ /\n",179,179,179,179,179,179,179,179,179,179,179);
- printf("\t%c %c %c %c (_%c %c %c_%c %c %c \\ /\\ / %c/ / \n", 179, 179, 179, 179, 179, 179, 179, 179, 179, 179, 179);
- printf("\t\\_%c %c_/\\__,_%c\\__%c_%c %c_%c\\/ \\/%c_/___%c\n", 179, 179, 179, 179, 179, 179, 179, 179, 179);
- }
- }
- void main()
- {
- int rounds, score=0, operand, operand2;
- int output; //controls output of gui lines in "for"
- float firstNum, secondNum, thirdNum,answer,userAnswer, timer;
- //Variables for roundup operation
- int whole,decimal;
- int debug = 3;
- /*
- Level skips will display prompts but bypass the levels.
- Any value of debug will display debug information for the decimal roundup code segment
- 1=Skips level 1
- 2=Skips level 1,2
- 3=All levels skipped
- */
- srand(time(NULL));
- gui(0);
- printf("\n\n\tThere are three rounds with 10 problems each.\n\n\tYou have 10 seconds to answer each correctly.");
- printf("\n\n\t"); for (output = 48; output; output--) printf("%c", 220);
- printf("\n\n\tFirst Round:\tAddition/Subtraction");
- printf("\n\n\t"); for (output = 48; output; output--) printf("%c", 223);
- printf("\n\n\t\tPress any key to begin.");
- _getch();
- printf("\n\n\tGet ready! In...3");
- Sleep(1000);
- printf("...2");
- Sleep(1000);
- printf("...Lets go!");
- Sleep(1000);
- //easy mode
- for (rounds = 10; rounds; rounds--)
- {
- if (debug > 1) break; //debug skip
- system("cls");
- gui(0);
- if (rounds>1) printf("\n\n\t\tEquation %d", 10-rounds + 1);
- else printf("\n\n\t\tFinal Equation");
- printf("\n\n\t\tCurrent Score : %d\n", score);
- firstNum = rand() % 10 + 1;
- secondNum = rand() % 10 + 1;
- operand = rand() % 2;
- timer = clock(); //timer begins
- if (operand)
- {
- answer = firstNum + secondNum;
- printf("\n\n\tThe sum of %2.0f + %2.0f is: ", firstNum,secondNum);
- }//True(1)=Addition
- else
- {
- answer = firstNum - secondNum;
- printf("\n\n\tThe difference of %2.0f - %2.0f is: ", firstNum, secondNum);
- }//subtraction
- fflush(stdin);
- scanf_s("%f", &userAnswer);
- timer = float((clock()-timer)/1000);
- if (answer == userAnswer)
- printf("\n\tYour answer is correct!\n\t");
- else if (answer > userAnswer)
- {
- printf("\n\tYour answer is too low! \n\tCorrect Answer: %2.0f\t",answer);
- timer += 5;
- }
- else
- {
- printf("\n\tYour answer is too high! \n\tCorrect Answer: %2.0f\t",answer);
- timer += 5;
- }
- /*--------------SCORE EVALUATION--------------*/
- if (timer < 1) { score += 10; printf("Score +10");}
- else if (timer < 2) { score += 9; printf("Score +9");}
- else if (timer < 3) { score += 8; printf("Score +8");}
- else if (timer < 4) { score += 7; printf("Score +7");}
- else if (timer < 5) { score += 6; printf("Score +6");}
- else if (timer < 6) { score += 5; printf("Score +5");}
- else if (timer < 7) { score += 4; printf("Score +4");}
- else if (timer < 8) { score += 3; printf("Score +3");}
- else if (timer < 9) { score += 2; printf("Score +2");}
- else if (timer < 10){ score += 1; printf("Score +1");}
- else { score += 0; printf("Score +0");}
- /*--------------SCORE EVALUATION--------------*/
- if (rounds > 1)
- {
- printf("\n\n\tNext round in 3");
- Sleep(1000);
- printf("...2");
- Sleep(1000);
- printf("...1");
- Sleep(1000);
- }
- }//LoopforEasyMode
- //between rounds
- printf("\n\n\tWell done! Next round is coming up");
- printf(". ");
- Sleep(1000);
- printf(". ");
- Sleep(1000);
- printf(". ");
- system("cls");
- gui(0);
- printf("\n\n\t"); for (output = 48; output; output--) printf("%c", 220);
- printf("\n\n\tSecond Round:\tDivision/Multiplication");
- printf("\n\n\tFor division, round up answers 2 decimal places.\n\t\t eg. 5.125 > 5.13");
- printf("\n\n\t"); for (output = 48; output; output--) printf("%c", 223);
- printf("\n\n\t\tPress any key to begin.");
- _getch();
- printf("\n\n\tGet ready! In...3");
- Sleep(1000);
- printf("...2");
- Sleep(1000);
- printf("...Lets go!");
- Sleep(1000);
- //average mode
- for (rounds = 10; rounds; rounds--)
- {
- if (debug > 1) break; //debug skip
- system("cls");
- gui(0);
- if (rounds>1) printf("\n\n\t\tEquation %d", 10 - rounds + 1);
- else printf("\n\n\t\tFinal Equation");
- printf("\n\n\t\tCurrent Score : %d\n", score);
- firstNum = rand() % 10 + 1;
- secondNum = rand() % 10 + 1;
- operand = rand() % 2;
- timer = clock(); //timer begins
- if (operand)
- {
- answer = firstNum * secondNum;
- printf("\n\n\tThe product of %2.0f * %2.0f is: ", firstNum, secondNum);
- }//True(1)=Multiplication
- else
- {
- answer = firstNum / secondNum;
- /*--------------ROUNDUP DECIMAL--------------*/
- whole = answer; //convert to whole answer
- decimal = (float(answer) - whole) * 1000; //remove whole answer and turn decimal to whole answer
- if ((decimal % 10)<5) decimal -= decimal % 10; //rounddown
- else decimal = decimal - decimal % 10 + 10; //roundup
- answer = whole + float(decimal) / 1000; //reassign
- if (debug) printf("%f answer, %d decimal, whole %d", answer, decimal, whole); /*DEBUG FUNCTION*/
- /*--------------ROUNDUP DECIMAL--------------*/
- printf("\n\n\tThe quotient of %2.0f / %2.0f is: ", firstNum, secondNum);
- }//Division
- fflush(stdin);
- scanf_s("%f", &userAnswer);
- timer = float((clock() - timer) / 1000);
- if (answer == userAnswer)
- printf("\n\tYour answer is correct!\n\t");
- else if (answer > userAnswer)
- {
- printf("\n\tYour answer is too low! \n\tCorrect Answer: %2.2f\t",answer);
- timer += 5;
- }
- else
- {
- printf("\n\tYour answer is too high! \n\tCorrect Answer: %2.2f\t",answer);
- timer += 5;
- }
- /*--------------SCORE EVALUATION--------------*/
- if (timer < 1) { score += 10; printf("Score +10");}
- else if (timer < 2) { score += 9; printf("Score +9");}
- else if (timer < 3) { score += 8; printf("Score +8");}
- else if (timer < 4) { score += 7; printf("Score +7");}
- else if (timer < 5) { score += 6; printf("Score +6");}
- else if (timer < 6) { score += 5; printf("Score +5");}
- else if (timer < 7) { score += 4; printf("Score +4");}
- else if (timer < 8) { score += 3; printf("Score +3");}
- else if (timer < 9) { score += 2; printf("Score +2");}
- else if (timer < 10){ score += 1; printf("Score +1");}
- else { score += 0; printf("Score +0");}
- /*--------------SCORE EVALUATION--------------*/
- if (rounds > 1)
- {
- printf("\n\n\tNext round in 3");
- Sleep(1000);
- printf("...2");
- Sleep(1000);
- printf("...1");
- Sleep(1000);
- }
- }//Average Mode
- printf("\n\n\tWell done! Next round is coming up");
- printf(". ");
- Sleep(1000);
- printf(". ");
- Sleep(1000);
- printf(". ");
- system("cls");
- gui(0);
- printf("\n\n\t"); for (output = 48; output; output--) printf("%c", 220);
- printf("\n\n\tFinal Round:\tAll Operators, 3 numbers");
- printf("\n\n\t"); for (output = 48; output; output--) printf("%c", 223);
- printf("\n\n\t\tPress any key to begin.");
- _getch();
- printf("\n\n\tGet ready! In...3");
- Sleep(1000);
- printf("...2");
- Sleep(1000);
- printf("...Lets go!");
- Sleep(1000);
- //Difficult
- for (rounds = 10; rounds; rounds--)
- {
- if (debug > 2) break; //debug skip
- system("cls");
- gui(0);
- if (rounds>1) printf("\n\n\t\tEquation %d", 10 - rounds + 1);
- else printf("\n\n\t\tFinal Equation");
- printf("\n\n\t\tCurrent Score : %d\n", score);
- firstNum = rand() % 10 + 1;
- secondNum = rand() % 10 + 1;
- thirdNum = rand() % 10 + 1;
- operand = rand() % 4;
- operand2 = rand() % 4;
- timer = clock(); //timer begins
- /*------------------------------*/
- // 0 - MULTIPLY
- // 1 - DIVIDE
- // 2 - ADD
- // 3 - SUBRTRACT
- if (operand == 0 && operand2 == 0)
- {
- answer = firstNum * secondNum * thirdNum;
- printf("\n\n\tThe value of %.2f * %.2f * %.2f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 0 && operand2 == 1)
- {
- answer = firstNum * secondNum / thirdNum;
- /*--------------ROUNDUP DECIMAL--------------*/
- whole = answer; //convert to whole answer
- decimal = (float(answer) - whole) * 1000; //remove whole answer and turn decimal to whole answer
- if ((decimal % 10)<5) decimal -= decimal % 10; //rounddown
- else decimal = decimal - decimal % 10 + 10; //roundup
- answer = whole + float(decimal) / 1000; //reassign
- if (debug) printf("%f answer, %d decimal, whole %d", answer, decimal, whole); /*DEBUG FUNCTION*/
- /*--------------ROUNDUP DECIMAL--------------*/
- printf("\n\n\tThe value of %.0f * %.0f / %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 0 && operand2 == 2)
- {
- answer = firstNum * secondNum + thirdNum;
- printf("\n\n\tThe value of %.0f * %.0f + %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 0 && operand2 == 3)
- {
- answer = firstNum * secondNum - thirdNum;
- printf("\n\n\tThe value of %.0f * %.0f - %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 1 && operand2 == 0)
- {
- answer = firstNum / secondNum * thirdNum;
- /*--------------ROUNDUP DECIMAL--------------*/
- whole = answer; //convert to whole answer
- decimal = (float(answer) - whole) * 1000; //remove whole answer and turn decimal to whole answer
- if ((decimal % 10)<5) decimal -= decimal % 10; //rounddown
- else decimal = decimal - decimal % 10 + 10; //roundup
- answer = whole + float(decimal) / 1000; //reassign
- if (debug) printf("%f answer, %d decimal, whole %d", answer, decimal, whole); /*DEBUG FUNCTION*/
- /*--------------ROUNDUP DECIMAL--------------*/
- printf("\n\n\tThe value of %.0f / %.0f * %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 1 && operand2 == 1)
- {
- answer = firstNum / secondNum / thirdNum;
- /*--------------ROUNDUP DECIMAL--------------*/
- whole = answer; //convert to whole answer
- decimal = (float(answer) - whole) * 1000; //remove whole answer and turn decimal to whole answer
- if ((decimal % 10)<5) decimal -= decimal % 10; //rounddown
- else decimal = decimal - decimal % 10 + 10; //roundup
- answer = whole + float(decimal) / 1000; //reassign
- if (debug) printf("%f answer, %d decimal, whole %d", answer, decimal, whole); /*DEBUG FUNCTION*/
- /*--------------ROUNDUP DECIMAL--------------*/
- printf("\n\n\tThe value of %.0f / %.0f / %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 1 && operand2 == 2)
- {
- answer = firstNum / secondNum + thirdNum;
- /*--------------ROUNDUP DECIMAL--------------*/
- whole = answer; //convert to whole answer
- decimal = (float(answer) - whole) * 1000; //remove whole answer and turn decimal to whole answer
- if ((decimal % 10)<5) decimal -= decimal % 10; //rounddown
- else decimal = decimal - decimal % 10 + 10; //roundup
- answer = whole + float(decimal) / 1000; //reassign
- if (debug) printf("%f answer, %d decimal, whole %d", answer, decimal, whole); /*DEBUG FUNCTION*/
- /*--------------ROUNDUP DECIMAL--------------*/
- printf("\n\n\tThe value of %.0f / %.0f + %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 1 && operand2 == 3)
- {
- answer = firstNum / secondNum - thirdNum;
- /*--------------ROUNDUP DECIMAL--------------*/
- whole = answer; //convert to whole answer
- decimal = (float(answer) - whole) * 1000; //remove whole answer and turn decimal to whole answer
- if ((decimal % 10)<5) decimal -= decimal % 10; //rounddown
- else decimal = decimal - decimal % 10 + 10; //roundup
- answer = whole + float(decimal) / 1000; //reassign
- if (debug) printf("%f answer, %d decimal, whole %d", answer, decimal, whole); /*DEBUG FUNCTION*/
- /*--------------ROUNDUP DECIMAL--------------*/
- printf("\n\n\tThe value of %.0f / %.0f - %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 2 && operand2 == 0)
- {
- answer = firstNum + secondNum * thirdNum;
- printf("\n\n\tThe value of %.0f + %.0f * %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 2 && operand2 == 1)
- {
- answer = firstNum + secondNum / thirdNum;
- /*--------------ROUNDUP DECIMAL--------------*/
- whole = answer; //convert to whole answer
- decimal = (float(answer) - whole) * 1000; //remove whole answer and turn decimal to whole answer
- if ((decimal % 10)<5) decimal -= decimal % 10; //rounddown
- else decimal = decimal - decimal % 10 + 10; //roundup
- answer = whole + float(decimal) / 1000; //reassign
- if (debug) printf("%f answer, %d decimal, whole %d", answer, decimal, whole); /*DEBUG FUNCTION*/
- /*--------------ROUNDUP DECIMAL--------------*/
- printf("\n\n\tThe value of %.0f + %.0f / %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 2 && operand2 == 2)
- {
- answer = firstNum + secondNum + thirdNum;
- printf("\n\n\tThe value of %.0f + %.0f + %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 2 && operand2 == 3)
- {
- answer = firstNum + secondNum - thirdNum;
- printf("\n\n\tThe value of %.0f + %.0f - %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 3 && operand2 == 0)
- {
- answer = firstNum - secondNum * thirdNum;
- printf("\n\n\tThe value of %.0f - %.0f * %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 3 && operand2 == 1)
- {
- answer = firstNum - secondNum / thirdNum;
- /*--------------ROUNDUP DECIMAL--------------*/
- whole = answer; //convert to whole answer
- decimal = (float(answer) - whole) * 1000; //remove whole answer and turn decimal to whole answer
- if ((decimal % 10)<5) decimal -= decimal % 10; //rounddown
- else decimal = decimal - decimal % 10 + 10; //roundup
- answer = whole + float(decimal) / 1000; //reassign
- if (debug) printf("%f answer, %d decimal, whole %d", answer, decimal, whole); /*DEBUG FUNCTION*/
- /*--------------ROUNDUP DECIMAL--------------*/
- printf("\n\n\tThe value of %.0f - %.0f / %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 3 && operand2 == 2)
- {
- answer = firstNum - secondNum + thirdNum;
- printf("\n\n\tThe value of %.0f - %.0f + %.0f is: ", firstNum, secondNum, thirdNum);
- }
- else if (operand == 3 && operand2 == 3) //I can put else implying (n1-n2-n3) operations.
- {
- answer = firstNum - secondNum - thirdNum;
- printf("\n\n\tThe value of %.0f - %.0f - %.0f is: ", firstNum, secondNum, thirdNum);
- }
- /*------------------------------*/
- fflush(stdin);
- scanf_s("%f", &userAnswer);
- timer = float((clock() - timer) / 1000);
- if (answer == userAnswer)
- printf("\n\tYour answer is correct!\n\t");
- else if (answer > userAnswer)
- {
- printf("\n\tYour answer is too low! \n\tCorrect Answer: %.2f\t", answer);
- timer += 5;
- }
- else
- {
- printf("\n\tYour answer is too high! \n\tCorrect Answer: %.2f\t", answer);
- timer += 5;
- }
- /*--------------SCORE EVALUATION--------------*/
- if (timer < 1) { score += 10; printf(" Score +10");}
- else if (timer < 2) { score += 9; printf(" Score +9");}
- else if (timer < 3) { score += 8; printf(" Score +8");}
- else if (timer < 4) { score += 7; printf(" Score +7");}
- else if (timer < 5) { score += 6; printf(" Score +6");}
- else if (timer < 6) { score += 5; printf(" Score +5");}
- else if (timer < 7) { score += 4; printf(" Score +4");}
- else if (timer < 8) { score += 3; printf(" Score +3");}
- else if (timer < 9) { score += 2; printf(" Score +2");}
- else if (timer < 10){ score += 1; printf(" Score +1");}
- else { score += 0; printf(" Score +0");}
- /*--------------SCORE EVALUATION--------------*/
- if (rounds > 1)
- {
- printf("\n\n\tNext round in 3");
- Sleep(1000);
- printf("...2");
- Sleep(1000);
- printf("...1");
- Sleep(1000);
- }
- }//Hard Mode
- Sleep(1000);
- system("cls");
- gui(1);
- printf("\n\n\tYou completed the game!");
- printf("\n\n\t"); for (output = 48; output; output--) printf("%c", 220);
- printf("\n\n\tYour final score is %d", score);
- printf("\n\n\t"); for (output = 48; output; output--) printf("%c", 223);
- printf("\n\n\tThanks for playing! Press any key to exit.");
- _getch();
- }//program ends
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement