#include "Python.h" #include #include #include int fac(int n) { if(n < 0) { return 0; } if(n < 2) { return 1; } return n * fac(n - 1); } char * reverse(char * src) { char tmp = 0; char * head = NULL; char * tail = NULL; if(src == NULL) { return NULL; } if(src[0] == '\0') { return src; } head = src; tail = src + strlen(src) - 1; while(head < tail) { tmp = *head; *head = *tail; *tail = tmp; head ++; tail --; } return src; } int main() { printf("4! == %d\n",fac(4)); printf("8! == %d\n",fac(8)); printf("12! == %d\n",fac(12)); char src[BUFSIZ]; memset(src,0x00,sizeof(src)); strcpy(src,"abcdef"); printf("reversing '%s' ",src); printf("we get '%s'\n",reverse(src)); memset(src,0x00,sizeof(src)); strcpy(src,"madam"); printf("reversing '%s' ",src); printf("we get '%s'\n",reverse(src)); return 0; } PyObject * Extest_fac(PyObject * self,PyObject * args) { int res = 0; int num = 0; PyObject * retval = NULL; res = PyArg_ParseTuple(args,"i",&num); if(!res) { return NULL; } res = fac(num); retval = (PyObject *)Py_BuildValue("i",res); return retval; } PyObject * Extest_doppel(PyObject * self,PyObject * args) { PyObject * retval = NULL; char * orig_str = NULL; char * dupe_str = NULL; int res = 0; res = PyArg_ParseTuple(args,"s",&orig_str); if(!res) { return NULL; } if(orig_str == NULL) { return NULL; } dupe_str = strdup(orig_str); dupe_str = reverse(dupe_str); retval = (PyObject *)Py_BuildValue("ss",orig_str,dupe_str); free(dupe_str); return retval; } static PyMethodDef ExtestMethods[] = { {"fac",Extest_fac,METH_VARARGS}, {"doppel",Extest_doppel,METH_VARARGS}, {NULL,NULL}, }; void initExtest() { Py_InitModule("Extest",ExtestMethods); }