Here's a hint:
Code:
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_TESTS 10
bool read(const char* format, void* address) {
char buffer[1024];
fgets(buffer, sizeof buffer, stdin);
return sscanf(buffer, format, address) == 1;
}
struct struc {
int a;
int b;
int c;
int add;
int grade;
};
int sj(int n)
{
int t;
t = rand() % n;
return t;
}
void ctm_i(struct struc* t)
{
t->a = sj(101);
t->c = sj(4);
if (t->c == 1) {
t->b = sj(101 - (t->a));
t->add = (t->a) + (t->b);
}
else {
t->b = sj((t->a) + 1);
t->add = (t->a) - (t->b);
}
t->grade = 10;
}
void tcm_i(struct struc* t, int n)
{
int ad;
printf(
" ***********************************************************************"
"*********\n");
printf(
" ......................................................................."
".........\n");
printf(" Question %d\n\n", n + 1);
printf(" You have 3 attempts for this question\n\n");
if (t->c == 1)
printf(" %d+%d= ", t->a, t->b);
else
printf(" %d-%d= ", t->a, t->b);
read(" %d", &ad);
if (ad == t->add)
{
t->grade = 10;
printf("\n Very good, you earned 10 marks\n\n");
}
else {
printf("\n Sorry, you have 2 attempts remaining\n\n");
read(" %d", &ad);
if (ad == t->add)
{
t->grade = 7;
printf("\n Good, you earned 7 marks\n\n");
}
else {
printf("\n Sorry, you have 1 attempt remaining\n\n");
read(" %d", &ad);
if (ad == t->add)
{
t->grade = 5;
printf("\n Not bad, you earned 5 marks\n\n");
}
else {
t->grade = 0;
printf("\n Failure, 0 mark\n\n");
printf("\n The correct answer is %d\n\n", t->add);
}
}
}
printf(
" ......................................................................."
".........\n");
printf(
" ***********************************************************************"
"*********\n");
}
int main()
{
int rounds = 0;
int highest = 0;
int lowest = INT_MAX;
int i, j, g = 0;
char x;
struct struc test[MAX_TESTS];
srand((unsigned)time(NULL));
printf(
" ***********************************************************************"
"************\n");
printf(
" ......................................................................."
"............\n");
printf(
" ****************************Welcome!****************************\n\n");
printf(" ...........This program is for students Grades 1-2............\n");
printf("\n Description:\n");
printf(
"(1)Computer randomly selects 10 questions, each question is worth 10 "
"points, the end of the test shows the student score;\n");
printf(
"(2)Only addition and subtraction within 100 is allowed. The sum or "
"difference of two numbers do not exceed the range of 0-100, negative "
"numbers are not included;\n");
printf("(3)There are 3 attempts for each question.;\n");
printf(
"(4)For each question, 10 points will be awarded for the first "
"successful attempt, 7 points for the second attempt, and 5 points for "
"the third attempt;\n");
printf(
"(5)For total scores above "
"output“EXCELLENT”,80-90“GOOD”,70-80“AVERAGE”,60-"
"70“PASS”,60“POOR“。\n");
printf(
" ......................................................................."
".........\n");
printf(
" ***********************************************************************"
"*********\n");
for (i = 0; i < MAX_TESTS; i++)
{
ctm_i(&test[i]);
for (j = 0; j < i; j++)
if (test[i].a == test[j].a && test[i].b == test[j].b &&
test[i].c == test[j].c)
ctm_i(&test[i]);
}
printf(" Are you ready? Please click on any key to continue: ");
getchar();
for (;;) {
rounds++;
for (i = 1; i <= 5; i++) {
printf(
" *******************************************************************"
"**"
"***********\n");
printf(
" ..................................................................."
".."
"...........\n");
}
for (i = 0; i < MAX_TESTS; i++) tcm_i(&test[i], i);
printf(" End");
for (i = 0; i < MAX_TESTS; i++) g = g + test[i].grade;
if (g > 90)
printf(" Excellent! You are a genius!\n\n");
else if (g > 80)
printf(" Not bad, keep it up!\n\n");
else if (g > 70)
printf(" Try harder next time!\n\n");
else
printf(" TRY AGAIN\n\n");
bool done = false;
bool unsure = true;
while (unsure) {
unsure = false;
puts("(S)how results");
puts("(P)lay another round");
puts("(Q)uit");
char choice;
read("%c", &choice);
if (choice == 'Q')
done = true;
else if (choice == 'S') {
if (rounds == 1)
printf("Final score: %d/100\n", g);
else {
puts("Whoops! Looks like highest/lowest have not been adjusted!");
printf("Highest score: %d/100\n", highest);
printf("Lowest score: %d/100\n", lowest);
}
getchar();
} else if (choice == 'P') {
// Do nothing?
} else {
puts("Invalid input!");
unsure = true;
}
}
if (done) break;
}
}