Switching to Python.
This commit is contained in:
122
src/CoinToss.py
Normal file
122
src/CoinToss.py
Normal file
@ -0,0 +1,122 @@
|
||||
###
|
||||
# CoinToss, a probability simulator.
|
||||
# Copyright (C) 2014 Nicolas A. Ortega
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License or
|
||||
# (at your option) any later.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
##
|
||||
|
||||
import sys
|
||||
from random import randint
|
||||
|
||||
version = "v0.3"
|
||||
|
||||
### DEFINE FUNCTIONS ###
|
||||
def printUsage():
|
||||
print "Usage:", sys.argv[0], "[[num_trials] | [args]]\nUse -h or --help for more information."
|
||||
|
||||
def printCopyright():
|
||||
print """CoinToss""", version, """Copyright (C) 2014 Nicolas A. Ortega\n
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details use `-w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; use `-c' for details\n"""
|
||||
|
||||
def printAllCopyright():
|
||||
print """CoinToss, a probability simulator.
|
||||
Copyright (C) 2014 Nicolas A. Ortega\n
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.\n
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.\n
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.\n"""
|
||||
|
||||
def printWarranty():
|
||||
print """ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.\n"""
|
||||
|
||||
def printHelp():
|
||||
printUsage()
|
||||
print """ -c -- Print copyright information
|
||||
-h | --help -- Print this help information
|
||||
-w -- Print warranty information
|
||||
-v -- Print version"""
|
||||
|
||||
|
||||
### MAIN ###
|
||||
|
||||
# If no arguments specified
|
||||
if len(sys.argv) == 1:
|
||||
printCopyright()
|
||||
printUsage()
|
||||
sys.exit(0)
|
||||
# Check if appropriate number of arguments
|
||||
elif len(sys.argv) != 2:
|
||||
print "Inappropriate number of arguments."
|
||||
printUsage()
|
||||
sys.exit(0)
|
||||
# Check if copyright argument is passed
|
||||
elif sys.argv[1] == "-c":
|
||||
printAllCopyright()
|
||||
sys.exit(0)
|
||||
# Check if warranty argument is passed
|
||||
elif sys.argv[1] == "-w":
|
||||
printWarranty()
|
||||
sys.exit(0)
|
||||
# Check if help argument is passed
|
||||
elif sys.argv[1] == "-h" or sys.argv[1] == "--help":
|
||||
printHelp()
|
||||
sys.exit(0)
|
||||
# Check if version argument is passed
|
||||
elif sys.argv[1] == "-v":
|
||||
print "CoinToss", version
|
||||
sys.exit(0)
|
||||
|
||||
printCopyright()
|
||||
|
||||
# Create necessary variables
|
||||
trials = int(sys.argv[1])
|
||||
heads = 0
|
||||
tails = 0
|
||||
|
||||
oldProgress = 0
|
||||
|
||||
print "Calculating random numbers..."
|
||||
print "Progress: 0 %"
|
||||
for i in range(0, trials):
|
||||
progress = (i * 100) / trials
|
||||
if progress is not oldProgress and progress % 10 is 0:
|
||||
print "Progress:", progress, "%"
|
||||
oldProgress = progress
|
||||
|
||||
if randint(1, 10) <= 5:
|
||||
heads += 1
|
||||
else:
|
||||
tails += 1
|
||||
|
||||
print "Progress: 100 %"
|
||||
print "Done.\n"
|
||||
print "Heads:", heads
|
||||
print "Tails:", tails
|
||||
|
||||
sys.exit(0)
|
131
src/main.c
131
src/main.c
@ -1,131 +0,0 @@
|
||||
/**
|
||||
* CoinToss, a probability simulator.
|
||||
* Copyright (C) 2014 Nicolás A. Ortega
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
// Global version string
|
||||
char version[7] = "v0.2";
|
||||
|
||||
/* Function prototypes start */
|
||||
void printCopyright();
|
||||
void printAllCopyright();
|
||||
void printWarranty();
|
||||
/* Function prototypes end */
|
||||
|
||||
// Main function:
|
||||
int main(int argc, char **argv) {
|
||||
// If inappropriate number of arguments
|
||||
if(argc < 2 || argc > 3) { // If inappropriate number of arguments (number required = 2)
|
||||
printf("Inappropriate amount of arguments.\n");
|
||||
printf("Usage: %s [num_trials]\n\nUse -h or --help for more information.", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(!strcmp(argv[1], "-c")) { // If argv[1] (the second argument) is equal to "-c" ...
|
||||
printAllCopyright();
|
||||
return 0;
|
||||
} else if(!strcmp(argv[1], "-w")) { // If argv[1] is equal to "-w"
|
||||
printWarranty();
|
||||
return 0;
|
||||
} else if(!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { // 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\
|
||||
-w -- Print warranty information\n", argv[0]);
|
||||
return 0;
|
||||
} else {
|
||||
int i = 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')) { // 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;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
printCopyright();
|
||||
|
||||
long long int trials = atoi(argv[1]);
|
||||
long long int heads = 0;
|
||||
long long int tails = 0;
|
||||
|
||||
// Setup the random number generator with the seed being the time
|
||||
srand(time(NULL));
|
||||
|
||||
long long int oldprogress = 0;
|
||||
|
||||
printf("Calculating random numbers...\n");
|
||||
printf("Progress: 0 %%\n");
|
||||
for(long long int i = 0; i < trials; i++) {
|
||||
long long int progress = (i * 100) / trials;
|
||||
if(progress != oldprogress && progress % 10 == 0) {
|
||||
printf("Progress: %lli %%\n", progress);
|
||||
oldprogress = progress;
|
||||
}
|
||||
int r = rand() % 100 + 1; // Calculate random number between 1 and 100
|
||||
if(r <= 50) heads++;
|
||||
else tails++;
|
||||
}
|
||||
printf("Progress: 100 %%\n");
|
||||
|
||||
printf("Done.\n\n");
|
||||
printf("Heads: %lli\n", heads);
|
||||
printf("Tails: %lli\n", tails);
|
||||
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\
|
||||
This is free software, and you are welcome to redistribute it\n\
|
||||
under certain conditions; use `-c' for details.\n\n\
|
||||
The source-code can be found at http://github.com/Deathsbreed/CoinToss\n\n", version);
|
||||
}
|
||||
|
||||
void printAllCopyright() {
|
||||
printf("CoinToss, a probability simulator.\n\
|
||||
Copyright (C) 2014 Nicolás A. Ortega\n\n\
|
||||
This program is free software: you can redistribute it and/or modify\n\
|
||||
it under the terms of the GNU General Public License as published by\n\
|
||||
the Free Software Foundation, either version 3 of the License, or\n\
|
||||
(at your option) any later version.\n\n\
|
||||
This program is distributed in the hope that it will be useful,\n\
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
|
||||
GNU General Public License for more details.\n\n\
|
||||
You should have received a copy of the GNU General Public License\n\
|
||||
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\
|
||||
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 */
|
Reference in New Issue
Block a user