C++向Lua传递数组
2014-05-20 15:09
134 查看
没什么解释,直接贴代码
cnid.lua 这个不是重点
cnid.lua 这个不是重点
--verify cnid function verify_cnid(id) --校验位 local verifymap = {1, 0, 88, 9, 8, 7, 6, 5, 4, 3, 2} --加权因子 local factor = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2} --加权和 local sum = 0 local index = 1 while index ~= 18 do --身份证长18位 sum = sum + (id[index]) * factor[index] index = index + 1 end --求模11 local r = sum % 11 if verifymap[r + 1] == id[18] then return true else return false end endluacnid.cpp
#include <cstdlib> #include <iostream> extern "C" { #include <lua.h> #include <lualib.h> #include <lauxlib.h> } typedef unsigned short wchar; //point to Lua Interpreter //lua_State *L; bool lua_verify(lua_State *L, wchar * id, int len) { //get lua function name lua_getglobal(L, "verify_cnid"); //get lua function parameter //create a new table lua_newtable(L); lua_pushnumber(L, -1); //push -1 into stack lua_rawseti(L, -2, 0); //set array[0] by -1 for(int i = 0; i < len; i++) { lua_pushinteger(L, id[i]); //push lua_rawseti(L, -2, i+1); // } //call function, 1 parameter, 1 return value lua_call(L, 1, 1); //get result bool result = (bool)lua_toboolean(L, -1); lua_pop(L, 1); return result; } int main() { lua_State *L; //init Lua L = lua_open(); //load Lua libs luaL_openlibs(L); //load lua file luaL_dofile(L, "cnid.lua"); //call Lua function wchar id[] = {4,4,2,3,2,5,1,8,8,0,0,4,1,8,4,0,4,3}; bool result = lua_verify(L, id, 18); std::cout << result << std::endl; //clear Lua lua_close(L); return 0; }
相关文章推荐
- C++怎么传递一个数组到LUA
- 7.Lua调用c++传递数组函数
- C++中数组参数传递
- 在VB中调用C/C++语言编写的dll,数组参数的传递
- C++_new创建的数组作为参数传递
- c#调用c++的ocx控件传递数组
- C++中数组作为参数传递时,是否能在传递过后求该数组的大小呢?
- 请赐教:用C++做一个DLL在VB中调用,要传递的是一个数组
- 在VB中调用C/C++语言编写的dll,数组参数的传递
- C++同过数组传递后改变数组的元素
- C++ 传递数组的问题
- C#调用c++的dll,结构体数组作为引用参数的传递方式
- lua与c++数据交换之表和数组
- C++向函数模版传递 const类型,数组类型
- C/C++二维数组的动态分配及参数传递
- c++中关于数组作为函数参数并传递数组元素个数的几种有效方法的讨论
- C++中数组参数传递
- C++中数组长度自动传递
- c++中关于数组作为函数参数并传递数组元素个数的几种有效方法的讨论
- C++ 指针 多维数组传递参数