Dev-C++ and Python

vegaseat 1 Tallied Votes 4K Views Share

Python has some powerful functions, like the math expression evaluator eval(). You can embed this function and others in your Development C++ code. All you need to do, is to download and install the free Python Development Pak and feed the Python code in the form of a string to the interpreter. Your executable file will work as long as there is access to the Python24.dll (part of the pak). Looks to me like you could use a Python program to create this C++ program embedding a Python code file. A Python to C++ translator is born!

// needs the Python-2.4.1.DevPak installed into Dev-C++, download from:
// http://prdownloads.sourceforge.net/devpaks/Python-2.4.1.DevPak?download
// or
// http://www.robloach.net/e107_plugins/content/content.php?type.12.content.20
// create project with File > New > Project... > Scripting > Python
// tested with Dev-C++     vegaseat    25sep2005


#include <iostream>
#include <string>

// run the Python interpreter included in eg. Python24.dll
// this macro saves on typing!
#define PY  PyRun_SimpleString(input.c_str())

using namespace std;

// include the Python library header
extern "C" {
	#include <python2.4/Python.h>
}


int main()
{

	// initialize Python
	Py_Initialize();

	// optional - display Python version information
	cout << "Python " << Py_GetVersion() << endl << endl << endl;

  string input;

  // use the Python eval() function to evaluate a math expression
  input = "str1 = '3 * 4 + 2'"; PY;  
  input = "print 'eval(%s) = %s' % (str1, eval(str1))"; PY;
  // 50 dashes for the fun of it
  input = "print '-' * 50"; PY;
  // more eval() stuff
  input = "str2 = '355/113.0  * 4 + 7'"; PY;  
  input = "print 'round(eval(%s), 2) = %s' % (str2, round(eval(str2), 2))"; PY;

	// finish up and close Python
	Py_Finalize();
	
	cin.get();  // console wait
	return 0;
}
Shantha.D 0 Newbie Poster

i followed url that you hav provided... but got one jsp file... how to instaal that(Phython) ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.