Created
March 22, 2014 08:28
-
-
Save xcvista/9703176 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* quesgen.c - Math question generator for elementary school pupils | |
* Copyright (c) 2014 Maxthon Chan <[email protected]> | |
* | |
* Compiling this program: Use your preferred C99 compiler. clang/LLVM is | |
* preferred, GCC is acceptable, MSVC is not guaranteed to work. | |
* clang quesgen.c -std=c99 -o quesgen | |
* No special library other than standard C library is required. | |
* | |
* Using this program: | |
* ./quesgen [count] > output.txt | |
* By default, it generates 100 questions. | |
* | |
* License: | |
* | |
* You are allowed to use, modify, copy and distribute this software, as long as | |
* you: | |
* 1) Keep the copyright notice, this license and the following disclaimer | |
* in your modified and/or distributed source code, | |
* 2) Keep the copyright notice, this license and the following disclaimer | |
* with your modified and/or distributed binary code. | |
* 3) Not claim your modified version of this program a work of its original | |
* author(s) unless explicitedly allowed. | |
* | |
* Disclaimer: | |
* | |
* THIS SOFTWARE IS PROVIDED TO YOU ON THE "AS-IS" BASIS. NO WARRANTY WHATSOEVER | |
* IS PROVIDED OR GUARANTEED FROM THE AUTHORS OF THIS SOFTWARE, INCLUDING BUT | |
* NOT LIMITED TO SUITABILITY OF MERCHANT, ABILITY TO SUPPORT CRITICAL SYSTEMS | |
* OR ANY SPECIFIC USE CASE. YOU ARE USING THIS SOFTWARE AT YOUR OWN RISK. WE | |
* AUTHORS TRY OUR BEST TO PREVENT PERCIEVEABLE ISSUES IN THIS SOFTWARE, BUT | |
* WE CANNOT GUARANTEE IT IS ERROR-FREE. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
int main(int argc, char *const *argv) | |
{ | |
int count = 100; | |
int a, b, sym; | |
if (argc > 1) // Handle the sole argument, if exists. | |
{ | |
count = atoi(argv[1]); | |
} | |
if (count <= 0) // Handle bad argument... | |
{ | |
count = 100; // ... by ignoring it. | |
} | |
srand(time(NULL)); // Initialize PRNG. | |
for (int i = 0; i < count; i++) | |
{ | |
sym = rand() & 0x3; // Generate the symbol. Faster this way. | |
if (sym == 3) // Division is handled differently. | |
{ | |
a = rand() % 30 + 1; | |
b = rand() % 30 + 1; | |
printf("%d / %d =\n", a * b, b); | |
} | |
else // Other three is trivial. | |
{ | |
static const char syms[] = "+-*"; // Faster this way. | |
a = rand() % 100 + 1; | |
b = rand() % 100 + 1; | |
printf("%d %c %d =\n", a, syms[sym], b); | |
} | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment