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
|
import os
import sys
import random
cpp_head = """#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
"""
cpp_tail = """ return 0;
}
"""
UINT64_MAX = (2 ** 64) - 1
random.seed()
while True:
# Get user input
line = raw_input(">>> ")
# Create source file
cpppath = str(random.randint(0, UINT64_MAX)) + ".cpp"
cppfile = open(cpppath, 'w')
cppfile.write(cpp_head + line + cpp_tail)
cppfile.close()
# Create executable file
exepath = str(random.randint(0, UINT64_MAX))
if os.name in [ "nt", "os2", "ce" ]:
exepath += ".exe"
os.system("g++ -o " + exepath + " " + cpppath)
# Delete source file
os.unlink(cpppath)
# Run executable file
if os.name in [ "nt", "os2", "ce" ]:
os.system(exepath)
else:
os.system("./" + exepath)
# Delete executable file
os.unlink(exepath)
|
X:\Projects\Scripts>python "g++i.py"
>>> std::cout << "hello, world" << std::endl;
hello, world |