C Source Codes
To save any of the code below: Click on "Get the .C File"
dnd_stat_roller.c
#include <stdio.h>
int main () {
int i,j,k,low_roll, low_stat, sum, stat_sum, points;
int dice[4],stat[6];
do {
srand ((unsigned) time(NULL));
for(low_stat=18,stat_sum=i=0;i<6;stat_sum+=stat[i],i++) {
printf("\nRolls:\t");
for(low_roll=6,sum=j=0;j<4;sum+=dice[j],j++) { // "Rolls" 4d6 and Sums
dice[j] = rand()%6+1;
printf("%d ",dice[j]);
if(dice[j] < low_roll) low_roll = dice[j]; // Keeps Track of Lowest Roll for current set of 4d6
}
sum-=low_roll; // For the purposes of D&D Character Creation, only the best 3 rolls are counted
stat[i]=sum;
if(stat[i] < low_stat) low_stat = stat[i]; // Keeps Track of Lowest Stat
printf("\tStat:\t%d",stat[i]);
}
printf("\n\t\t\tAvg:\t%lf\n",(double) stat_sum/i);
if(low_stat>=10) points=2; // Costs 2 to buy 8 up to 10 FIRST
else if(low_stat==9) points=1; // Costs 1 to buy 8 up to 9
else points=0;
for(i=0;i<6;i++) {
if(stat[i]<=10) ;
else if(stat[i]==11) points+=1;
else if(stat[i]==12) points+=2;
else if(stat[i]==13) points+=3;
else if(stat[i]==14) points+=5;
else if(stat[i]==15) points+=7;
else if(stat[i]==16) points+=9;
else if(stat[i]==17) points+=12;
else if(stat[i]==18) points+=16;
}
printf("\nPoint System Equivalent:\t%d\n",points);
printf("\nEnter 0 to exit:\t");
scanf("%d",&k); fflush(stdin);
} while(k!=0);
return 0;
}
dice_roller.c
#include <stdio.h>
int main() {
int rolls,sum,i,j,k;
do{
printf("How many times? ");
scanf("%d",&i); // i <= 0 results in program termination
if(i>0) {
fflush(stdin);
do {
printf("How many sided? ");
scanf("%d",&j);
fflush(stdin);
if(j<4) printf("\nERROR: Must roll 4 sided or larger.\n");
else printf("\nRolling %dd%d\n",i,j);
} while(j<4); // Must Roll d4 or larger
srand ((unsigned) time(NULL));
for(k=rolls=sum=0;rolls<i;sum+=k,rolls++) { // Rolls, Prints Roll, Sums, Increments Counter
k = rand()%j+1;
printf("%d ",k);
}
printf("\n\n");
if(i>1) printf("Average: %lf\n",(double) sum/i); // Prints Average
printf("Total: %d\n\n",sum); // Prints Sum of Rolls
}
} while (i>0);
return 0;
}
html_prep.c
/*
All of the below was authored by me.
By using any part of this source code, you agree to waive all liabilities from the author.
Furthermore, you agree that you will not use this source code for personal (or corporate) gain.
Finally, should you change any part of this source code, you agree to give credit to the author where such credit is due.
This program prepares code for xhtml display (set your CSS to use font-family: monospace).
Simply Compile, then drag and drop your files onto the .exe
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
int main (int argc, char *argv[])
{ char *fpath, c1;
int h,i,j,k;
FILE *in,*out;
if(argc<2) { printf("ERROR! Requires at least one argument.\n"); system("pause"); return -1; }
for(k=0,i=1; i<argc; i++)
{ in=fopen (argv[i], "r");
if(in!=NULL)
{ for(j=0; argv[i][j]!='\0'; j++);
fpath = (char *) malloc( (j * 1)+4); // sizeof(char) == 1
for(j=0; argv[i][j]!='\0'; j++)
*(fpath+j) = argv[i][j];
*(fpath+j)='.';
*(fpath+j+1)='h';
*(fpath+j+2)='t';
*(fpath+j+3)='m';
*(fpath+j+4)='\0';
/* At this point, fpath = argv[i].htm */
out=fopen(fpath, "w");
if(out!=NULL)
{ for(h=0;!feof(in);h++)
{ c1 = fgetc(in);
if (feof(in)) break;
else if(c1=='<') { fprintf(out,"<"); }
else if(c1=='>') { fprintf(out,">"); }
else if(c1=='&') { fprintf(out,"&"); }
else if(c1=='\"') { fprintf(out,"""); }
else if(c1=='\t') { for(;h%7;h++) fprintf(out," "); }
else if(c1=='\n') { fprintf(out,"<br />\n"); h=0; }
else fputc(c1,out);
}
}
free(fpath); fclose(out);
}
else
{ printf("%s could not be opened.\n",argv[i]);
k++;
}
fclose(in);
}
if(k!=0) system("pause");
return k;
}
filename_space_to_underscore.c
/*
All of the below was authored by me.
By using any part of this source code, you agree to waive all liabilities from the author.
Furthermore, you agree that you will not use this source code for personal (or corporate) gain.
Finally, should you change any part of this source code, you agree to give credit to the author where such credit is due.
This program converts the spaces in your file name to underscores.
Simply Compile, then drag and drop your files onto the .exe
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
int main (int argc, char *argv[])
{ char *fpath;
int i,j,k;
if(argc<2) { printf("ERROR! Requires at least one argument.\n"); system("pause"); return -1; }
for(k=0,i=1; i<argc; i++)
{ for(j=0; argv[i][j]!='\0'; j++);
fpath = (char *) malloc(j * 1); // sizeof(char) == 1
for(j=0; argv[i][j]!='\0'; j++)
*(fpath+j) = argv[i][j];
*(fpath+j)='\0';
for(;*(fpath+j)!='\\' || j==0 ;j--) ; // Non-windows: Change the condition to "*(fpath+j) != '\/' || j==0"
for(;*(fpath+j)!='\0';j++)
if(isspace(*(fpath+j))) *(fpath+j)='_';
if(rename(argv[i],fpath)) // rename(old,new) returns zero on successful rename
{ printf("Renaming of %s failed.\n",argv[i]);
printf("errno value: %d\n\n", errno);
k++;
}
free(fpath);
}
if(k!=0) system("pause"); // Pause at end of execution if rename errors occurred to allow user to record errno values
return k;
}
bragg.c
/*
All of the below was authored by me.
By using any part of this source code, you agree to waive all liabilities from the author.
Furthermore, you agree that you will not use this source code for personal (or corporate) gain.
Finally, should you change any part of this source code, you agree to give credit to the author where such credit is due.
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define pi atan2(0.,-1.)
#define n 5120
void deblank (char *s); // " ABCD EF " --> "ABCD EF"
void endnull (char *s); // Ensures strings end with nulls
int main () {
char outfile[n];
int h,k,l,count,exit;
double theta,bragg, dhkl, wavelength, a, b, c, max_brag, min_brag;
FILE *out;
for(exit=1;exit;) {
/* Get Lattice Parameters, and Wavelength of Monochromatic X-ray */
do{ printf("Enter Lattice Parameter 'a' (nm): "); scanf("%lf",&a); } while(a<0);
do{ printf("Enter Lattice Parameter 'b' (nm): "); scanf("%lf",&b); } while(b<0);
do{ printf("Enter Lattice Parameter 'c' (nm): "); scanf("%lf",&c);} while(c<0);
do{ printf("Enter Wavelength 'lamda' (nm): "); scanf("%lf",&wavelength); } while (wavelength<0.);
do{ printf("Min Desired Bragg Angle (degrees): "); scanf("%lf",&min_brag);
} while (min_brag<0. && min_brag<=180.);
do{ printf("Max Desired Bragg Angle (degrees): "); scanf("%lf",&max_brag);
} while (max_brag<min_brag && max_brag<=180.);
fflush(stdin);
/* Get Output File Name */
do {
printf("Output path: ");
gets(outfile); endnull(outfile); deblank(outfile);
out=fopen (outfile, "w");
if (out==NULL) printf ("\nUh oh!\n\n%s cannot be opened.\nMake sure %s exists.\n\n",outfile,outfile);
} while (out==NULL);
fflush(stdin);
fprintf(out,"This file is tab-delimited for your convenience.\n\na: %lf;\tb: %lf;\tc: %lf;\twavelength: %lf;\nPlane\tdhkl\tBragg (Degrees)\n",a,b,c,wavelength);
/* Find Bragg Angles */
for(count=l=0;l<10;l++) {
for(k=0;k<10;k++) {
for(h=0;h<10;h++) {
if(h==0 && k==0 && l==0) continue;
dhkl = sqrt (1/(h*h*(1/a)*(1/a) + k*k*(1/b)*(1/b) + l*l*(1/c)*(1/c)));
if( (wavelength/(2*dhkl)) <= 1.) {
theta = asin(wavelength/(2*dhkl));
theta *= (180 / pi);// Radian --> Degrees
bragg = 2*theta;
if(bragg >= min_brag && bragg <= max_brag) {
fprintf(out,"(%d%d%d)\t%lf\t%lf\n",h,k,l,dhkl,bragg);
count++;
}
}
}
}
}
fprintf(out,"\n\n%d Planes diffract within desired range\nDesired Bragg Angle Range (Degrees):%lf - %lf",count,min_brag,max_brag);
printf("Enter 0 to exit: "); scanf("%d",&exit);
}
return 0;
}
void deblank (char *s) {
char *t=s;
for(;*s!='\0';s++) ;
s--;
for (;*s==' ';s--) *s='\0';
for (s=t;*s==' ';s++) ;
for (;*s!='\0';t++,s++) *t=*s;
*t='\0';
return;
}
void endnull (char *s) {
int i;
for(i=0;i<n || *(s+i)=='\0';i++) {
if(i==n-1) *(s+i)='\0';
}
return;
}
newline_begone.c
/*
All of the below was authored by me.
By using any part of this source code, you agree to waive all liabilities from the author.
Furthermore, you agree that you will not use this source code for personal (or corporate) gain.
Finally, should you change any part of this source code, you agree to give credit to the author where such credit is due.
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define n 5120
void deblank (char *s); // " ABCD EF " --> "ABCD EF"
void endnull (char *s); // Ensures strings end with nulls
int strdiff(char *s, char *t);
int filedif(char *s, char *t);
// strdiff returns zero if two strings are identical ----> if (s="abc.txt" && t="path\abc.txt") return 1;
// filedif returns zero if two filenames are identical --> if (s="abc.txt" && t="path\abc.txt") return 0;
int main() {
char infile[n],outfile[n],c1,exit;
FILE *in, *out;
int i,j;
do {
/* Get INPUT filename */
do {
printf("Input path: ");
gets(infile); endnull(infile); deblank(infile);
in=fopen (infile, "r");
if (in==NULL) printf ("\nUh oh!\n\n%s cannot be opened.\nMake sure %s exists.\n\n",infile,infile);
} while (in==NULL); fflush(stdin);
do {
printf("Output path: ");
gets(outfile); endnull(outfile); deblank(outfile);
if(filedif(infile,outfile)) {
out=fopen(outfile,"w");
if (out==NULL) printf ("\nUh oh!\n\n%s cannot be created.\n\n",outfile);
}
else {
printf("\nChoose a different output filename!\n\n");
out=NULL;
}
} while(out==NULL);
/* Copy Data */
for(i=0,j=0;!feof(in);i++) {
c1 = fgetc(in);
if (feof(in)) break;
else if(c1!='\n') fputc(c1,out);
else j++;
}
printf("%d NEW-Line characters removed out of %d total characters.\n",j,i);
do {
printf("\nDo you want to exit (Y/N)?");
exit = getchar();
} while (exit!='Y' && exit!='y' && exit!='N' && exit!='n');
fclose(in); fclose(out); fflush(stdin);
} while (exit!='Y' && exit!='y');
return 0;
}
void deblank (char *s) {
char *t=s;
for(;*s!='\0';s++) ;
s--;
for (;*s==' ';s--) *s='\0';
for (s=t;*s==' ';s++) ;
for (;*s!='\0';t++,s++) *t=*s;
*t='\0';
return;
}
void endnull (char *s) {
int i;
for(i=0;i<n || *(s+i)=='\0';i++) {
if(i==n-1) *(s+i)='\0';
}
return;
}
int strdiff(char *s, char *t) {
int i,j,k;
for(k=0;*(s+k)==*(t+k) || *(t+k)=='\0' || *(s+k)=='\0' ;k++) ;
for(i=0;*(s+i)!='\0';i++) ;
for(j=0;*(t+j)!='\0';j++) ;
if(i!=j) return 1;
else if (k==j) return 0;
else return 1;
}
int filedif(char *s, char *t) {
char *so=s, *to=t;
for(;*s!='\0';s++) ;
for(;*t!='\0';t++) ;
for(;*s!='\\' && s!=so;s--);
for(;*t!='\\' && t!=to;t--);
if(s!=so) s++;
if(t!=to) t++;
if (strdiff(s,t)) return 1;
else return 0;
}
mastermind.c
/*
All of the below was authored by me.
By using any part of this source code, you agree to waive all liabilities from the author.
Furthermore, you agree that you will not use this source code for personal (or corporate) gain.
Finally, should you change any part of this source code, you agree to give credit to the author where such credit is due.
This code utilizes pointers, and creates one-dimensional arrays using "pointer = (cast) malloc (arguments);".
When modifying this code, it is important to keep in mind that variables declared within a function, and variables declared inside a function arguement list are function specific (local).
*/
#include <stdio.h>
#include <stdlib.h>
void config(int *colors, int *size, int *guesses);
void gen_arr(int *arr, int colors, int size);
void get_arr(int *arr, int colors, int size);
void arr_copy(int *arr, int *arrcopy, int size);
int pos_chk(int *gss, int *brd, int size);
int clr_chk(int *gss, int *gss_cpy, int *brd, int size);
int main() {
int *brd, *gss, *gss_cpy, i, exit, colors, size, guesses, won, lost, win, pos_cor, clr_cor;
printf("---=== Mastermind ===---\n");
for (won=lost=0,exit=1;exit;) {
do {
config(&colors, &size, &guesses);
/* sizeof(int) == 4 */
gss = (int *) malloc(size * 4);
gss_cpy = (int *) malloc(size * 4);
brd = (int *) malloc(size * 4);
if (brd==NULL || gss==NULL || gss_cpy==NULL)
printf("Uh Oh! Your computer doesn't like me. Try setting FEWER positions.");
} while (brd==NULL || gss==NULL || gss_cpy==NULL);
printf("\n\n\n%d colors to choose from, each represented by POSITIVE integers (1 to %d).\nEnter 0 to change previous position.\nYou have %d tries.\nGood Luck!\n\n\n",colors,colors,guesses);
gen_arr(brd,colors,size); // This initializes the "brd" array created using "pointer = malloc(arguments);"
for (i=1; i<=guesses;i++) {
get_arr(gss,colors,size); // This initializes the "gss" array
pos_cor = pos_chk(gss,brd,size); // Checks if gss and brd arrays are identical; if not, how many values are the same
if (pos_cor!=4) {
clr_cor = clr_chk(gss,gss_cpy,brd,size);
printf("\n\n\n%d colors correct with %d in proper position.\n%d tries remaining.\n\n\n",clr_cor,pos_cor,guesses-i);
win=0;
}
else { win=1; break; }
}
if(!win) {
lost++;
putchar(10); // ASCII character 10 is the newline character
for (i=0; i<size; i++) printf(" %d",*(brd+i));
printf(" was the answer.\n\nBetter Luck Next Time.\n\n");
}
else { won++; printf("Congratulations! You solved the puzzle!\n"); }
/*
This only displays how many times you have played for THIS time of running the executable.
This does NOT store any information on the computer running the executable.
You are welcome to code in some way of keeping track of this; however, do so at your own risk
*/
printf("You have played %d time (%d wins %d losses).\n\nEnter 0 to exit. ",won+lost,won,lost);
scanf("%d",&exit);
free(gss); free(gss_cpy); free(brd);
gss=NULL; gss_cpy=NULL; brd=NULL;
fflush(stdin); // Clears Input Buffer
}
system("pause");return 0;
}
void config(int *colors, int *size, int *guesses) {
do{ printf("Number of different colors (Minimum: 6): "); scanf("%d",colors); } while (*colors<6);
do{ printf("Number of positions (Minimum: 4): "); scanf("%d",size); } while (*size<4);
do{ printf("Number of guesses (Minimum: 1): "); scanf("%d",guesses);} while (!(*size));
}
void gen_arr(int *arr,int colors, int size) {
int i;
srand ((unsigned) time(NULL));
for (i=0; i<size; i++) *(arr+i)=rand()%colors+1;
return;
}
void get_arr (int *arr,int colors, int size) {
int i;
for (i=0; i<size; i++)
do {
if (i<0) i=0;
printf("Color of Position %d: ",i+1);
scanf("%d", arr+i);
if (*(arr+i)>colors || *(arr+i)<0)
printf("Invalid Color Integer. Please choose between 1 and %d inclusive.\n",colors);
else if (!*(arr+i)) i-=2;
} while (*(arr+i)>colors || *(arr+i)<0);
return;
}
void arr_copy(int *arr, int *arrcopy, int size) {
int i;
for (i=0;i<size;i++)
*(arrcopy+i) = *(arr+i);
return;
}
int pos_chk(int *gss, int *brd, int size) {
int i,correct;
for (correct=i=0;i<size;i++)
if (*(gss+i) == *(brd+i)) correct++;
return correct;
}
int clr_chk(int *gss, int *gss_cpy, int *brd, int size) {
int i,j,correct;
putchar(10);
arr_copy(gss, gss_cpy, size);
for (correct=i=0;i<size;i++)
for (j=0;j<size;j++) {
if (*(gss+j) != *(brd+i)) continue;
else { *(gss+j)= -1;correct++; break;}
}
arr_copy(gss_cpy,gss,size);
return correct;
}