|
马上加入TC
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
一个语言只所有为什么好用,用的人多,其中有两部分:
第一部分是:语言平台本身强大
第二部分是:基于些平台的开源基函数强大
所以,为了TC语方更好用,更强大。更多的提高大家的编码效率,本人向所有高手们发发起开源运动呼吁,把你们的珍藏的基础工具函数拿出来吧。让大家共享吧。
列表代码:
- //用数组实现在列表,模似2.03中变量队列的功能,可保存随意个元素
- namespace 列表
- //一个多行二列的数组,第一列为id,第二列为数据
- int[][] date
- //当前列表中有多少个元素
- int size
- //当前数组的长度
- int length
- //每次扩大值
- int growth
- //多线程同步标识
- bool isinit
- //初始化数组与数组长度,初始大小为100
- function bool 初始化(int 初始大小,int 扩张值)
- if( isinit == false)
- length = 初始大小
- date = int[length][2]
- growth = 扩张值
- isinit = true
- endif
- return true
- endfunction
- //销毁列表
- function bool 销毁()
- if( isinit == true )
- date = int[1][2]
- length = 0
- growth = 0
- size = 0
- isinit = false
- endif
- return true
- endfunction
- //增加一个元素到列表中,id为编号,value为值,不能是0做为id值
- //true为增加成功,false为增加失败
- //如果id重复,会将原有值进行覆盖
- function bool 增加元素(int id,int value)
- if( id == 0 )
- return false
- endif
- //列表已满
- if( size >= length )
- 列表.扩张数组()
- endif
- //循环数组,查看是否存在空位,如果有空位,则将数据放入
- int i = 0
- while(i < length)
- //如果查找到为0,则表示此位没有被使用
- if( date[i][0] == 0 )
- date[i][0] = id
- date[i][1] = value
- size = size + 1
- break
- endif
- i = i + 1
- endwhile
- return true
- endfunction
- //根据编号删除元素
- //成功返回true 失败返回false
- function bool 删除元素(int id)
- int i = 0
- while(i < length)
- if( date[i][0] == id)
- date[i][0] = 0
- date[i][1] = 0
- size = size - 1
- return true
- endif
- i = i + 1
- endwhile
- return false
- endfunction
- //查看编号是否已存在
- //true表示已存在 false表示不存在
- function bool 编号是否存在(int id)
- int i = 0
- while(i < length)
- if( date[i][0] == id )
- return true
- endif
- i = i + 1
- endwhile
- return false
- endfunction
- //查看此值是否在列表中已存
- //不能判度0值是否存在
- function bool 值是否存在(int value)
- int i = 0
- while(i < length)
- if( date[i][1] == value )
- return true
- endif
- i = i + 1
- endwhile
- endfunction
- //根据编号取出列表中的元素
- //成功,返回指定的值。失败返回-2147483647,约定此值为失败
- function int 取出元素(int id)
- if( 列表.编号是否存在(id) == false)
- return -2147483647
- endif
- int i = 0
- while(i < length)
- if( date[i][0] == id )
- return date[i][1]
- endif
- i = i + 1
- endwhile
- endfunction
- //得到当前列表元数个数
- function int 列表元素个数()
- return size
- endfunction
- //得到当前列表的大小
- function int 列表大小()
- return length
- endfunction
- //这个函数可以得到所有id值,返回一个int类型数组
- //返回数组的0下标值为数组的长度
- function int[] 得到所有编号()
- int[] ids = int[size + 1]
- ids[0] = size
- int i = 0,j = 1
- while( i < length)
- if(date[i][0] != 0)
- ids[j] = date[i][0]
- j = j + 1
- endif
- i = i + 1
- endwhile
- return ids
- endfunction
- //这个函数可以得到所有值,返回一个int类型数组
- //返回数组的0下标值为数组的长度
- function int[] 得到所有值()
- int[] values = int[size + 1]
- values[0] = size
- int i = 0,j = 1
- while( i < length)
- if(date[i][0] != 0)
- values[j] = date[i][1]
- endif
- i = i + 1
- endwhile
- return values
- endfunction
- //此函数为内部函数
- function bool 扩张数组()
- //定义新的数组,比原数组大扩张值
- int[][] temp = int[length + growth][2]
- //将原数组中的数据拷贝到新数组中
- int i = 0
- while(i < length)
- temp[i][0] = date[i][0]
- temp[i][1] = date[i][1]
- i = i + 1
- endwhile
- //将原数组替换成新数组
- date = temp
- //长度修改为扩张后的长度
- length = length + growth
- return true
- endfunction
- endnamespace
复制代码 测试代码
- #include "列表.t"
- namespace 工具
- //定义默认空间
- function bool start_click()
- 列表.初始化(10,10)
- //string id,value
- //repeat(11)
- // help.inputbox("id","id",id)
- // help.inputbox("value","value",value)
- // help.sleep(100)
- //
- // 列表.增加元素(convert.strtoint(id,0),convert.strtoint(value,0))
- //endrepeat
- //先加10个元素
- 列表.增加元素(1,1)
- 列表.增加元素(2,2)
- 列表.增加元素(3,3)
- 列表.增加元素(4,4)
- 列表.增加元素(5,5)
- 列表.增加元素(6,6)
- 列表.增加元素(7,7)
- 列表.增加元素(8,8)
- 列表.增加元素(9,9)
- 列表.增加元素(10,10)
- //打出列表大小和元数个数
- help.messagebox("列表大小是:" + convert.inttostr(列表.列表大小()))
- help.messagebox("列表元素个数是:" + convert.inttostr(列表.列表元素个数()))
- //多加一个,打出列表大小和元素个数
- 列表.增加元素(11,11)
- help.messagebox("增加一个元素后列表大小是:" + convert.inttostr(列表.列表大小()))
- help.messagebox("增加一个元素后列表元素个数是:" + convert.inttostr(列表.列表元素个数()))
- //取出一个元素
- help.messagebox("id:11对应的值为:" + convert.inttostr(列表.取出元素(11)))
- //判断元素是否存在
- if( 列表.编号是否存在(1) )
- help.messagebox("编号1存在")
- endif
- if( 列表.值是否存在(1) )
- help.messagebox("值1存在")
- endif
- if( 列表.编号是否存在(15) )
- help.messagebox("编号15存在")
- else
- help.messagebox("编号15不存在")
- endif
- if( 列表.值是否存在(15) )
- help.messagebox("值15存在")
- else
- help.messagebox("值15不存在")
- endif
- //删除一个元素
- 列表.删除元素(1)
- help.messagebox("删除一个元素后列表大小是:" + convert.inttostr(列表.列表大小()))
- help.messagebox("删除一个元素后元素个数是:" + convert.inttostr(列表.列表元素个数()))
- //得到元素的编号数组
- help.messagebox("删除后所有编号是")
- int[] ids
- ids = 列表.得到所有编号()
- int i = 0
- while( i < ids[0] )
- help.messagebox(convert.inttostr(ids[i+1]))
- i = i + 1
- endwhile
- help.messagebox("增加一个元素100,这个元素将会在第一位置")
- //再加一个元素
- 列表.增加元素(100,100)
- //得到元素的编号数组
- ids = 列表.得到所有编号()
- i = 0
- while( i < ids[0] )
- help.messagebox(convert.inttostr(ids[i+1]))
- i = i + 1
- endwhile
- 列表.销毁()
- return true
- //函数返回语句
- endfunction
- function bool exit_click()
- //这里添加你要执行的代码
- help.exit()
- return true
- //函数返回语句
- endfunction
- endnamespace
- //空间结束语句
复制代码 本人最近想研究脚本日志模块,有想一起研究的朋友到18群找寒心月,就是我。
|
|