lua程序设计(第四版)练习答案自做(第三十章)

30.1

#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
int filter(lua_State *L)
{
	int index=1;
	luaL_checktype(L,1,LUA_TTABLE);
	luaL_checktype(L,2,LUA_TFUNCTION);
	int len=luaL_len(L,1);
	lua_newtable(L);
	lua_insert(L,3);
	for(int i=1;i<=len;i++)
	{
		lua_pushvalue(L,2);
		lua_geti(L,1,i);
		lua_call(L,1,1);
		if(lua_toboolean(L,-1))
		{
			lua_geti(L,1,i);
			lua_seti(L,3,index++);
			lua_pop(L,1);
		}
		else
			lua_pop(L,1);
	}
	return 1;
}

30.2

#include <string.h>
#include <lauxlib.h>
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
static int l_split(lua_State *L)
{
	size_t len;
	int i=1;
	const char *s=lua_tolstring(L,1,&len);
	if(s==NULL)
		return 0;
	const char *sep=luaL_checkstring(L,2);
	char  *e;
	lua_newtable(L);
	while((e=memchr(s,*sep,len))!=NULL)
	{
		lua_pushlstring(L,s,e-s);
		lua_rawseti(L,-2,i++);
		len=len-(e-s+1);
		s=e+1;
	}
	if(len!=0)
	{
		lua_pushlstring(L,s,len);
		lua_rawseti(L,-2,i);
	}
	return 1;
}

30.3

#include <string.h>
#include <lauxlib.h>
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
static int l_transliterate(lua_State* L)
{
	int index;
	size_t len;
	luaL_Buffer b;
	luaL_checktype(L,1,LUA_TSTRING);
	luaL_checktype(L,2,LUA_TTABLE);
	const char *str=lua_tolstring(L,1,&len);
	luaL_buffinit(L,&b);
	for(index=0;index<len;index++)
	{
		lua_pushlstring(L,str+index,1);
		switch(lua_gettable(L,2))
		{
			case 1:
				if(lua_toboolean(L,-1))
				{
					lua_pop(L,1);
					lua_pushlstring(L,str+index,1);
					luaL_addvalue(&b);
					break;
				}
				else
				{
					lua_pop(L,1);
					break;
				}
			case 4:
				luaL_addvalue(&b);
				break;
			default:
				lua_pop(L,1);
				lua_pushlstring(L,str+index,1);
				luaL_addvalue(&b);
		}
	}
	luaL_pushresult(&b);
	return 1;
}

30.4

#include <string.h>
#include <lauxlib.h>
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
static int l_transliterate(lua_State* L)
{
	int index;
	size_t len;
	luaL_Buffer b;
	lua_getfield(L,LUA_REGISTRYINDEX,"table");
	lua_insert(L,2);
	luaL_checktype(L,1,LUA_TSTRING);
	luaL_checktype(L,2,LUA_TTABLE);
	const char *str=lua_tolstring(L,1,&len);
	luaL_buffinit(L,&b);
	for(index=0;index<len;index++)
	{
		lua_pushlstring(L,str+index,1);
		switch(lua_gettable(L,2))
		{
			case 1:
				if(lua_toboolean(L,-1))
				{
					lua_pop(L,1);
					lua_pushlstring(L,str+index,1);
					luaL_addvalue(&b);
					break;
				}
				else
				{
					lua_pop(L,1);
					break;
				}
			case 4:
				luaL_addvalue(&b);
				break;
			default:
				lua_pop(L,1);
				lua_pushlstring(L,str+index,1);
				luaL_addvalue(&b);
		}
	}
	luaL_pushresult(&b);
	return 1;
}
static int l_settrans(lua_State *L)
{
	luaL_checktype(L,1,LUA_TTABLE);
	lua_pushvalue(L,1);
	lua_setfield(L,LUA_REGISTRYINDEX,"table");
	return 0;
}
static int l_gettrans(lua_State *L)
{
	lua_getfield(L,LUA_REGISTRYINDEX,"table");
	return 1;
}
static const struct luaL_Reg trans[]=
{
	{"transliterate",l_transliterate},
	{"settrans",l_settrans},
	{"gettrans",l_gettrans},
	{NULL,NULL}
};
int luaopen_lib(lua_State *L)
{
	luaL_newlib(L,trans);
	return 1;
}

30.5

#include <string.h>
#include <lauxlib.h>
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
static int l_transliterate(lua_State* L)
{
	int index;
	size_t len;
	luaL_Buffer b;
	lua_getfield(L,lua_upvalueindex(1),"table");
	lua_insert(L,2);
	luaL_checktype(L,1,LUA_TSTRING);
	luaL_checktype(L,2,LUA_TTABLE);
	const char *str=lua_tolstring(L,1,&len);
	luaL_buffinit(L,&b);
	for(index=0;index<len;index++)
	{
		lua_pushlstring(L,str+index,1);
		switch(lua_gettable(L,2))
		{
			case 1:
				if(lua_toboolean(L,-1))
				{
					lua_pop(L,1);
					lua_pushlstring(L,str+index,1);
					luaL_addvalue(&b);
					break;
				}
				else
				{
					lua_pop(L,1);
					break;
				}
			case 4:
				luaL_addvalue(&b);
				break;
			default:
				lua_pop(L,1);
				lua_pushlstring(L,str+index,1);
				luaL_addvalue(&b);
		}
	}
	luaL_pushresult(&b);
	return 1;
}
static int l_settrans(lua_State *L)
{
	luaL_checktype(L,1,LUA_TTABLE);
	lua_pushvalue(L,1);
	lua_setfield(L,lua_upvalueindex(1),"table");
	return 0;
}
static int l_gettrans(lua_State *L)
{
	lua_getfield(L,lua_upvalueindex(1),"table");
	return 1;
}
static const struct luaL_Reg trans[]=
{
	{"transliterate",l_transliterate},
	{"settrans",l_settrans},
	{"gettrans",l_gettrans},
	{NULL,NULL}
};
int luaopen_lib(lua_State *L)
{
	luaL_newlibtable(L,trans);
	lua_newtable(L);
	luaL_setfuncs(L,trans,1);
	return 1;
}

30.6

私以为失去了灵活性。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值