Friday, July 31, 2009

Template Hell

Man, When I first saw pointers, referencing and de-referencing and all, I thought that I had seen the worst that C++ can throw at you. And now come and greet the new enemy that I found

TEMPLATES!!!

So you think you know what templates do ? Lets see, they are a type of metaprogramming right ? They allow you to use C++ compiler as an interpreter and write generic code with the use of which C++ compiler would generate specific code. So that is what templates does ? Nope. Sorry but I thought so too. But along comes this code snippet which supposedly implements a while loop. Go figure! And when you do please tell me too :D

template
class loop {
private: enum { go = (I-1) != 0 };
public: static inline void f() {
// Whatever needs to go here
loop::f(); }
};

class loop<0> {
public:
static inline void f()
{ }
};

loop::f();

Sunday, July 26, 2009

Jamming On

Well this time around I guess I might have some time to go after the code jam contest. Took some small steps in that way and here is the first result. Think I would use C++ for the contest. I would love to experiment with Python and use some C# or Java for this. I would love the java libraries in the more complex problems. But seems I can make do with C++ this while and this would be a good time as any to get those rusty C++ skills honed in. So here goes nothing. :D

This is my solution for the first practise problem. This is really cool, at least for me and I hope is good and correct C++. No C stdio, no c-strings and bettter still no mallocs. Take a look and see what comes.

/*
* main.cpp
*
* Created on: 27 Jul 2009
* Author: Osada
*/

#include
#include
#include
#include "math.h"

using namespace std;

void process(fstream *input_file, fstream *output_file, int line_no)
{
string num_str;
string source_lang;
string target_lang;
string convert_num_str;
char input_char = '\0';

(*input_file) >> num_str >> source_lang >> target_lang;

cout << "The number string is " <<>
cout <<>
cout <<>

/* Data processing variables set */
long target_lang_dim = target_lang.size();
long source_lang_dim = source_lang.size();
long source_lang_value = 0;
long pos_value = 0;
long convert_residue = 0;

std::reverse(num_str.begin(), num_str.end());
for(int i =0; i <>
{
pos_value = source_lang.find(num_str.at(i), 0);
source_lang_value += pos_value * powl(source_lang_dim, i);
}

convert_residue = source_lang_value;
ldiv_t div_result;
while(convert_residue != 0)
{
div_result = div(convert_residue, target_lang_dim);
convert_residue = div_result.quot;
convert_num_str.push_back(target_lang.at(div_result.rem));
}
std::reverse(convert_num_str.begin(), convert_num_str.end());
(*output_file) << "Case #" <<>
}

int main (int argc, char **argv)
{
/* File input variables */
fstream input_file("A-large-practice.in", fstream::in);
fstream output_file("output.txt", fstream::out);
int no_of_lines = 0;
input_file >> no_of_lines;
for(int i = 0; i
{
process(&input_file, &output_file, (i + 1));
}
input_file.close();
output_file.close();
}