Function Gen

So I wanted to make a little function to write my username... sooo I wrote a function to write a function to write my usernmame.

#include <string> /*&&*/ #include <sstream>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
string get_function_from_name(string name){
    ostringstream oss;
    oss << name.size();
    string ret = "";
    ret += "string get_me(){\n  int temp[";
    ret += oss.str();
    ret += "]={";
    
    int cur = 0;
    
    for (int temp = 0; temp < name.size(); temp++){
        ostringstream out("");
        out << -(cur - (int)name[temp]);
        cur = (int)name[temp]; 
        ret += out.str();
        if (temp != name.size() - 1)
        ret += ",";
    }
    
    ret += "};\n  string me = \"\";\n  int to = 0;\n  for (int c = 0; c < ";
    ret += oss.str();
    ret += "; c++){\n    to +=temp[c];\n    me+=(char)to;\n   }\n   return me;\n}";
    
    return ret;
}


It's a weak little function, and I'm wondering how many different username generating function function generators we can come up with!


1
2
3
4
string get_function_from_name(string name)
{
    return "string get_me()\n{\n\treturn \""+name+"\";\n}";
}
:^P
@Bazzy: inventive.... =P
Here's mine :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>

std::string program = std::string(
	"#include <cctype>\n"
	"#include <iostream>\n"
	"#include <string>\n\n"
	"namespace crypto {\n"
	"\tstd::string rot13(std::string str)\n\n"
	"\t{\n\n"
	"\t\tfor (size_t i = 0; i < str.size(); ++i) {\n\n"
	"\t\t\tif (isalpha(str[i])) {\n\n"
	"\t\t\t\tif ((str[i] < ('A' + 13)) || (str[i] < ('a' + 13)))\n\n"
	"\t\t\t\t\tstr[i] += 13;\n\n"
	"\t\t\t\telse\n\n"
	"\t\t\t\t\tstr[i] -= 13;\n\n"
	"\t\t\t}\n\n"
	"\t\t}\n\n"
	"\t\treturn str;\n"
	"\t}\n"
	"}\n\n"
	"int main(int argc, char** argv)\n"
	"{\n"
	"\tif (argc < 2) {\n"
	"\t\tstd::cerr << \"Usage: \" << argv[0] << \" <string>\" << std::endl;\n"
	"\t\treturn 1;\n"
	"\t}\n"
	"\tstd::cout << crypto::rot13(argv[1]) << std::endl;\n"
	"\treturn 0;\n"
	"}"
);

int main()
{
	std::ofstream out("pointless.1.cpp");
	out << program << std::endl;
	out.close();

#ifdef _WIN32
	system("g++ -o pointless.1.exe pointless.1.cpp");
	system("pointless.1.exe puevfanzr");
#else
	system("g++ -o pointless.1 pointless.1.cpp");
	system("./pointless.1 puevfanzr");
#endif

	return 0;
}


Edit: Excuse both my use of system and my assumption that you have g++ on your system and in your path.
Last edited on
More inventive code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
string get_function_from_name ( const string &name )
{
    ostringstream  ss;
    int rn = rand();
    srand(rn);
    ss << "string get_me()\n{\n\tsrand(" << rn << ");\n\tint t[]={";
    for ( int i = 0; i < name.size(); i++ )
    {
        int n = rand()%123+1;
        ss << (name[i]/n^rand()) << ',';
        ss << name[i]%n+rand() << ',';
    }
    ss <<   "};\n\tstring r;"
            "\n\tfor ( int i = 0; i < sizeof(t)/sizeof(int); i += 2 )\n\t{"
            "\n\t\tr += (rand()%123+1);"
            "\n\t\tr[i/2] *= t[i]^rand();"
            "\n\t\tr[i/2] += t[i+1]-rand();"
            "\n\t}\n\treturn r;\n}";
    return ss.str();
}
TABS?!?!?!!?

Heathens!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::string bf_difference( int k ){
	int j = k < 0 ? -k : k, a;
	for( int i = 1; i*i <= j; i++ ) if( j%i == 0 ) a = i;
	
	if( a+j/a+5 >= j ) return std::string( j, k < 0 ? '-' : '+' );
	else return ">" + bf_difference(a) + "[-<" + std::string( j/a, k < 0 ? '-' : '+' ) + ">]<";
}

std::string get_function_from_name( std::string name ){
	std::string result;
	char prev = 0;
	for(unsigned i = 0; i < name.length(); prev = name[i], i++)
		result += bf_difference( name[i]-prev ) + ".";
	return result;
}

No one said it had to be C++, right? It greatly lacks sophistication, but at least it tries to use loops.. It appears to me that an algorithm to find the shortest expression would be rather complicated.
1
2
3
4
5
6
#include <iostream>
#define NAME(name) (#name)
int main() {
    std::cout << NAME(me) << std::endl;
    return 0;
}



me
Last edited on
A WITCH! A WITCH! Burn him alive!!!

@ moorecm: Seriously though I've never seen preprocessor commands used to link to the output consule before. Please explain how this works.

EDIT: Now that I've re-read what #define does I can see what he's actually doing here. Kind of takes the magic out of it if you ask me :,(
Last edited on
Nothing is as interesting when you know how it works :(
Topic archived. No new replies allowed.