现在的位置: 首页 > 综合 > 正文

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

2013年07月01日 ⁄ 综合 ⁄ 共 579字 ⁄ 字号 评论关闭
 

#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;
}

 

抱歉!评论已关闭.