I was writing a function for a little programming challenge I thought
would be fun. When I got all done, I thought, wow, that is a funky
function. With a few variable names changed to hide the meaning of
what the function is doing, I thought it would be fun to see if anyone
could make a correct guess as to the ultimate goal of this function:
unsigned short result [dim][dim];
void foo(const string& info1, const string& info2, string& ret_val) {
for (int x=0;x<dim;++x) {
for (int y=0;y<dim;++y) {
result[x][y] = (info1.at(x)-'0') * (info2.at(y)-'0');
}
}
ret_val.clear();
// and now, the evil
unsigned short carry=0;
unsigned short sum=0;
int xPos,yPos;
bool use_high;
for (int x=dim*2-1;x>=0;--x) {
use_high=x<dim;
xPos= x<dim ? 0 : x-dim;
yPos= x<dim ? x : dim-1;
sum=0;
do {
sum +=
use_high ?
result[xPos][yPos] / 10 :
result[xPos][yPos] % 10 ;
if (use_high) {
--yPos;
} else {
++xPos;
}
use_high=!use_high;
} while ( xPos<dim&&yPos>=0);
sum += carry;
carry = sum / 10;
ret_val.insert( ret_val.begin(), (char)(sum % 10 + '0') );
}
}
If you like, you can see the function w/ syntax highlighting at my
blog http://allmybrain.com
-Dennis
/*
PLUG: http://plug.org, #utah on irc.freenode.net
Unsubscribe: http://plug.org/mailman/options/plug
Don't fear the penguin.
*/