How to count in windows bat files

Filed Under (script) by The Chef on 13-05-2009

Tagged Under : ,

This is a simple way to count form 0 to 1000 using bat files.

@echo off
if [%1]==[] goto NONE
if [%2]==[] goto ONE
if [%3]==[] goto TWO
goto THREE

:NONE
for %%x in (0 1 2 3 4 5 6 7 8 9) do call %0 %%x
goto DONE

:ONE
for %%x in (0 1 2 3 4 5 6 7 8 9) do call %0 %1 %%x
goto DONE

:TWO
for %%x in (0 1 2 3 4 5 6 7 8 9) do call %0 %1 %2 %%x
goto DONE

:THREE
echo %1%2%3
goto DONE

 :D ONE

10 tools a C/C++/C#/PHP programmer should never miss

Filed Under (c#, c/c++, mysql, net, php, system, tools) by The Chef on 13-05-2009

Tagged Under : , , ,

Calculate a file Shannon entropy in C

Filed Under (c/c++) by The Chef on 08-05-2009

Tagged Under : , ,

In information theory, entropy is a measure of the uncertainty associated with a random variable. The term by itself in this context usually refers to the Shannon entropy, which quantifies, in the sense of an expected value, the information contained in a message, usually in units such as bits. Equivalently, the Shannon entropy is a measure of the average information content one is missing when one does not know the value of the random variable.


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

int main(int argc, char **argv) {

	FILE * pFile;
	int n = 0;
	unsigned char* buffer;
	double entropy = 0.0;
	clock_t t0, t1;

	t0 = clock();

	/*
	 * read buffer
	 */
	buffer = malloc(64 * 1024);
	/*
	 * alphabet array with occurances
	 * each item index represents the value of alphabet
	 * each item  value represents the occurance
	 */
	long alphabet[256];
	int i;
	for (i = 0; i < 256; i++) {
		alphabet[i] = 0;
	}

	/*
	 * total items
	 */
	int t = 0;

	/*
	 * reading the file
	 */
	pFile = fopen(argv[1], "rb");
	do {
		n = fread(buffer, 1, 64 * 1024, pFile);
		for (i = 0; i < n; i++) {
			/*
			 * update occurance
			 */
			alphabet[(int) buffer[i]]++;
			t++;
		}
	} while (n != 0);
	fclose(pFile);

	/*
	 * entropy calculation
	 */
	for (i = 0; i < 256; i++) {
		if (alphabet[i] != 0) {
			double r = (double) alphabet[i] / (double) t;
			entropy += -r * log2(r);
		}
	}
	t1 = clock();

	printf("Entropy = %2.5f\n", entropy);
	printf("Exec time: %g seconds.\n", (double) (t1 - t0) / (double) CLOCKS_PER_SEC);

	return 0;
}
Page 4 of 10« First...23456...Last »