lua例子(进出栈)

#include <stdio.h>
extern "C"
{
#include "lua-5.2.2/src/lauxlib.h"
#include "lua-5.2.2/src/lualib.h"
#include "lua-5.2.2/src/lstate.h"
}
//lua与c交互栈的索引,假如栈中有5个元素
//5 -1
//4 -2
//3 -3
//2 -4
//1 -5
void stackDump(lua_State* L)
{
    int i = 0;
    int top = lua_gettop(L);//获取栈中元素的个数,即栈顶元素的索引
    for (int i = 0; i < top; i++)
    {
        int t = lua_type(L, i);//返回栈中元素的类型
        switch (t)
        {
        case LUA_TSTRING:
            printf("%s", lua_tostring(L, i));//从栈中i索引位置取字符串,返回字符串的指针,对于字符串指针不能在函数外部,函数返回后会清理栈
            break;
        case LUA_TBOOLEAN:  /* booleans */
            printf(lua_toboolean(L, i) ? "true" : "false");
            break;
        case LUA_TNUMBER:  /* numbers */
            printf("%g", lua_tonumber(L, i));
            break;
        default:  /* other values */
            printf("%s", lua_typename(L, t));//转换类型码到类型名
            break;
        }
        printf("  ");
    }
    //printf("\n");
}

int main()
{
    lua_State* L = luaL_newstate();
    lua_pushboolean(L, 1);//向栈中压入bool类型
    lua_pushinteger(L, 10);//向栈中压入int类型
    lua_pushnil(L); //向栈中压入空值nil
    lua_pushstring(L, "hello");//压入c风格字符串
    stackDump(L);
    /* true  10  nil  `hello‘  */ 

    lua_pushvalue(L, -4); //压入堆栈上指定索引位置的拷贝到栈顶
    stackDump(L);
    ///* true  10  nil  `hello‘  true  */ 

    lua_replace(L, 3);
    stackDump(L);
    ///* true  10  true  `hello‘  */
    /*Lua_insert移动栈顶元素到指定索引位置*/
    lua_settop(L, 6); //设置栈顶,不够补nil,多了删除,lua_settop(L, 0)清空栈
    stackDump(L);
    ///* true  10  true  `hello‘  nil  nil  */ 

    lua_remove(L, -3);//移除指定索引位置的元素,并将上面的元素下移
    stackDump(L);
    ///* true  10  true  nil  nil  */ 

    lua_settop(L, -5);
    stackDump(L);
    /* true  */ 

    lua_close(L);
    getchar();
    return 0;
}

这个vs不知怎搞的老是出现蛋疼的问题

时间: 2024-08-15 00:35:58

lua例子(进出栈)的相关文章

lua 例子

//顶 -1 -2 -3 //顶 3 2 1 #include <stdio.h> #include <string.h> extern "C"{ #include <lua.h> #include <lauxlib.h> #include <lualib.h> } #pragma comment(lib,"lua.lib") void stackDump(lua_State* L) { int i; in

lua例子学习

#include <iostream> #include <string.h> extern "C" { /*头文件lua.h定义了Lua提供的基础函数,包括创建Lua环境.调用Lua函数.读写Lua环境中全局变量,以及注册供Lua调用的新函数等等*/ #include "lua-5.2.2/src/lua.h" /*头文件lauxlib.h定义了辅助库提供的辅助函数,它的所有定义都以LuaL_开头.辅助库是一个使用lua.h中API编写出的

PTA-——模拟进出栈

PTA 02-线性结构4 Pop Sequence 方法一: 1 #include<stdio.h> 2 #define MAXSIZE 1000 3 4 typedef struct{ 5 int data[MAXSIZE]; 6 int top; 7 }SqStack; 8 9 int InitStack(SqStack *s){ 10 s->top=-1; 11 return 0; 12 } 13 14 int Push(SqStack *s,int e){ 15 if(s->

lua例子getglobal()

#include <stdio.h> #define MAX_COLOR 255 extern "C" { #include "lua-5.2.2/src/lauxlib.h" #include "lua-5.2.2/src/lualib.h" #include "lua-5.2.2/src/lstate.h" } int getfield(const char* key, lua_State* L) { int

JAVA堆栈进出栈

    /**      * @param args      */     @SuppressWarnings({ "rawtypes", "unchecked" }) public static void main(String[] args) {         Stack stack = new Stack(); // 创建堆栈对象          System.out.println("11111, absdder, 29999.3 三个元素入

Lua实现的栈、队列

基于quick cocos2d-x2.2.5的API,写的一个栈,队列.满足游戏开发中的需求. 避免重复造车轮~(简单测试.留着自己用.不确定是否还存在bug) 栈Stack: 1 -- 2 -- Date: 2014-11-19 15:29:02 3 -- 4 local Stack = class("Stack") 5 6 function Stack:ctor() 7 self.stack_table = {} 8 end 9 10 function Stack:push(ele

火车进出栈 java

题目描述 一列火车n节车厢,依次编号为1,2,3,-,n.每节车厢有两种运动方式,进栈与出栈,问n节车厢出栈的可能排列方式有多少种. 输入 一个数,n(n<=60000) 输出 一个数s表示n节车厢出栈的可能排列方式 题解: 这题要用大数,java 然后卡特兰数:有个数学模型 s[i]=c(n,2n)/(n+1) 组合数公式:这个总是记不住 这个题不能直接求,会TLE.需要先求出结果的每个质因数有多少次幂,然后用快速幂求,再把所有求幂得到的结果乘起来. http://www.cnblogs.co

Fastjson序列化map时,保留map的进出栈顺序的方法

最近工作中遇到了这样的需求: 我用fastjson序列有序map的时候,tojson方法会让这个map无序,tojsonstring虽然能保留map的结构,不过会让结果里含有\(我是jsonStrin套jsonString,java会出现转义字符) 解决方案JSONObject有个传入boolean值的构造函数 如果传入的是true就代表保留数据结构的顺序 这时候我们可以吧要序列化的map放到我们创建的JSONObject对象里 最后再把我们的JSONObject对象放到最终的结果里 代码示例

数据结构03-顺序栈(用C++、C#、lua实现)

目录 1.C#实现 2.C++实现 3.lua实现 本文为数据结构-顺序栈的代码实现. 作者水平比较差,有错误的地方请见谅. 1.C#实现 栈接口 IStack.cs /// <summary> /// 栈接口 /// </summary> public interface IStack<T> { int GetLength(); //求栈的长度 bool IsEmpty(); //判断栈是否为空 void Clear(); //清空 void Push(T item)