zoukankan      html  css  js  c++  java
  • Lua的函数的定义、math数学函数、lua字符串的处理、lua支持的字符串类、模式串中的特殊字符_学习笔记04

      Lua的函数的定义、math数学函数  

    • 定义函数

    function [function name] (param1,param2)

        [function code]

    --定义一个函数用来求的两个数字的和

    function plus (num1,num2)

              return num1+num2

    end

    res = plus(54,12)

    print(res)

    • Lua内置提供了一些常用函数

      1.数学处理的math相关函数

      2.字符串处理的string 相关函数

      3.表处理的table相关函数

      4.文件操作的io相关函数

    (1)数学运算函数

    print(math.abs(-15))  --取绝对值

    print(math.max(11,22,05,655,85)) --求最大的值

    abs

    取绝对值

    math.abs(-15)

    15

    acos

    反余弦函数

    math.acos(0.5)

    1.04719755

    asin

    反正弦函数

    math.asin(0.5)

    0.52359877

    atan2

    x / y的反正切值

    math.atan2(90.0, 45.0)

    1.10714871

    atan

    反正切函数

    math.atan(0.5)

    0.463647609

    ceil

    不小于x的最大整数

    math.ceil(5.8)

    6

    cosh

    双曲线余弦函数

    math.cosh(0.5)

    1.276259652

    cos

    余弦函数

    math.cos(0.5)

    0.87758256

    deg

    弧度转角度

    math.deg(math.pi)

    180

    exp

    计算以e为底x次方值

    math.exp(2)

    2.718281828

    floor

    不大于x的最大整数

    math.floor(5.6)

    5

    fmod (mod)

    取模运算

    math.mod(14, 5)

    4

    frexp

    把双精度数val分解为数字部分(尾数)和以2为底的指数n,即val=x*2n

    math.frexp(10.0)

    0.625    4

    ldexp

    计算value * 2的n次方

    math.ldexp(10.0, 3)

    80 = 10 * (2 ^3)

    log10

    计算以10为基数的对数

    math.log10(100)

    2

    log

    计算一个数字的自然对数

    math.log(2.71)

    0.9969

    max

    取得参数中最大值

    math.max(2.71, 100, -98, 23)

    100

    min

    取得参数中最小值

    math.min(2.71, 100, -98, 23)

    -98

    modf

    把数分为整数和小数

    math.modf(15.98)

    15    98

    pow

    得到x的y次方

    math.pow(2, 5)

    32

    rad

    角度转弧度

    math.rad(180)

    3.14159265358

    random

    获取随机数

    math.random(1, 100)
    math.random(100)

    获取1-100的随机数

    randomseed

    设置随机数种子

    math.randomseed(os.time())

    在使用math.random函数之前必须使用此函数设置随机数种子

    sinh

    双曲线正弦函数

    math.sinh(0.5)

    0.5210953

    sin

    正弦函数

    math.sin(math.rad(30))

    0.5

    sqrt

    开平方函数

    math.sqrt(16)

    4

    tanh

    双曲线正切函数

    math.tanh(0.5)

    0.46211715

    tan

    正切函数

    math.tan(0.5)

    0.5463024

    (2)字符串处理的相关函数

    •  string.find() 函数用于在一个给定的目标字符串中搜索一个模式。最简单的模式就是一个单词,它只会匹配与自己完全相同的拷贝。当find找到一个模式后,它会返回两个值:匹配到的起始索引和结尾索引;如果没有找到任何匹配,它就返回nil

         eg:  local str = "Hello World"

        local i, j = string.find(str,"Hello") -- 返回Hello在str中的起始位置和终止位置

        print(i, j)

    • find的兄弟match函数,string.match与string.find非常相似,它也是用于在一个字符串中搜索一种模式。区别在于string.match返回的是目标字符串中与模式相匹配的那部分子串,并不是该模式所在的位置。示例代码:

        local str = "Hello12345World"

        local subStr = string.match(str,"%d+")

        print(subStr) --结果为 12345

    • 比较全的字符串处理,如下图所示:

    Eg: string = "mengmeng xia"     --替换字符串用 string.gsub
       newstring = string.gsub(string,"xia","da")
       print("The new string is",newstring)

    (3)Lua所支持的所有字符类. 任意字符

        %s 空白符
        %p 标点字符
        %c 控制字符
        %d 数字
        %x 十六进制数字
        %z 代表0的字符
        %a 字母
        %l 小写字母
        %u 大写字母
        %w 字母和数字

    (4)模式串中的特殊字符:

       ( ) . % + - * ? [ ^ $
      '%' 用作特殊字符的转义字符
      '%.' 匹配点;
      '%%' 匹配字符 '%'。

    我们什么都没有,唯一的本钱就是青春。梦想让我与众不同,奋斗让我改变命运!
  • 相关阅读:
    μc/osⅡ简化版任务机制浅析
    用pygame学习初级python(一) 15.4.19
    openfire+asmack搭建的安卓即时通讯(六) 15.4.16
    openfire+asmack搭建的安卓即时通讯(五) 15.4.12
    openfire+asmack搭建的安卓即时通讯(四) 15.4.10
    openfire+asmack搭建的安卓即时通讯(三) 15.4.9
    openfire+asmack搭建的安卓即时通讯(二) 15.4.9
    openfire+asmack搭建的安卓即时通讯(一) 15.4.7
    无界非阻塞队列ConcurrentLinkedQueue核心源码浅析
    StampedLock主要API实现浅析
  • 原文地址:https://www.cnblogs.com/mengmengxia/p/9148894.html
Copyright © 2011-2022 走看看