AISolving.cpp file   Leave a comment

// AIPSolving.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "AIPSolving.h"

#include "AIUtils.h"

#include "range_array.h"

#include "mzalgorithms.h"

#include "mzlist.h"

#include <fstream>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

using namespace marzan;

#define _MAX_MATRIX_ORDER 1000

double marzan_metrix[_MAX_MATRIX_ORDER][_MAX_MATRIX_ORDER];

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		/*basic_string*/const TCHAR* palindromes[] = {
			_T("A man, a plan, a caret, a ban, a myriad, a sum, a lac, a liar, a hoop, a pint, a catalpa, a gas, an oil, a bird, ")
			_T("a yell, a vat, a caw, a pax, a wag, a tax, a nay, a ram, a cap, a yam, a gay, a tsar, a wall, a car, a luger, a ward, ")
			_T("a bin, a woman, a vassal, a wolf, a tuna, a nit, a pall, a fret, a watt, a bay, a daub, a tan, a cab, a datum, a gall, ")
			_T("a hat, a fag, a zap, a say, a jaw, a lay, a wet, a gallop, a tug, a trot, a trap, a tram, a torr, a caper, a top, a tonk, ")
			_T("a toll, a ball, a fair, a sax, a minim, a tenor, a bass, a passer, a capital, a rut, an amen, a ted, a cabal, a tang, a sun, ")
			_T("an ass, a maw, a sag, a jam, a dam, a sub, a salt, an axon, a sail, an ad, a wadi, a radian, a room, a rood, a rip, a tad, ")
			_T("a pariah, a revel, a reel, a reed, a pool, a plug, a pin, a peek, a parabola, a dog, a pat, a cud, a nu, a fan, a pal, a rum, ")
			_T("a nod, an eta, a lag, an eel, a batik, a mug, a mot, a nap, a maxim, a mood, a leek, a grub, a gob, a gel, a drab, a citadel, ")
			_T("a total, a cedar, a tap, a gag, a rat, a manor, a bar, a gal, a cola, a pap, a yaw, a tab, a raj, a gab, a nag, a pagan, a bag, ")
			_T("a jar, a bat, a way, a papa, a local, a gar, a baron, a mat, a rag, a gap, a tar, a decal, a tot, a led, a tic, a bard, a leg, ")
			_T("a bog, a burg, a keel, a doom, a mix, a map, an atom, a gum, a kit, a baleen, a gala, a ten, a don, a mural, a pan, a faun, ")
			_T("a ducat, a pagoda, a lob, a rap, a keep, a nip, a gulp, a loop, a deer, a leer, a lever, a hair, a pad, a tapir, a door, ")
			_T("a moor, an aid, a raid, a wad, an alias, an ox, an atlas, a bus, a madam, a jag, a saw, a mass, an anus, a gnat, a lab, a cadet, ")
			_T("an em, a natural, a tip, a caress, a pass, a baronet, a minimax, a sari, a fall, a ballot, a knot, a pot, a rep, a carrot, a mart, ")
			_T("a part, a tort, a gut, a poll, a gateway, a law, a jay, a sap, a zag, a fat, a hall, a gamut, a dab, a can, a tabu, a day, a batt, ")
			_T("a waterfall, a patina, a nut, a flow, a lass, a van, a mow, a nib, a draw, a regular, a call, a war, a stay, a gam, a yap, a cam, ")
			_T("a ray, an ax, a tag, a wax, a paw, a cat, a valley,")
			_T("a drib, a lion, a saga, a plat, a catnip, a pooh, a rail, a calamus, a dairyman, a bater, a canal - Panama!"), 
			_T("civic"),
			_T("evil live!"),
			_T("evil camp :up pup,mac,live!"),
			_T("Able was I, ere I saw Elba."),
			_T("Sex at noon taxes!"),
			_T("Campus motto: Bottoms up, Mac! "),
			_T("Elba anemone, H.P. stressed, a-sits; 'tis a desserts phenomena able!"),
			_T("Madam, I'm Adam."),
			_T("Dennis and Edna sinned."),
			_T("No evil I did, I live on. "),
			_T("Rotary gyrator "),
			_T("A Man, a plan, a canal - Panama!"),
			_T("Was it a cat I saw? "),
			_T("Was it a car or a cat I saw? "),
			_T("Swap God for a Janitor, Rot in a jar of dog paws."),
			_T("No, son, onanism's a gross orgasm sin: a no-no, son."),
			_T("To old Dallas, we Jews, all add loot."),
			_T("Doc, note, I dissent. A fast never prevents a fatness. I diet on cod."),
			_T("A man, a plan, a canoe, pasta, heros, rajahs, a coloratura, maps, snipe, percale, macaroni, a gag, a banana bag, a tan, a tag, a banana bag again, or: a camel, a crepe, pins, spam, a rut, a Rolo, cash, a jar, sore hats, a peon, a canal -- Panama! "),
		};

		int palc = sizeof(palindromes)/sizeof(palindromes[0]);
		int pali = 0;
		volatile bool bVal = false;
		while(pali < palc){
			bVal = is_palindrome(palindromes[pali++]);
			cout << (bVal ? _T("Yep!") : _T("Nop")) << endl;
		}

		__uint _x = gcd(17, 53), 
			   _y = gcd(53, 17 % 53);

		cout << _T("Fibonacci Sequence Numbers:") << endl;

		clock_t start = clock();
		for(__uint k=0;k<=92; k++)
		{
			cout << _T("F(") << k << _T(") = ");
			printf("%I64d\n", Fibonacci(k));
		}

		double f1time = (clock() - start) / CLOCKS_PER_SEC;

		cout << _T("Divide and Conquer time:") << f1time << _T(" secs.") << endl;

		start = clock();
		for(k=0;k<=92; k++)
		{
			cout << _T("F(") << k << _T(") = ");
			printf("%I64d\n", FibonacciH(k));
		}

		double f2time = (clock() - start) / CLOCKS_PER_SEC;

		cout << _T("My Own Impl.(1) time:") << f2time << _T(" secs.") << endl;

		start = clock();
		for(k=0;k<=92; k++)
		{
			cout << _T("F(") << k << _T(") = ");
			printf("%I64d\n", FibonacciH2(k));
		}

		f2time = (clock() - start) / CLOCKS_PER_SEC;

		cout << _T("My Own Impl. (2) time:") << f2time << _T(" secs.") << endl;

		//cout << _T("postfix to infix:") << endl;
		//postfix_to_infix(cin);

		__uint uarray[20] = {1, 20, 15, 16, 2, 5, 6, 3, 7, 4, 9, 10, 19, 18, 14, 13, 11, 12, 17, 8};

		//quick_sort(uarray, uarray + sizeof(uarray)/ sizeof(uarray[0]));

		binary_insertion_sort(uarray, uarray + sizeof(uarray)/ sizeof(uarray[0]));

		__uint* up = interpolation_binary_search(uarray, uarray+sizeof(uarray)/sizeof(uarray[0]), 20);
		up = interpolation_binary_search(uarray, uarray+sizeof(uarray)/sizeof(uarray[0]), 1);

		up = interpolation_binary_search(uarray, uarray+sizeof(uarray)/sizeof(uarray[0]), 9);
		up = interpolation_binary_search(uarray, uarray+sizeof(uarray)/sizeof(uarray[0]), 3);

		up = interpolation_binary_search(uarray, uarray+sizeof(uarray)/sizeof(uarray[0]), 10);
		up = interpolation_binary_search(uarray, uarray+sizeof(uarray)/sizeof(uarray[0]), 5);
		up = interpolation_binary_search(uarray, uarray+sizeof(uarray)/sizeof(uarray[0]), 15);

		up = interpolation_binary_search(uarray, uarray+sizeof(uarray)/sizeof(uarray[0]), 8);
		up = interpolation_binary_search(uarray, uarray+sizeof(uarray)/sizeof(uarray[0]), 12);

		//

	srand(time(0));

	unsigned int _N = _MAX_MATRIX_ORDER;

	volatile bool bChangeSign = false;

	for(unsigned int _IX = 0; _IX < _N; _IX++)
		for(unsigned int _JX = 0; _JX < _N; _JX++)
		{
			marzan_metrix[_IX][_JX] = (double)(rand() % _MAX_MATRIX_ORDER) + ((double)rand() / _MAX_MATRIX_ORDER);

			if(bChangeSign)
				marzan_metrix[_IX][_JX] = -marzan_metrix[_IX][_JX];

			bChangeSign = !bChangeSign;
		}

		double _SubMatrixVal[_MAX_MATRIX_ORDER];

		unsigned int _X = 1;

		_SubMatrixVal[0] = marzan_metrix[0][0];

		double _MaxSubMatrix = _SubMatrixVal[0];

		printf("Sub Matrix is Total(Matrix[%ld, %ld]) = %.4f\n", _X, _X, _MaxSubMatrix);

		start = clock();

		for(unsigned int _K = 1; _K< _N; _K++)
		{
			double _SummSubMx = 0.0;

			for(unsigned int _R = 0; _R < _K; _R++)
				_SummSubMx += marzan_metrix[_R][_K];

			for(unsigned int _C = 0; _C <=_K; _C++) 				_SummSubMx += marzan_metrix[_K][_C]; 			_SubMatrixVal[_K] = _SubMatrixVal[_K - 1] + _SummSubMx; 			if(_SubMatrixVal[_K] > _MaxSubMatrix)
			{
				_MaxSubMatrix = _SubMatrixVal[_K];

				_X = _K + 1;			
				printf("Sub Matrix is Total(Matrix[%ld, %ld]) = %.4f\n", _X, _X, _MaxSubMatrix);
			}else{

				printf("Skipped Sub Matrix is Total(Matrix[%ld, %ld]) = %.4f\n", _K + 1, _K + 1, _SubMatrixVal[_K]);
			}

		}

		clock_t diff = clock() - start;

		printf("\n\nMax Sub Matrix is Total(Matrix[%ld, %ld]) = %.4f\n", _X, _X, _MaxSubMatrix);

		printf("%.4f secs\n", (double)diff / (double)CLOCKS_PER_SEC);

		system("pause");
		//

		string test = "Harold Marzan Mercado"; 
		const TCHAR* ttest = _T("Harold Marzan Mercado");

		hash_value _value1 = hash_pjw(test.begin(), test.end(), 0x1000),
				   _value2 = hash_pjw(ttest, ttest + _tcslen(ttest), 0x1000);

		int myArr[13] = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 4, 7, 9, 5};

		for(__uint is=0;is< sizeof(myArr)/sizeof(myArr[0]); is++)
			cout << myArr[is] << _T(" ");

		cout << endl;	

		QuickSort(myArr, sizeof(myArr)/sizeof(myArr[0]));
		quick_sort(myArr, myArr+ sizeof(myArr)/sizeof(myArr[0]));			

		InsertionSort(myArr, sizeof(myArr)/sizeof(myArr[0]));
		insertion_sort(myArr, myArr + sizeof(myArr)/sizeof(myArr[0]));

		BubbleSort(myArr, sizeof(myArr)/sizeof(myArr[0]));
		bubble_sort(myArr, myArr + sizeof(myArr)/sizeof(myArr[0]));

		SelectionSort(myArr, sizeof(myArr)/sizeof(myArr[0]));
		selection_sort(myArr, myArr+sizeof(myArr)/sizeof(myArr[0]));

		BinaryInsertionSort(myArr, sizeof(myArr)/sizeof(myArr[0]));
		binary_insertion_sort(myArr, myArr+sizeof(myArr)/sizeof(myArr[0]));

		for(is=0;is< sizeof(myArr)/sizeof(myArr[0]); is++)		
			cout << myArr[is] << _T(" ");

		cout << endl;	
;

		cout << _T("Ackermanns' s :") << ackermann_fn(2, 2) << endl;
		cout << _T("Ackermanns' s :") << ackermann_fn(3, 3) << endl;
		cout << _T("Ackermanns' s :") << ackermann_fn(2, 4) << endl;
		//
		PRNG rng;

		for(int iee=0; iee<=100;iee++){
			cout << rng.NextEx() << endl;
		}

		vector<RandomVariable*> rvs(3);
		rvs[0] = new SimpleRV();
		rvs[1] = new UniformRV(15, 49);
		rvs[2] = new ExpRV(1.45);

		vector<RandomVariable*>::iterator itee = rvs.begin();
		while(itee != rvs.end())
		{
			RandomVariable* rv_ptr = *itee;
			for(iee=0; iee<=100;iee++)
			{
				cout << rv_ptr->Generate() << endl;
			}
			itee++;
		}

		itee = rvs.begin();
		while(itee != rvs.end())
			delete *itee++;

		pascal_triangle(cout, 10);

		int ahrr[] = { 1, 3, 5, 7, 9, 12, 23, 28, 34, 37, 39, 41, 42, 43, 56, 57, 59, 60};

		int* p = marzan::binary_search(ahrr, ahrr + sizeof(ahrr)/sizeof(ahrr[0]), 41);
		cout << *p << endl;

		array killer(sizeof(ahrr)/sizeof(ahrr[0]));
		for(__uint u = 0; u< killer.size(); u++)
			killer[u] = ahrr[u];

		__uint killed = marzan::binary_search(killer, 7, 0, killer.size());		
		cout << killer[killed] << endl;	
		killed = binary_search(killer, 23, 0, killer.size());		
		cout << killer[killed] << endl;	
		killed = binary_search(killer, 39, 0, killer.size());		
		cout << killer[killed] << endl;	
		killed = binary_search(killer, 43, 0, killer.size());		
		cout << killer[killed] << endl;	
		killed = binary_search(killer, 57, 0, killer.size());		
		cout << killer[killed] << endl;	
		killed = binary_search(killer, 60, 0, killer.size());		
		cout << killer[killed] << endl;	
		killed = binary_search(killer, 1, 0, killer.size());		
		cout << killer[killed] << endl;	
		killed = binary_search(killer, 55, 0, killer.size());
		if(killed!=killer.size())
			cout << killer[killed] << endl;	

		QuickSort(killer);
		InsertionSort(killer);
		SelectionSort(killer);
		BubbleSort(killer);
		BinaryInsertionSort(killer);

		for(u = 0; u< killer.size(); u++)
			cout << killer[u] << _T(" ");
		cout << endl;

		ifstream file("c:\\test.txt");

		if(file){
			istreambuf_iterator _F(file), _L;

			bidir_iterator<istreambuf_iterator > _FB(_F), _LB;
			/*
			{
				bidir_iterator<istreambuf_iterator > _FX(_F);
				cout << *_FX++ << endl;
				cout << *_FX++ << endl;
				cout << *_FX++ << endl;
				cout << *_FX++ << endl;

				cout << *_FX-- << endl;
				cout << *_FX-- << endl;
				cout << *_FX-- << endl;
				cout << *_FX-- << endl;
				cout << *_FX-- << endl;
			}
			*/

			while(_FB!=_L){
				cout <<  *_FB++ << _T("-");				
			}
			cout << endl;
			char chx = '';

			while(_FB!=_LB){					
				cout <<  *_FB-- << _T("-");
			}

			file.close();
		}

		//AVL
		AVLTree avl_dbls;

		avl_dbls.insert(1.23);

		//BST
		binary_search_tree bst_dbls;

		bst_dbls.insert(23.125);
		bst_dbls.insert(21.15);
		bst_dbls.insert(3.87);
		bst_dbls.insert(13.9);
		bst_dbls.insert(67.5);
		bst_dbls.insert(8.95);
		bst_dbls.insert(54.75);

		//

		matrix X(3, 3), R(3, 3);

		X[0][0] = 0.500;
		X[0][1] = 0.250;
		X[0][2] = 0.250;

		X[1][0] = 0.500;
		X[1][1] = 0.000;
		X[1][2] = 0.500;

		X[2][0] = 0.250;
		X[2][1] = 0.250;
		X[2][2] = 0.500;

		display_matrix(X, 1, 6);

		R = X;

		for(__uint exp=1;exp<=6;exp++)
		{
			//R = X ^ exp;
			cout << _T("P^(") << exp << _T(") = ") << endl;
			for(__uint ii=0;ii<R.rows_count(); ii++){
				for(__uint jj=0;jj<R.columns_count(); jj++)
					cout << _T("[ ") << ii << _T(" , ") << jj << _T(" ]= ") << R[ii][jj] << _T("\t");
				cout << endl;
			}
			R = R * X;
			cout << endl;
		}

		for(exp=1;exp<=6;exp++)
		{
			R = power(X, exp);
			for(__uint ii=0;ii<R.rows_count(); ii++){
				for(__uint jj=0;jj<R.columns_count(); jj++)
					cout << _T("[ ") << ii << _T(" , ") << jj << _T(" ]= ") << R[ii][jj] << _T("\t");
				cout << endl;
			}

			cout << endl;
		}

		R = X ^ 3;

		vector v(3); //Probability Vector...
		v[0] = v[1] = v[2] = .333;

		vector vr = v * R;
		vector::iterator itv = vr.begin();
		cout << _T("vector of initial state Si = ( ");
		while(itv != vr.end())
			cout << *itv++ << _T(", ");
		cout << _T(" ).") << endl;

		cout << _T("Exercise 1:") << endl;
		vector w(3);
		w[0] = .50; w[1] = w[2] = .25;

		for(exp=1;exp<=6;exp++){
			vector vwr = v * (X^exp);
			cout << _T("( ");
			for(__uint w=0;w<vwr.size();w++)
				cout << vwr[w] << _T(", ");
			cout << _T(" )") << endl;
		}

		cout << _T("Exercise 2:") << endl;

		matrix uspres(2, 2);
		uspres[0][0] = 1;
		uspres[0][1] = 0;

		uspres[1][0] = .50;
		uspres[1][1] = .50;
/*
		usres = uspres;

		for(exp=1;exp<=10;exp++){
			cout << _T("P^(") << exp << _T(") = ") << endl;
			for(__uint ii=0;ii<usres.rows_count(); ii++){
				for(__uint jj=0;jj<usres.columns_count(); jj++)
					cout << _T("[ ") << ii << _T(" , ") << jj << _T(" ]= ") << usres[ii][jj] << _T("\t");
				cout << endl;
			}
			usres = usres * uspres;
			cout << endl;
		}
*/
		display_matrix(uspres, 1, 10);

		matrix hm(3, 3);

		hm[0][0] = .500;
		hm[0][1] = .250;
		hm[0][2] = .250;

		hm[1][0] = .500;
		hm[1][1] = .250;
		hm[1][2] = .250;

		hm[2][0] = .500;
		hm[2][1] = .250;
		hm[2][2] = .250;

		display_matrix(hm, 1, 7);		

	//	NaryTree naryTree(3,_T('X'));

	//	depth_first(naryTree, TreeVisitor());

		open_scatter_table<__uint, bool> open_diccionary(131072); //2**17

		open_diccionary.insert(hash("scientist"), true);
		//...
		if(open_diccionary.find(hash("scientista"))==open_diccionary.end()){
				cout << _T("suggest spell check!...") << endl;
		}

		string numbers[] = {
			"un",
			"deux",
			"trois",
			"quatre",
			"cinq",
			"six",
			"sept",
			"huit",
			"neuf",
			"dix",
			"onze",
			"douze",
		};

		open_scatter_table<string, double> open_stex(51);

		for(k = 0; k< sizeof(numbers) / sizeof(numbers[0]); k++)
			open_stex.insert(numbers[k], 1.5 + k);

		open_scatter_table<string, double>::iterator it_num = open_stex.begin();
		while(it_num != open_stex.end()){
			cout << _T("key = ") << it_num->first << _T(", value = ") << it_num->second << endl << endl;
			it_num++;
		}

		open_scatter_table<string, double> open_sctable(19);

		open_sctable.insert("valor promedio", 19.872345);
		open_sctable.insert("moda", 14.872345);
		open_sctable.insert("mediana", 14.872345);
		open_sctable.insert("blim blim que no pasa el celdo!", 7.872345);

		open_scatter_table<string, double>::iterator ito = open_sctable.begin();

		while(ito != open_sctable.end())
		{
			cout << _T("key = ") << ito->first << _T(", value = ") << ito->second << endl << endl;
			ito++;
		}

		open_sctable.remove("blim blim que no pasa el celdo!");
		ito = open_sctable.begin();
		while(ito != open_sctable.end())
		{
			cout << _T("key = ") << ito->first << _T(", value = ") << ito->second << endl << endl;
			ito++;
		}

		chain_scatter_table scatter_table(20);

		scatter_table.insert(19.89);
		scatter_table.insert(60.10);
		scatter_table.insert(15.79);
		scatter_table.insert(34.20);
		scatter_table.insert(50.10);

		scatter_table.insert(9.89);
		scatter_table.insert(29.89);
		scatter_table.insert(49.89);
		scatter_table.insert(99.89);

		chain_scatter_table::iterator it_sc = scatter_table.begin();
		while(it_sc != scatter_table.end()){
			cout << _T("key = ") << *it_sc << endl;
			it_sc++;
		}

		scatter_table.remove(19.89);
		cout << _T("after removing 19.89 from scatter table") << endl;
		it_sc = scatter_table.begin();
		while(it_sc != scatter_table.end()){
			cout << _T("key = ") << *it_sc << endl;
			it_sc++;
		}

		chain_scatter_table scatter_int_table(20);

		scatter_int_table.insert(5);
		scatter_int_table.insert(10);
		scatter_int_table.insert(6);
		scatter_int_table.insert(7);
		scatter_int_table.insert(8);
		scatter_int_table.insert(1);
		scatter_int_table.insert(20);
		scatter_int_table.insert(2);
		scatter_int_table.insert(3);
		scatter_int_table.insert(4);
		scatter_int_table.insert(9);

		chain_scatter_table::iterator it_int = scatter_int_table.begin();
		while(it_int != scatter_int_table.end()){
			cout << _T("key = ") << *it_int << endl;
			it_int++;

		}

		hash_table<double, string> the_hash(20);

		the_hash.insert(2.4, "Harold");
		the_hash.insert(5.67, "Lawrence");
		the_hash.insert(8.9, "Marzan");
		the_hash.insert(12.15, "Mercado");
		/*
		the_hash.insert(19.89, "Rosa");
		the_hash.insert(60.10, "Denny");
		the_hash.insert(15.79, "Ramirez");
		the_hash.insert(34.20, "Silva");		
		*/
		the_hash[19.89] = "Rosa";
		the_hash[60.10] = "Denny";
		the_hash[15.79] = "Ramirez";
		the_hash[34.20] = "Silva";

		hash_table<double, string>::iterator ith = the_hash.begin();

		while(ith!=the_hash.end())
		{
			cout << _T("key = ") << ith->first << _T(" value = ") << ith->second << endl;
			ith++;
		}

		ith = the_hash.find(5.67);
		if(ith!=the_hash.end())
			cout << _T("key = ") << ith->first << _T(" value = ") << ith->second << endl;

		hash_table<string, int> the_str_hash(20);

		the_str_hash.insert("Uno",		1);
		the_str_hash.insert("Dos",		2);
		the_str_hash.insert("Tres",		3);
		the_str_hash.insert("Cuatro",	4);
		the_str_hash.insert("Cinco",	5);
		the_str_hash.insert("Seis",		6);
		the_str_hash.insert("Siete",	7);
		the_str_hash.insert("Ocho",		8);
		the_str_hash.insert("Nueve",	9);
		the_str_hash.insert("Diez",		10);

		hash_table<string, int>::iterator it_str = the_str_hash.begin();
		while(it_str != the_str_hash.end()){
			cout << _T("key = ") << it_str->first << _T(" value = ") << it_str->second << endl;
			++it_str;
		}

		string n_strings[] = {
			"Harold Lawrence",
			"Harold Lawrence Marzan Mercado",
			"Harold Lawrence Marzan", 
			"Harold Hans",
			"Harold Hans Marzan Ramirez",
			"Harold Hans Marzan",
			"Harold",
			"Harold M.",
			"HAROLD",
			"HAROLD M.",
			"ett",
			"tva",
			"tre",
			"fyra",
			"fem",
			"sex",
			"sju",
			"atta",
			"nio",
			"tio",
			"elva",
			"tolv",
		};

		for(__uint lx=0; lx< sizeof(n_strings)/sizeof(n_strings[0]); lx++)
			cout << _T("text = ") << n_strings[lx] << _T(" hash = ") << 
						hash_string(n_strings[lx].begin(), n_strings[lx].end(), 6) << endl << endl;

		hash_table<string, int> hash_str2((sizeof(n_strings)/sizeof(n_strings[0])) * 4);
		for(lx=0; lx< sizeof(n_strings)/sizeof(n_strings[0]); lx++)
			hash_str2[n_strings[lx]] = lx;

		hash_table<string, int>::iterator it_str2 = hash_str2.begin();
		while(it_str2 != hash_str2.end()){
			cout << _T("key = ") << it_str2->first << _T(" value = ") << it_str2->second << endl;
			++it_str2;
		}

                        //R, C
		matrix mA(3, 2), //A = m * n;
			           mB(2, 3), //B = n * p;
					   mC(3, 3); //C = AB = m * p;

		//fila1
		mA[0][0] = 2.0;
		mA[0][1] = 3.0;
		//fila2
		mA[1][0] = 4.0;
		mA[1][1] = 2.0;
		//fila3
		mA[2][0] = 2.0;
		mA[2][1] = 6.0;

		//fila1
		mB[0][0] = 4.0;
		mB[0][1] = 3.0;
		mB[0][2] = 5.0;

		//fila2
		mB[1][0] = 6.0;
		mB[1][1] = 9.0;
		mB[1][2] = 8.0;

		mC = (mA * mB);

		cout << _T("result matrix C:") << endl;
		for(matrix::uint_type i=0;i<mC.rows_count();i++)
		{			
			for(matrix::uint_type j=0;j<mC.columns_count();j++)
			{
				cout << _T("C[") << i << _T(" , ") << j << _T("] = ") << mC[i][j] << _T("  ");
			}
			cout << endl;
		}

		TreeNode* root = new TreeNode('A');

		TreeNode* node = new TreeNode('B'),
					  *xnode,
					  *ynode;
		root->addNode(node);
		xnode = node;		

		node = new TreeNode('E');
		xnode->addNode(node);
		node = new TreeNode('F');
		xnode->addNode(node);
		ynode = node;
		node = new TreeNode('C');
		root->addNode(node);

		node = new TreeNode('J');
		ynode->addNode(node);

		node = new TreeNode('K');
		ynode->addNode(node);

		node = new TreeNode('D');
		root->addNode(node);
		xnode = node;
		node = new TreeNode('G');
		xnode->addNode(node);
		node = new TreeNode('H');
		xnode->addNode(node);
		ynode = node;
		node = new TreeNode('I');
		xnode->addNode(node);

		node = new TreeNode('L');
		ynode->addNode(node);

		BreadthFirstTraversal(root);

		cout << endl << endl;
		BreadthFirstTraversal2(root);

		TreeNode::removeNodes(root);

		cout << _T("testing marzan stl list") << endl << endl;
		//
		marzan::list listax;

		listax.push_back(15.99);
		listax.push_back(19.99);
		listax.push_front(10.45);
		listax.push_front(3.69);
		listax.push_back(33.78);
		listax.push_back(.7812);

		marzan::list::iterator itf = listax.begin();

		itf = listax.insert(itf, 1.90);

		while(itf != listax.end()){
			 cout << *itf++ << endl;
		}
		cout << endl;
		listax.insert(listax.begin(), 0.666);
		itf = listax.begin();
		while(itf != listax.end()){
			 cout << *itf++ << endl;
		}		
		listax.erase(33.78);
		cout << endl;
		itf = listax.begin();
		while(itf != listax.end()){
			 cout << *itf++ << endl;
		}

		itf = listax.begin();

		itf++;
		itf++;
		listax.erase(itf);
		cout << endl;
		itf = listax.begin();
		while(itf != listax.end()){
			 cout << *itf++ << endl;
		}

		//

		int array[] = { 1, 2 , 3, 4, 5, 6, 7, 8, 9, 10, };

		reverse_ex(array, array + sizeof(array)/sizeof(array[0]));

		copy(array, array + sizeof(array)/sizeof(array[0]), ostream_iterator<int, TCHAR>(cout, _T(" ")));

		cout << endl;
		for(unsigned int ex=0;ex<=32;++ex){
			double dpw = power2(2.0, ex);
			cout << _T("for [ ") << 2.0 << _T(" ^ ") << ex << _T("] = ") << dpw << endl;
		}

		double coef_x[] = { 3.14, 4.66, 12.71, 21.49, 12.45, 57.90, };
		double summ = geometric_summ_horner(coef_x, 2.0, sizeof(coef_x)/sizeof(coef_x[0]));
		cout << summ << endl;
		summ = geometric_summ_horner(coef_x, 3, sizeof(coef_x)/sizeof(coef_x[0]));
		cout << summ << endl;

		summ = geometric_summ_q(2.0, 31);
		cout << summ << endl;

/*
		FlightDatabase fdb;
		fdb.Add(_T("New York"), _T("Chicago"), 900);
		fdb.Add(_T("New York"), _T("Toronto"), 500);
		fdb.Add(_T("Toronto"), _T("Los Angeles"), 2500);
		fdb.Add(_T("New York"), _T("Denver"), 1800);
		fdb.Add(_T("Toronto"), _T("Chicago"), 500);
		fdb.Add(_T("Toronto"), _T("Calgary"), 1700);
		fdb.Add(_T("Denver"), _T("Los Angeles"), 1000);
		fdb.Add(_T("Chicago"), _T("Denver"), 1000);
		fdb.Add(_T("Denver"), _T("Houston"), 1000);
		fdb.Add(_T("Denver"), _T("Urbana"), 1000);
		fdb.Add(_T("Houston"), _T("Los Angeles"), 1500);

		fdb.ShowList();

		TCHAR szFrom[40],
			 szTo[40];
		cout << _T("from?:");
		cin.getline(szFrom, 40);

		cout << _T("to?:");
		cin.getline(szTo, 40);

		SearchEngine se(fdb);
*/

		//LPFLIGHT_INFO pfi = NULL;
		//se.FindRoute(szFrom, szTo, &pfi);
		//if(pfi){
		//	se.ShowCompleteRoute(pfi);
		//}
		//delete pfi;

		//se.FindRoutes(szFrom, szTo);

		///
		//-2 -1 0 1 2 3
#define LOWER_BOUND -5
#define UPPER_BOUND  5

		range_array ra(LOWER_BOUND, UPPER_BOUND, 100); 

		cout << _T("ra.size()==") << ra.size() << endl;
		cout << endl;
		range_array::iterator it = ra.begin();
		while(it!=ra.end()){
			cout << *it++ << endl;
		}
		cout << endl;
		it = ra.begin();

		*it++ = 99;
		*it++ = 77;
		it++;
		*it++ = 33;
		*it++ = 11;

		it = ra.begin();
		while(it!=ra.end()){
			cout << *it++ << endl;
		}
		cout << endl;

		for(int bound=LOWER_BOUND;bound<=UPPER_BOUND;bound++)
			cout << ra.at(bound) << endl;

		cout << endl;
		range_array ra_copy = ra;

		cout << _T("ra_copy.size()==") << ra_copy.size() << endl;

		cout << endl;
		it = ra_copy.begin();
		while(it!=ra_copy.end()){
			cout << *it++ << endl;
		}

		cout << endl;
		for(bound=LOWER_BOUND;bound<=UPPER_BOUND;bound++)
			cout << ra_copy.at(bound) << endl;

		cout << _T("ctor(num, val init)") << endl;
		range_array ra_init(10, 1234);
		for(range_array::iterator iti = ra_init.begin(); iti!=ra_init.end(); iti++)
			cout << *iti << endl;

		iti = ra_init.begin();

		iti++;
		*iti = 1999;
		iti = ra_init.insert(iti, 1920);
		iti++; //1920
		iti++; //1999
		iti++; //1234
		iti++; //1234
		*iti++ = 2001;
		*iti++ = 2002;
		*iti++ = 2003;
		iti++; //1234
		*iti = 2004;
		iti = ra_init.insert(iti, 2666); //2666
		iti++; //to 2004
		iti++; //to 1234 who'll change to 2005
		*iti = 2005;

		cout << endl;
		cout << endl;
		for(iti = ra_init.begin(); iti!=ra_init.end(); iti++)
			cout << *iti << endl;

		cout << _T("ra_init.swap(ra)") << endl;
		ra_init.swap(ra);

		cout << _T("ra contents") << endl;
		it = ra.begin();
		while(it!=ra.end()){
			cout << *it++ << endl;
		}

		cout << _T("ra_init contents") << endl;
		for(iti = ra_init.begin(); iti!=ra_init.end(); iti++)
			cout << *iti << endl;

		cout << endl;
		cout << endl;		
		it = ra.begin();
		ra.erase(it);
		cout << _T("after deletion") << endl;		
		it = ra.begin();
		while(it!=ra.end()){
			cout << *it++ << endl;
		}

		cout << endl;
		cout << endl;		

		ra_init.erase(ra_init.begin()+5, ra_init.end());

		cout << _T("after deleting range in ra_init") << endl;
		for(iti = ra_init.begin(); iti!=ra_init.end(); iti++)
			cout << *iti << endl;

		range_array ra_range(ra_init.begin(), ra_init.end());
		cout << _T("after range init(begin(), end())") << endl;
		cout << endl;		
		range_array::iterator ir = ra_range.begin();
		while(ir!=ra_range.end()){
			cout << *ir++ << endl;
		}		

		range_array rd;

		rd.push_back(15.99);
		rd.push_back(80.25);
		rd.push_back(9.40);
		rd.push_back(100.50);
		rd.push_back(77.40);

		rd.push_front(5000);
		rd.push_back(10000);
		rd.push_front(3000);

		cout << endl;
		cout << rd.front() << endl;
		cout << _T("back()") << endl;
		cout << rd.back() << endl;
		cout << endl;
		cout << rd.at(4) << endl;
		cout << rd.at(5) << endl;
		cout << endl;
		cout << endl;		

		cout << rd.at(-2) << endl;
		cout << rd.at(-1) << endl;
		cout << rd.at(0) << endl;

		cout << endl;
		cout << endl;		
		range_array::iterator itrd = rd.begin();
		while(itrd != rd.end())
			cout << *itrd++ << endl;

		rd.pop_back();
		cout << _T("after pop_back rd, size()==") << rd.size() << endl;		
		itrd = rd.begin();
		while(itrd != rd.end())
			cout << *itrd++ << endl;

		rd.pop_front();
		cout << _T("after pop_front rd, size()==") << rd.size() << endl;		
		itrd = rd.begin();
		while(itrd != rd.end())
			cout << *itrd++ << endl;

		for(int x=1; x<= 100; x++)
			rd.push_back(3.141516927 * x);

		cout << endl;
		cout << endl;
		cout << _T("iterating using bounds with size()==") << rd.size() << endl;		
		for(int z=rd.lbound();z<=rd.ubound();z++)		
			cout << _T("pos = [") << z << _T("] = ") << rd.at(z) << endl;

		range_array rd_copia;

		copy(rd.begin(), rd.end(), back_inserter(rd_copia));
		cout << _T("rd_copia has size()== ") << rd_copia.size() 
			 << _T(" capacity== ") << rd_copia.capacity() << endl;
		itrd = rd_copia.begin();
		while(itrd != rd_copia.end())
			cout << *itrd++ << endl;

		cout << _T("exact copy? = ") << (int)(rd==rd_copia) << endl;

		cout << _T("range(begin,end) only 40 elements from ") << rd.size() << _T(" existents") << endl;
		range_array rd_racopy(rd.begin()+41, rd.end()-25);

		cout << _T("rd_racopy < rd ?") << (int)(rd_racopy < rd) << endl;

		cout << _T("rd_racopy has size()== ") << rd_racopy.size() 
			 << _T(" capacity== ") << rd_racopy.capacity() << endl;
		itrd = rd_racopy.begin();
		while(itrd != rd_racopy.end())
			cout << *itrd++ << endl;

		//copy all elements from rd, them apply conditions
		cout << endl;
		cout << endl;
		range_array ra_rles60;
		//copy, and replace if values are less than 60, set then to 99.99
		replace_copy_if(rd.begin(), rd.end(), inserter(ra_rles60, ra_rles60.begin()), 
						bind2nd(less(), 60.0), 99.99);

		//copy from the itself, and if values greater than 200, set them to 199.99
		replace_copy_if(ra_rles60.begin(), ra_rles60.end(), ra_rles60.begin(), 
						bind2nd(greater(), 200.0), 199.99);

		cout << _T("descending...") << endl;
		sort(ra_rles60.begin(), ra_rles60.end(), greater() );
		itrd = ra_rles60.begin();
		while(itrd != ra_rles60.end())
			cout << *itrd++ << endl;

		cout << _T("ascending...") << endl;
		sort(ra_rles60.begin(), ra_rles60.end(), less() );
		itrd = ra_rles60.begin();
		while(itrd != ra_rles60.end())
			cout << *itrd++ << endl;

		cout << _T("copy_ex") << endl;

		copy_ex(rd.begin(), rd.end(), ostream_iterator<double, TCHAR>(cout, _T("\n")));

		cout << _T("end copy_ex") << endl;

		cout << _T("copy_if") << endl;

		range_array rdx;

		copy_if(rd.begin(), rd.end(), back_inserter(rdx), bind2nd(less(), 101.00));

		copy_ex(rdx.begin(), rdx.end(), ostream_iterator<double, TCHAR>(cout, _T("\n")));

		cout << _T("end copy_if") << endl;

		rd.clear();
		cout << _T("after deleting rd, size()==") << rd.size() << endl;		

		while(!rd.empty()){
			itrd = rd.begin();
			cout << _T("deleting value: ") << *itrd << endl;
			rd.erase(rd.begin());
		}		
	}

	return nRetCode;		
}

Posted February 2, 2013 by hmarzan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: