`lua程序设计第四版 第29章答案

lua程序设计第四版 第29章答案
29.1~29.3

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include “lua.h”
#include “lauxlib.h”
#include “lualib.h”

inline int error2(lua_State* L, const char* fmt, …) {
va_list argp;
va_start(argp, fmt);
vfprintf(stderr, fmt, argp);
va_end(argp);
lua_close(L);
return 1;
}
// 29.1
static int l_summation(lua_State* L) {
int top = lua_gettop(L);
printf(“l_summation ~top[%d]\n”, top);
double ans = 0;
for (int i = 1; i <= top; ++i) {
int t = lua_type(L, i);
if (t != LUA_TNUMBER)
error2(L, “wrong result type”);
ans += lua_tonumber(L, i);
}
lua_pushnumber(L, ans);
return 1;
}

//29.2
static int l_pack(lua_State* L) {
int top = lua_gettop(L);
lua_newtable(L);
for (int i = 1; i <= top; ++i) {
int t = lua_type(L, i);
switch (t)
{
case LUA_TNUMBER:
lua_pushinteger(L, i);
lua_pushnumber(L, lua_tonumber(L, i));
lua_settable(L, -3);
break;
case LUA_TSTRING:
lua_pushinteger(L, i);
lua_pushstring(L, lua_tostring(L, i));
lua_settable(L, -3);
break;
case LUA_TBOOLEAN:
lua_pushinteger(L, i);
lua_pushboolean(L, lua_toboolean(L, i));
lua_settable(L, -3);
break;
default:
break;
}
}
return 1;
}

//29.3
static int l_reverse(lua_State* L) {
int top = lua_gettop(L);
for (int i = 1; i <= top; ++i) {
int t = lua_type(L, i);
switch (t)
{
case LUA_TNUMBER:
lua_pushnumber(L, lua_tonumber(L, i));
break;
case LUA_TSTRING:
lua_pushstring(L, lua_tostring(L, i));
break;
case LUA_TBOOLEAN:
lua_pushboolean(L, lua_toboolean(L, i));
break;
}
}
for (int i = top; i > 0; --i) {
lua_pushvalue(L, i);
}
for (int i = 1; i <= top; ++i) {
lua_remove(L, 1);
}
return top;
}

static const struct luaL_Reg mylib[] = {
{“summation”, l_summation},
{“pack”, l_pack},
{NULL, NULL}
};

int luaopen_mylib(lua_State* L) {
luaL_newlib(L, mylib);
return 2;
}

int main() {
lua_State* L = luaL_newstate();
luaL_openlibs(L);
lua_pushcfunction(L, l_summation);
lua_setglobal(L, “summation”);
lua_pushcfunction(L, l_pack);
lua_setglobal(L, “pack”);
lua_pushcfunction(L, l_reverse);
lua_setglobal(L, “reverse”);
luaL_dofile(L, “main.lua”);
lua_close(L);
return 1;
}

main.lua

print(‘summation()’,summation())
print(‘summation(2.3, 5.4)’,summation(2.3, 5.4))
print(‘summation(2.3, -34)’,summation(2.3, -34))

a = pack(3,4,5,“string”, true);
print(‘a’, type(a))
for k,v in ipairs(a) do
print(k, v);
end

print(‘reverse 1, 2, 3 “hello”, true’);
print(reverse(1, 2, 3 ,“hello”, true))

在这里插入图片描述

29.4
int l_foreach(lua_State* L) {
int top = lua_gettop(L);
printf("[top][%d]\n", top);
const char* table_typename = lua_typename(L, lua_type(L, -2));
const char* function_typename = lua_typename(L, lua_type(L, -1));
lua_CFunction f = lua_tocfunction(L, -1);
printf("%p\n", f);
lua_pop(L, 1);
printf("[%s] [%s]\n", table_typename, function_typename);
top = lua_gettop(L);
printf(“second [top][%d]\n”, top);
lua_pushnil(L);
while (lua_next(L, top))
{
// -1 是value -2 是key
int key_type = lua_type(L, -2);
int value_type = lua_type(L, -1);
const char* key_typename = lua_typename(L, lua_type(L, -2));
const char* value_typename = lua_typename(L, lua_type(L, -1));
//printf("[%d] [%d] [%s] [%s]\n", key_type, value_type, key_typename, value_typename);
if (key_type == LUA_TSTRING) {
const char* skey = lua_tostring(L, -2);
double a = lua_tonumber(L, -1);
printf(“string key[%s]–>value[%g]\n”, skey, a);
lua_pushcfunction(L, f);
lua_pushstring(L, skey);
lua_pushnumber(L, a);
lua_call(L, 2, 0);
}
else if (key_type == LUA_TNUMBER) {
double dkey = lua_tonumber(L, -2);
double a = lua_tonumber(L, -1);
printf(“number key[%g]–>value[%g]\n”, dkey, a);
lua_pushcfunction(L, f);
lua_pushnumber(L, dkey);
lua_pushnumber(L, a);
lua_call(L, 2, 0);
}
lua_pop(L, 1);
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值