求输出hello kitty壁纸大图案的c++ 代码

求C++题或代码
本回答由提问者推荐
var sogou_ad_id=731547;
var sogou_ad_height=160;
var sogou_ad_width=690;C/C++与Python互相调用
#include &.h&&&
void HelloWorld(){&
&&&& Py_Initialize();&
&&&&&&& PyRun_SimpleString(&import sys&);&&
&&&&&&& PyRun_SimpleString(&print 'hi,python!'&);&
&&&&&&& PyRun_SimpleString(&sys.path.append('./')&);&
&&&& PyObject * pModule = NULL;&
&&&& PyObject * pFunc = NULL;&
&&&& printf(&11111 %x\n&,pModule);&
&&&& pModule =PyImport_ImportModule(&mypy&);&
&&&&&&& printf(&11111 %x\n&,pModule);&
&&&& pFunc= PyObject_GetAttrString(pModule, &HelloWorld&);&
&&&& PyEval_CallObject(pFunc, NULL);&
&&&& Py_Finalize();&
void main(){&
&&&&&&& printf(&tttttt\n&);&
&&&&&&& HelloWorld();&
#include &Python.h&
void HelloWorld(){
&&&& Py_Initialize();
&&&&&&& PyRun_SimpleString(&import sys&);
&&&&&&& PyRun_SimpleString(&print 'hi,python!'&);
&&&&&&& PyRun_SimpleString(&sys.path.append('./')&);
&&&& PyObject * pModule = NULL;
&&&& PyObject * pFunc = NULL;
&&&& printf(&11111 %x\n&,pModule);
&&&& pModule =PyImport_ImportModule(&mypy&);
&&&&&&& printf(&11111 %x\n&,pModule);
&&&& pFunc= PyObject_GetAttrString(pModule, &HelloWorld&);
&&&& PyEval_CallObject(pFunc, NULL);
&&&& Py_Finalize();
void main(){
&&&&&&& printf(&tttttt\n&);
&&&&&&& HelloWorld();
#!/usr/bin/env python&&
print '2222'&
def HelloWorld():&
& print 'this is Helloworld'&
#!/usr/bin/env python
print '2222'
def HelloWorld():
& print 'this is Helloworld'
编译: mips-linux-gnu-gcc -EL test.c -L./lib -lpython2.7 -I./include/python2.7/
#include &iostream&&&&&
#include &Python.h&&&
void HelloWorld();&&&
void Add();&&&
void TestTransferDict();&&&
void TestClass();&&&
int main()&&&
&&&& cout && &Starting Test...& &&&&&
&&&& cout && &HelloWorld()-------------& &&&&&
&&&& HelloWorld();&&&
&&&& cout && &Add()--------------------& &&&&&
&&&& Add();&&&
&&&& cout && &TestDict-----------------& &&&&&
&&&& TestTransferDict();&&&
&&&& cout && &TestClass----------------& &&&&&
&&&& TestClass();&&&
//&&&& system(&pause&);&&&&
&&&& return 0;&&&
void HelloWorld()&&&
&&&& Py_Initialize();&
&&&&&&& PyRun_SimpleString(&import sys&);&&
&&&&&&& PyRun_SimpleString(&print 'hi,python!'&);&
&&&&&&& PyRun_SimpleString(&sys.path.append('./')&);&
&&&& PyObject * pModule = NULL;&
&&&& PyObject * pFunc = NULL;&
&&&& pModule =PyImport_ImportModule(&Test001&);&
&&&& pFunc= PyObject_GetAttrString(pModule, &HelloWorld&);&
&&&& PyEval_CallObject(pFunc, NULL);&
&&&& Py_Finalize();&
void Add()&&&
&&&& Py_Initialize();&&&
&&&&&&& PyRun_SimpleString(&import sys&);&&
&&&&&&& PyRun_SimpleString(&sys.path.append('./')&);&&&&
&&&& PyObject * pModule = NULL;&&&&&&&
&&&& PyObject * pFunc = NULL;&&&&&&&&&
&&&& pModule =PyImport_ImportModule(&Test001&);&
&&&& pFunc= PyObject_GetAttrString(pModule,&add&);&
&&&& PyObject *pArgs = PyTuple_New(2);&
&&&& PyTuple_SetItem(pArgs, 0, Py_BuildValue(&i&, 5));&
&&&& PyTuple_SetItem(pArgs, 1, Py_BuildValue(&i&, 7));&
&&&& PyObject *pReturn = NULL;&&&
&&&& pReturn = PyEval_CallObject(pFunc, pArgs);&
&&&& PyArg_Parse(pReturn, &i&, &result);&
&&&& cout && &5+7 = & && result &&&&&
&&&& Py_Finalize();&&&&&&&&&&&&&&&&&&&
void TestTransferDict()&&&
&&&& Py_Initialize();&&&
&&&&&&& PyRun_SimpleString(&import sys&);&
&&&&&&& PyRun_SimpleString(&sys.path.append('./')&);&
&&&& PyObject * pModule = NULL;&&&&&&&
&&&& PyObject * pFunc = NULL;&&&&&&&&&
&&&& pModule =PyImport_ImportModule(&Test001&);&
&&&& pFunc= PyObject_GetAttrString(pModule, &TestDict&);&
&&&& PyObject *pArgs = PyTuple_New(1);&&&&
&&&& PyObject *pDict = PyDict_New();&
&&&& PyDict_SetItemString(pDict, &Name&, Py_BuildValue(&s&, &WangYao&));&
&&&& PyDict_SetItemString(pDict, &Age&, Py_BuildValue(&i&, 25));&
&&&& PyTuple_SetItem(pArgs, 0, pDict);&
&&&& PyObject *pReturn = NULL;&&&
&&&& pReturn = PyEval_CallObject(pFunc, pArgs);&
&&&& int size = PyDict_Size(pReturn);&&&
&&&& cout &&&TTTTTTTT& && size &&&&&
&&&& PyObject *pNewAge = PyDict_GetItemString(pReturn, &Age&);&&&
&&&& int newA&&&
&&&& PyArg_Parse(pNewAge, &i&, &newAge);&&&
&&&& cout && &True Age: & && newAge &&&&&
&&&& Py_Finalize();&&&&&&&&&&&&&&&&&&&
void TestClass()&&&
&&&& Py_Initialize();&&&
&&&&&&& PyRun_SimpleString(&import sys&);&
&&&&&&& PyRun_SimpleString(&sys.path.append('./')&);&
&&&& PyObject * pModule = NULL;&&&&&&&
&&&& PyObject * pFunc = NULL;&&&&&&&&&
&&&& pModule =PyImport_ImportModule(&Test001&);&
&&&& pFunc= PyObject_GetAttrString(pModule, &TestDict&);&&
&&&& PyObject *pClassPerson = PyObject_GetAttrString(pModule, &Person&);&&&
&&&& PyObject *pInstancePerson = PyInstance_New(pClassPerson, NULL, NULL);&&&
&&&& PyObject_CallMethod(pInstancePerson, &greet&, &s&, &Hello Kitty&);&
&&&& Py_Finalize();&&&&&&&&&&&&&
#include &iostream&&
#include &Python.h&
void HelloWorld();&
void Add();&
void TestTransferDict();&
void TestClass();&
int main()&
&&&& cout && &Starting Test...& &&&
&&&& cout && &HelloWorld()-------------& &&&
&&&& HelloWorld();&
&&&& cout && &Add()--------------------& &&&
&&&& Add();&
&&&& cout && &TestDict-----------------& &&&
&&&& TestTransferDict();&
&&&& cout && &TestClass----------------& &&&
&&&& TestClass();&
//&&&& system(&pause&);&
&&&& return 0;&
void HelloWorld()&
&&&& Py_Initialize();
&&&&&&& PyRun_SimpleString(&import sys&);
&&&&&&& PyRun_SimpleString(&print 'hi,python!'&);
&&&&&&& PyRun_SimpleString(&sys.path.append('./')&);
&&&& PyObject * pModule = NULL;
&&&& PyObject * pFunc = NULL;
&&&& pModule =PyImport_ImportModule(&Test001&);
&&&& pFunc= PyObject_GetAttrString(pModule, &HelloWorld&);
&&&& PyEval_CallObject(pFunc, NULL);
&&&& Py_Finalize();
void Add()&
&&&& Py_Initialize();&
&&&&&&& PyRun_SimpleString(&import sys&);
&&&&&&& PyRun_SimpleString(&sys.path.append('./')&);&&
&&&& PyObject * pModule = NULL;&&&&&
&&&& PyObject * pFunc = NULL;&&&&&&&
&&&& pModule =PyImport_ImportModule(&Test001&);
&&&& pFunc= PyObject_GetAttrString(pModule,&add&);
&&&& PyObject *pArgs = PyTuple_New(2);
&&&& PyTuple_SetItem(pArgs, 0, Py_BuildValue(&i&, 5));
&&&& PyTuple_SetItem(pArgs, 1, Py_BuildValue(&i&, 7));
&&&& PyObject *pReturn = NULL;&
&&&& pReturn = PyEval_CallObject(pFunc, pArgs);
&&&& PyArg_Parse(pReturn, &i&, &result);
&&&& cout && &5+7 = & && result &&&
&&&& Py_Finalize();&&&&&&&&&&&&&&&&&
void TestTransferDict()&
&&&& Py_Initialize();&
&&&&&&& PyRun_SimpleString(&import sys&);
&&&&&&& PyRun_SimpleString(&sys.path.append('./')&);
&&&& PyObject * pModule = NULL;&&&&&
&&&& PyObject * pFunc = NULL;&&&&&&&
&&&& pModule =PyImport_ImportModule(&Test001&);
&&&& pFunc= PyObject_GetAttrString(pModule, &TestDict&);
&&&& PyObject *pArgs = PyTuple_New(1);&&
&&&& PyObject *pDict = PyDict_New();
&&&& PyDict_SetItemString(pDict, &Name&, Py_BuildValue(&s&, &WangYao&));
&&&& PyDict_SetItemString(pDict, &Age&, Py_BuildValue(&i&, 25));
&&&& PyTuple_SetItem(pArgs, 0, pDict);
&&&& PyObject *pReturn = NULL;&
&&&& pReturn = PyEval_CallObject(pFunc, pArgs);
&&&& int size = PyDict_Size(pReturn);&
&&&& cout &&&TTTTTTTT& && size &&&
&&&& PyObject *pNewAge = PyDict_GetItemString(pReturn, &Age&);&
&&&& int newA&
&&&& PyArg_Parse(pNewAge, &i&, &newAge);&
&&&& cout && &True Age: & && newAge &&&
&&&& Py_Finalize();&&&&&&&&&&&&&&&&&
void TestClass()&
&&&& Py_Initialize();&
&&&&&&& PyRun_SimpleString(&import sys&);
&&&&&&& PyRun_SimpleString(&sys.path.append('./')&);
&&&& PyObject * pModule = NULL;&&&&&
&&&& PyObject * pFunc = NULL;&&&&&&&
&&&& pModule =PyImport_ImportModule(&Test001&);
&&&& pFunc= PyObject_GetAttrString(pModule, &TestDict&);
&&&& PyObject *pClassPerson = PyObject_GetAttrString(pModule, &Person&);&
&&&& PyObject *pInstancePerson = PyInstance_New(pClassPerson, NULL, NULL);&
&&&& PyObject_CallMethod(pInstancePerson, &greet&, &s&, &Hello Kitty&);
&&&& Py_Finalize();&&&&&&&&&&&
Test001.py
def HelloWorld():&&&
&&& print &Hello World&&&&
def add(a, b):&&&
&&& return a+b&&&
def TestDict(dict):&&&
&&& print dict&&&
&&& dict[&Age&] = 17&&&
&&& return dict&&&
class Person:&&&
&&& def greet(self, greetStr):&&&
&&&&&&& print greetStr&&&
#print add(5,7)&&&&
#a = raw_input(&Enter To Continue...&)&
def HelloWorld():&
&&& print &Hello World&&
def add(a, b):&
&&& return a+b&
def TestDict(dict):&
&&& print dict&
&&& dict[&Age&] = 17&
&&& return dict&
class Person:&
&&& def greet(self, greetStr):&
&&&&&&& print greetStr&
#print add(5,7)&
#a = raw_input(&Enter To Continue...&)
PyRun_SimpleString(&import sys&);&
PyRun_SimpleString(&sys.path.append('./')&);&
&&&&&&& PyRun_SimpleString(&import sys&);
&&&&&&& PyRun_SimpleString(&sys.path.append('./')&);
分别导入sys,接着设置py文件的路径
4、PySys_SetArgv、PySys_SetPath和PyRun_AnyFile的用法:
#include &Python.h&&&
void HelloWorld(){&
&&&&&&& char *argv[2];&
&&&&&&& argc = 2;&
&&&&&&& char *tmp=&hello&;&
&&&&&&& char *tmp1=&world&;&
&&&&&&& argv[0]=&
&&&&&&& argv[1]= tmp1;&
&&&&&&& Py_Initialize();&
&&&&&&& PySys_SetArgv(argc, argv);&
&&&&&&& PySys_SetPath(&./&);&
//&&&&& PyRun_SimpleString(&import sys&);&&&
//&&&&& PyRun_SimpleString(&sys.path.append('./')&);&&
&&&&&&& PyRun_SimpleString(&print 'hi,python!'&);&
&&&&&&& PyObject * pModule = NULL;&
&&&&&&& PyObject * pFunc = NULL;&
&&&&&&& pModule =PyImport_ImportModule(&mypy&);&
&&&&&&& Py_Finalize();&
void main(){&
&&&&&&& printf(&tttttt\n&);&
&&&&&&& HelloWorld();&
#include &Python.h&
void HelloWorld(){
&&&&&&& char *argv[2];
&&&&&&& argc = 2;
&&&&&&& char *tmp=&hello&;
&&&&&&& char *tmp1=&world&;
&&&&&&& argv[0]=
&&&&&&& argv[1]= tmp1;
&&&&&&& Py_Initialize();
&&&&&&& PySys_SetArgv(argc, argv);
&&&&&&& PySys_SetPath(&./&);
//&&&&& PyRun_SimpleString(&import sys&);
//&&&&& PyRun_SimpleString(&sys.path.append('./')&);
&&&&&&& PyRun_SimpleString(&print 'hi,python!'&);
&&&&&&& PyObject * pModule = NULL;
&&&&&&& PyObject * pFunc = NULL;
&&&&&&& pModule =PyImport_ImportModule(&mypy&);
&&&&&&& Py_Finalize();
void main(){
&&&&&&& printf(&tttttt\n&);
&&&&&&& HelloWorld();
#!/usr/bin/env python&&
import sys&
print '----sys.argv[0]: ',sys.argv[0]&
print '----sys.argv[1]: ',sys.argv[1]&
print '2222'&
def HelloWorld():&
& print 'this is Helloworld'&
if __name__ == '__main__':&
& print 'this is main&
#!/usr/bin/env python
import sys
print '----sys.argv[0]: ',sys.argv[0]
print '----sys.argv[1]: ',sys.argv[1]
print '2222'
def HelloWorld():
& print 'this is Helloworld'
if __name__ == '__main__':
& print 'this is main
PySys_SetArgv,设置参数;
PySys_SetPath,设置py文件路径;
如果用下面的代码:
PyObject* file = PyFile_FromString((char *) &mypy.py&, (char*)&r&);&
FILE *fp = PyFile_AsFile(file);&
PyRun_AnyFile(fp,&mypy.py&);&
&&&&& PyObject* file = PyFile_FromString((char *) &mypy.py&, (char*)&r&);
&&&&& FILE *fp = PyFile_AsFile(file);
&&&&& PyRun_AnyFile(fp,&mypy.py&);
PyObject * pModule = NULL;&
pModule =PyImport_ImportModule(&mypy&);&
&&&&&&& PyObject * pModule = NULL;
&&&&&&& pModule =PyImport_ImportModule(&mypy&);
也可以执行mypy.py,但是PyRun_AnyFile会执行mypy.py中的__main__中的代码;
二、【Python调用C/C++】
&Python开发效率高,运行效率低。而c/c++恰恰相反。因此在python脚本中调用c/c++的库,对python进行扩展,是很有必要的。使用python api,
#include &stdio.h&&&
void display() {&
&&& printf(&This is Display Function\n&);&&
#include &stdio.h&
void display() {
&printf(&This is Display Function\n&);
gcc -shared -fpic test.c -o libtestso1.so -I./include/python2.7
import ctypes&
so = ctypes.CDLL(&./libtestso1.so&)&
so.display()&
import ctypes
so = ctypes.CDLL(&./libtestso1.so&)
so.display()
testso2.cpp
#include&iostream&&&
class TestLib{&
&&&&&&& public:&
&&&&&&&&&&&&&&& void display();&
&&&&&&&&&&&&&&& void display(int a);&
void TestLib::display() {&
&&&&&& std::cout&&&First display&&&std::&
void TestLib::display(int a) {&
&&&&&&& std::cout&&&Second display&&&std::&
extern &C& {&
&&&&&&& TestL&
&&&&&&& void display() {&
&&&&&&&&&&&&&& obj.display();&&
&&&&&&& }&
&&&&&&& void display_int() {&
&&&&&&&&&&&&&& obj.display(2);&&
&&&&&&& }&
#include&iostream&
class TestLib{
&&&&&&& public:
&&&&&&&&&&&&&&& void display();
&&&&&&&&&&&&&&& void display(int a);
void TestLib::display() {
&&&&&& std::cout&&&First display&&&std::
void TestLib::display(int a) {
&&&&&&& std::cout&&&Second display&&&std::
extern &C& {
&&&&&&& TestL
&&&&&&& void display() {
&&&&&&&&&&&&&& obj.display();
&&&&&&& void display_int() {
&&&&&&&&&&&&&& obj.display(2);
g++ -shared -fpic testso2.cpp -o libtestso2.so -I./include/python2.7/
testso2.py
import ctypes&
so = ctypes.CDLL(&./libtestso2.so&)&
so.display()&
so.display_int(1)&
import ctypes
so = ctypes.CDLL(&./libtestso2.so&)
so.display()
so.display_int(1)
testso.cpp
#include &python2.6/Python.h& //包含python的头文件&&&&
// 1 c/cpp中的函数&&&&
int my_c_function(const char *arg) {&&&
& int n = system(arg);&&&
// 2 python 包装&&&&
static PyObject * wrap_my_c_fun(PyObject *self, PyObject *args) {&&&
& const char *&&&
& if (!PyArg_ParseTuple(args, &s&, &command))//这句是把python的变量args转换成c的变量command&&&&
&&& return NULL;&&&
& n = my_c_function(command);//调用c的函数&&&&
& return Py_BuildValue(&i&, n);//把c的返回值n转换成python的对象&&&&
// 3 方法列表&&&&
static PyMethodDef MyCppMethods[] = {&&&
&&& //MyCppFun1是python中注册的函数名,wrap_my_c_fun是函数指针&&&&
&&& { &MyCppFun1&, wrap_my_c_fun, METH_VARARGS, &Execute a shell command.& },&&&
&&& { NULL, NULL, 0, NULL }&&&
// 4 模块初始化方法&&&&
PyMODINIT_FUNC initMyCppModule(void) {&&&
& //初始模块,把MyCppMethods初始到MyCppModule中&&&&
& PyObject *m = Py_InitModule(&MyCppModule&, MyCppMethods);&&&
& if (m == NULL)&&&
#include &python2.6/Python.h& //包含python的头文件&
// 1 c/cpp中的函数&
int my_c_function(const char *arg) {&
& int n = system(arg);&
// 2 python 包装&
static PyObject * wrap_my_c_fun(PyObject *self, PyObject *args) {&
& const char *&
& if (!PyArg_ParseTuple(args, &s&, &command))//这句是把python的变量args转换成c的变量command&
&&& return NULL;&
& n = my_c_function(command);//调用c的函数&
& return Py_BuildValue(&i&, n);//把c的返回值n转换成python的对象&
// 3 方法列表&
static PyMethodDef MyCppMethods[] = {&
&&& //MyCppFun1是python中注册的函数名,wrap_my_c_fun是函数指针&
&&& { &MyCppFun1&, wrap_my_c_fun, METH_VARARGS, &Execute a shell command.& },&
&&& { NULL, NULL, 0, NULL }&
// 4 模块初始化方法&
PyMODINIT_FUNC initMyCppModule(void) {&
& //初始模块,把MyCppMethods初始到MyCppModule中&
& PyObject *m = Py_InitModule(&MyCppModule&, MyCppMethods);&
& if (m == NULL)&
mips-linux-gnu-g++ -EL -shared -fpic testso.cpp -o MyCppModule.so -L./lib -lpython2.7 -I./include/python2.7/
# -*- coding: utf-8 -*-&&&&
import MyCppModule&&&
#导入python的模块(也就是c的模块,注意so文件名是MyCppModule&&&&&&
r = MyCppModule.MyCppFun1(&ls -l&)&&&
print r&&&&
print &OK&&&
# -*- coding: utf-8 -*-&
import MyCppModule&
#导入python的模块(也就是c的模块,注意so文件名是MyCppModule&&&
r = MyCppModule.MyCppFun1(&ls -l&)&
print &OK& PS:PyModule_Create和Py_InitModule的区别:
(window.slotbydup=window.slotbydup || []).push({
id: '2467140',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467141',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467142',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467143',
container: s,
size: '1000,90',
display: 'inlay-fix'
(window.slotbydup=window.slotbydup || []).push({
id: '2467148',
container: s,
size: '1000,90',
display: 'inlay-fix'2013年3月 C/C++大版内专家分月排行榜第三
2013年 总版技术专家分年内排行榜第三
2012年 总版技术专家分年内排行榜第七
2013年 总版技术专家分年内排行榜第三
2012年 总版技术专家分年内排行榜第七
2013年 总版技术专家分年内排行榜第三
2012年 总版技术专家分年内排行榜第七
匿名用户不能发表回复!|
每天回帖即可获得10分可用分!小技巧:
你还可以输入10000个字符
(Ctrl+Enter)
请遵守CSDN,不得违反国家法律法规。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。

我要回帖

更多关于 hello kitty壁纸 的文章

 

随机推荐