用python 在c++计算数学表达式的例子

来源:互联网 发布:c语言程序流程图 编辑:程序博客网 时间:2024/05/22 22:06

 http://blog.csdn.net/hardVB/

#include "stdafx.h"
#include 
"Python.h"
#include 
<iostream>

#pragma comment( lib, "python22.lib" )    

int _tmain(int argc, _TCHAR* argv[])
{
    Py_Initialize();
    PyObject 
* module;
    PyObject 
* dict;
    PyObject 
* obj;
    
long lval;

    PyRun_SimpleString(
"obj = (3 * 5)+8");
    module 
= PyImport_AddModule("__main__");
    dict   
= PyModule_GetDict(module);
    obj    
=  PyMapping_GetItemString(dict, "obj");
    
if (obj != NULL)
    
{
        lval 
= PyInt_AsLong(obj);
        printf(
"obj: %d ", lval);
    }

    
else
    
{
        printf(
"Object not found ");
    }

    
return 0;
}

原创粉丝点击