Added comments.

This commit is contained in:
Deathsbreed 2014-11-03 11:47:49 -06:00
parent f116a4727c
commit 165213a1ad

View File

@ -21,26 +21,31 @@
#include <string.h>
#include <time.h>
// Global version string
char version[7] = "v0.1";
/* Function prototypes start */
void printCopyright();
void printAllCopyright();
void printWarranty();
/* Function prototypes end */
// Main function
int main(int argc, char **argv) {
if(argc < 2 || argc > 3) {
// If inappropriate number of arguments
if(argc < 2 || argc > 3) { // If inappropriate number of arguments (number required = 2)
printf("Inappropriate amount of arguments.");
printf("Usage: %s [num_trials]\n", argv[0]);
return 1;
}
if(strcmp(argv[1], "-c") == 0) {
if(strcmp(argv[1], "-c") == 0) { // If argv[1] (the second argument) is equal to "-c" ...
printAllCopyright();
return 0;
} else if(strcmp(argv[1], "-w") == 0) {
} else if(strcmp(argv[1], "-w") == 0) { // If argv[1] is equal to "-w"
printWarranty();
return 0;
} else if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) {
} else if(strcmp(argv[1], "-h") == 0 || strcmp(argv[1], "--help") == 0) { // If argv[1] is equal to "-h" or "--help"
printf("Usage: %s [num_trials]\n\
-c -- Print copyright info\n\
-h | --help -- Print this help information\n\
@ -48,9 +53,8 @@ int main(int argc, char **argv) {
return 0;
} else {
int i = 0;
while(argv[1][i] != '\0') {
printf("%c", argv[1][i]);
if(isdigit(argv[1][i] - '0') != 0) {
while(argv[1][i] != '\0') { // While argv[1][i] is not equal to NULL (can't use NULL for pointers)
if(isdigit(argv[1][i] - '0') != 0) { // if argv[1][i] is not a number between 0 and 9
printf("You did not specify a number.\n");
printf("Usage: %s [num_trials]\n", argv[0]);
return 1;
@ -65,12 +69,13 @@ int main(int argc, char **argv) {
int heads = 0;
int tails = 0;
// Setup the random number generator with the seed being the time
srand(time(NULL));
printf("Calculating random numbers...\n");
for(int i = 0; i < trials; i++) {
if(i % 100000000 == 0) printf("Working...\n");
int r = rand() % 100 + 1;
int r = rand() % 100 + 1; // Calculate random number between 1 and 100
if(r <= 50) heads++;
else tails++;
}
@ -81,6 +86,7 @@ int main(int argc, char **argv) {
return 0;
}
/* Defined functions start */
void printCopyright() {
printf("CoinToss %s Copyright (C) 2014 Nicolás A. Ortega\n\
This program comes with ABSOLUTELY NO WARRANTY; for details use `-w'.\n\
@ -106,10 +112,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.\n");
void printWarranty() {
printf(" THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY\n\
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT\n\
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY\n\
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM \"AS\" IS WITHOUT WARRANTY\n\
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,\n\
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n\
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM\n\
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF\n\
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.\n");
}
/* Defined functions end */