Created
December 27, 2016 15:04
-
-
Save igaryhe/5719617ce1885cbfb17e6a36c3995390 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <ctype.h> | |
#include <time.h> | |
#include <string.h> | |
#include <math.h> | |
typedef struct { | |
char id[7]; | |
int score; | |
int time; | |
} Entry; | |
typedef struct { | |
int first; | |
char oper; | |
int second; | |
int result; | |
} Equation; | |
// this function is used to judge if the ID is legal | |
// this function accept a char array parameter which store the inputed ID | |
// return 1 if the form is legal, otherwise return 0 | |
int validation(char id[]) { | |
int flag = 1; | |
if (!isalpha(id[0]) || !isalpha(id[1])) flag = 0; | |
for (int i = 2; i != 6; i++) | |
if (!isdigit(id[i])) flag = 0; | |
if (id[6] != '\0') flag = 0; | |
return flag; | |
} | |
// randomly generate 10 equations to an Equation array | |
// the generating process contain the following steps: | |
// 1. randomly generate a number from 0 to 3, which matches to one of the operators | |
// 2. if the operator is +/*, generate the augend/multiplier and the addend/multiplicand | |
// if the operator is -//,generate the result and the subtrahend/divisor | |
// 3. calculate the result if the operator is +/* | |
// calculate the minuend/dividend if the operator is-// | |
// 4. if any part of the equation is larger that 99, randomly generate the numbers again | |
void generate(Equation e[]) { | |
srand((unsigned)time(NULL)); | |
for (int i = 0; i != 10; i++) { | |
int seed = rand() % 4; | |
switch (seed) { | |
case 0: { | |
e[i].oper = '+'; | |
e[i].first = rand() % 100; | |
e[i].second = rand() % (100 - e[i].first); | |
e[i].result = e[i].first + e[i].second; | |
break; | |
} | |
case 1: { | |
e[i].oper = '-'; | |
e[i].result = rand() % 100; | |
e[i].second = rand() % (100 - e[i].result); | |
e[i].first = e[i].result + e[i].second; | |
break; | |
} | |
case 2: { | |
e[i].oper = '*'; | |
e[i].first = rand() % 100; | |
if (e[i].first == 0) e[i].second = rand() % 100; | |
else e[i].second = rand() % (99 / e[i].first + 1); | |
e[i].result = e[i].first * e[i].second; | |
break; | |
} | |
case 3: { | |
e[i].oper = '/'; | |
e[i].result = rand() % 100; | |
if (e[i].result == 0) e[i].second = rand() % 99 + 1; | |
else e[i].second = rand() % (99 / e[i].result) + 1; | |
e[i].first = e[i].result * e[i].second; | |
break; | |
} | |
} | |
} | |
} | |
// this function is used to run the main test part | |
// the function accept an user Entry as a parameter | |
// the function operate according to the following sequence: | |
void exam(Entry *user) { | |
user->score = 0; | |
time_t begin, end; | |
Equation e[10]; | |
int input[10]; | |
generate(e); | |
begin = time(NULL); | |
for (int i = 0; i != 10; i++) { | |
printf("%d %c %d = ", e[i].first, e[i].oper, e[i].second); | |
scanf("%d", &input[i]); | |
if (input[i] == e[i].result) user->score += 10; | |
} | |
end = time(NULL); | |
user->time = end - begin; | |
printf("Prob.\t| Correct Answ.\t| Ur Answ\n"); | |
for (int j = 0; j != 10; j++) | |
printf("%d %c %d\t| %d\t\t| %d\n", e[j].first, e[j].oper, e[j].second, e[j].result, input[j]); | |
printf("Your score is: %d\n", user->score); | |
} | |
// this function is used to check the history score of current user | |
// this function accept two parameters | |
// the first is a file pointer, another one is a char array which contain the current user ID | |
// the file that the function reads need to be exist and in correct form | |
void check(FILE *c, char id[]) { | |
c = fopen("record.txt", "r"); | |
Entry read; | |
while(fscanf(c, "%s%d%d", read.id, &read.score, &read.time) != EOF) { | |
if (strcmp(read.id, id) == 0) | |
printf("%s\t%d\t%d seconds\n", read.id, read.score, read.time); | |
} | |
fclose(c); | |
} | |
int main() { | |
// declare two file pointer | |
// f for file output, and c for file input | |
FILE *f, *c; | |
Entry user; | |
do { | |
printf("Please input your four digit ID no:"); | |
scanf("%s", user.id); | |
if (validation(user.id) == 0) printf("Invalid ID\n"); | |
} while(validation(user.id) == 0); | |
char choice; | |
do { | |
// TODO | |
/* if 'enter' is not declared, the enter that user inputed will be | |
filled in 'choice', which means that the loop need to be ran for | |
another time before it can accept user input.*/ | |
char enter; | |
scanf("%c", &enter); | |
printf("Enter the number of choice to continue:\n"); | |
printf("1. Start a test\n"); | |
printf("2. Check scores\n"); | |
printf("3. Exit\n"); | |
scanf("%c", &choice); | |
switch (choice) { | |
case '1': { | |
f = fopen("record.txt", "a+"); | |
exam(&user); | |
fprintf(f, "%s\t", user.id); | |
fprintf(f, "%d\t", user.score); | |
fprintf(f, "%d\n", user.time); | |
fclose(f); | |
break; | |
} | |
case '2': { | |
check(c, user.id); | |
break; | |
} | |
case '3': { | |
break; | |
} | |
default: { | |
printf("Invalid input\n"); | |
break; | |
} | |
} | |
} while (choice != '3'); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment