//
average.cpp : Defines the entry point for the console application.
//
#include " stdafx.h "
#include < stdio.h >
extern " C " {
#include " lua.h "
#include " lualib.h "
#include " lauxlib.h "
}
/* 指向Lua解释器的指针 */
lua_State * L;
static int average(lua_State * L)
{
/* 得到参数个数 */
int n = lua_gettop(L);
double sum = 0 ;
int i;
/* 循环求参数之和 */
for (i = 1 ; i <= n; i ++ )
{
/* 求和 */
sum += lua_tonumber(L, i);
}
/* 压入平均值 */
lua_pushnumber(L, sum / n);
/* 压入和 */
lua_pushnumber(L, sum);
/* 返回返回值的个数 */
return 2 ;
}
int main ( int argc, char * argv[] )
{
/* 初始化Lua */
L = lua_open();
/* 载入Lua基本库 */
luaL_openlibs(L);
/* 注册函数 */
lua_register(L, " average " , average);
/* 运行脚本 */
luaL_dofile(L, " avg.lua " );
/* 清除Lua */
lua_close(L);
/* 暂停 */
printf( " Press enter to exit… " );
getchar();
return 0 ;
}
//
#include " stdafx.h "
#include < stdio.h >
extern " C " {
#include " lua.h "
#include " lualib.h "
#include " lauxlib.h "
}
/* 指向Lua解释器的指针 */
lua_State * L;
static int average(lua_State * L)
{
/* 得到参数个数 */
int n = lua_gettop(L);
double sum = 0 ;
int i;
/* 循环求参数之和 */
for (i = 1 ; i <= n; i ++ )
{
/* 求和 */
sum += lua_tonumber(L, i);
}
/* 压入平均值 */
lua_pushnumber(L, sum / n);
/* 压入和 */
lua_pushnumber(L, sum);
/* 返回返回值的个数 */
return 2 ;
}
int main ( int argc, char * argv[] )
{
/* 初始化Lua */
L = lua_open();
/* 载入Lua基本库 */
luaL_openlibs(L);
/* 注册函数 */
lua_register(L, " average " , average);
/* 运行脚本 */
luaL_dofile(L, " avg.lua " );
/* 清除Lua */
lua_close(L);
/* 暂停 */
printf( " Press enter to exit… " );
getchar();
return 0 ;
}
avg.lua:
-- call a C++ function
avg, sum = average(10, 20, 30, 40, 50)
print("The average is ", avg)
print("The sum is ", sum)