Skip to content

Instantly share code, notes, and snippets.

@imkimchi
Created April 21, 2017 20:55
Show Gist options
  • Save imkimchi/cf15f09cd86082a0fbfde53175f5183e to your computer and use it in GitHub Desktop.
Save imkimchi/cf15f09cd86082a0fbfde53175f5183e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
void load(char* name, double* earnings, int* correct, int* incorrect);
void intro() {
printf("***********************\n");
printf("***********************\n");
printf("***********************\n");
printf("****** ******\n");
printf("******TheMathGame******\n");
printf("****** By Prof .******\n");
printf("****** Flores ******\n");
printf("***********************\n");
printf("***********************\n");
printf("***********************\n\n");
printf("y/Y to continue, any other char to exit \n");
}
void getName(char* name) {
printf("Enter your name and press <ENTER>");
scanf("%s%*c", name);
}
int savestats(char name[], double dearnings, int icorrect, int iincorrect)
{
FILE*fp = fopen("data.txt", "r");
FILE*op = fopen("temp.txt", "w");
if (fp != NULL)
{
char temp[256] = {};
int correct = 0;
int incorrect = 0;
double earnings = 0;
while (!feof(fp))
{
fscanf(fp, "%s %lf %d %d\n", temp, &earnings, &correct, &incorrect);
if (strcmp(temp, name) == 0)
continue;
fprintf(op, "%s %lf %d %d\n", temp, earnings, correct, incorrect);
}
fclose(fp);
remove("data.txt");
}
fprintf(op, "%s %lf %d %d\n", name, dearnings, icorrect, iincorrect);
fclose(op);
rename("temp.txt", "data.txt");
return 0;
}
void stats(char name[]) {
FILE* fp = NULL;
fp = fopen("data.txt", "r");
if (fp != NULL) {
char strTemp[255] = {};
int correct = 0;
int incorrect = 0;
double earnings = 0.0;
while (strcmp(name, strTemp) != 0)
{
fscanf(fp, "%s %lf %d %d", strTemp, &earnings, &correct, &incorrect);
}
printf("********************************\n");
printf("********************************\n");
printf("********************************\n");
printf("****** *****\n");
printf("******%s *****\n", name);
printf("******Total Earnings $%.2lf*****\n", earnings);
printf("******Total Correct %d *****\n", correct);
printf("******Total Correct %d *****\n", incorrect);
printf("*******************************\n");
}
fclose(fp);
}
int printProb(char probType, char name[]) {
int randNum1;
int randNum2;
int input=0;
int result = 0;
char type[20] = {};
char oper=0;
int temp;
srand(time(0));
randNum1 = rand() % 10 + 1;
randNum2 = rand() % 10 + 1;
if (probType == '1')
{
strcpy(type, "ADDITION");
oper = '+';
result = randNum1 + randNum2;
}
else if (probType == '2')
{
strcpy(type, "SUBTRACTION");
oper = '-';
result = randNum1 - randNum2;
if (result < 0)
{
temp = randNum1;
randNum1 = randNum2;
randNum2 = temp;
result = randNum1 = randNum2;
}
}
else if (probType == '3')
{
strcpy(type, "MULTIPLITION");
oper = '*';
result = randNum1 * randNum2;
}
else if (probType == '4')
{
strcpy(type, "DIVIDITION");
oper = '/';
result = randNum1 / randNum2;
}
printf("******%13s*****\n", type);
printf("************************\n");
printf("************************\n");
printf("***** %2d %c %2d = ? ******\n", randNum1, oper, randNum2);
printf("************************\n");
printf("************************\n");
scanf("%d%*c", &input);
if (input == result) {
return true;
}
else {
return false;
}
}
void right(double* earnings, int* correct) {
*earnings += 0.5;
*correct += 1;
printf("***********RIGHT!***********\n");
}
void wrong(double* earnings, int* incorrect) {
*earnings -= 0.3;
*incorrect += 1;
printf("***********WRONG!***********\n");
}
void problems(char name[], double* earnings, int* correct, int* incorrect) {
bool a = true;
while (a) {
printf("******CHOOSE A PROBLEM*****\n");
printf("***************************\n");
printf("***************************\n");
printf("****** ******\n");
printf("****** 1. ADD ******\n");
printf("****** 2. SUBTRACT ******\n");
printf("****** 3. MULTIPLY ******\n");
printf("****** 4. DIVIDE ******\n");
printf("****** 5. STATS ******\n");
printf("****** n/N to Quit ******\n");
printf("***************************\n");
printf("***************************\n");
char probType;
scanf("%c%*c", &probType);
if (probType >= '1' && probType <= '4')
{
if (printProb(probType, name) == true)
right(earnings, correct);
else
wrong(earnings, incorrect);
savestats(name, *earnings, *correct, *incorrect);
}
else if (probType == '5')
stats(name);
else if (probType == 'n' || probType == 'N')
a = false;
}
}
int main(void) {
char name[256] = {}; // 사람새끼가 이름이 256자씩되겠노?
int correct = 0;
int incorrect = 0;
double earnings = 0;
intro();
char answer;
scanf("%c%*c", &answer);
if (answer == 'Y' || answer == 'y') {
getName(name);
load(name, &earnings, &correct, &incorrect);
savestats(name, earnings, correct, incorrect);
problems(name, &earnings, &correct, &incorrect);
}
else {
return 0;
}
}
void load(char* name, double* earnings, int* correct, int* incorrect)
{
char temp[256] = {};
double tearning = 0;
int tcorrect = 0;
int tincorrect = 0;
FILE* fp = fopen("data.txt", "r");
if (fp == NULL)
return;
else
{
while (!feof(fp))
{
fscanf(fp, "%s %lf %d %d\n", temp, &tearning, &tcorrect, &tincorrect);
if (strcmp(temp, name) == 0)
{
*earnings = tearning;
*correct = tcorrect;
*incorrect = tincorrect;
}
}
}
fclose(fp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment