[LUA] Chap. 3 Expression and arthimetic

[예제]
--#arthimetic
local tolerance = 10
function isturnback (angle)
  angle = angle % 360
  return (math.abs(angle-180) < tolerance)
 end

 print(isturnback(180))
 
 local tol = 0.17
 function isturn(ang3)
  ang3 = ang3 % (2*math.pi)
  return (math.abs(ang3 - math.pi) < tol)
 end

 print(isturn(math.pi))

 --#>, <
 a = {}; a.x = 1; a.y = 0
 b = {}; b.x = 1; b.y = 0
 c = a

 --#logical
 print(4 and 5)
 print(nil and 13)
 print(false and 13)
 print(4 or 5)
 print(false or 5)

 print(not nil)
 print(not false)
 print(not 0) -- 0 means true
 print(not not 1)
 print(not not nil)

 --#length
 print(a[#a]) --print last value of table a
 a[#a] = nil  --delete last value of table a
 a[#a + 1] = v --add v in the last of table a   table == list ??

 -- 'key' : a['f'], a[4], a[7] ... 'f', 4, 7 is the key

 a={}
 a[1]=1
 a[2]=nil
 a[3]=3
 a[4]=555

 for i=1, 4 do
  print(a[i])
 end

 print(#a)
 print(a[#a])

 b={}
 b[1]=1
 b[2]=nil
 b[3]=5
 b['x'] =3
 b['y'] = 4
 b[10000]=33
 b[10004]=34

 print("b :",#b)

 print(b['x'])

 --#table create
 days = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"}
 print(days[0])
 print(days[1])

 a = {x=10, y=20} -->a={};a.x=10;a.y=20

 w = {x=0, y=0, label="console"}
 x = {math.sin(0), math.sin(1), math.sin(2)}
 w[1] = "another field"
 x.f = w
 print(w["x"])
 print(w[1])
 print(x.f[1])
 w.x = nil

 print(x[3])

 --record?? list??
 polyline = {color="blue",
             thickness=2,
             npoints=4,
             {x=0,y=0},
             {x=-10,y=0},
             {x=0, y=1},
             {x=0,y=1}
            }
 print(polyline["color"])            
 print(polyline[2].x)
 print(polyline[4].y)

 opnames = {["+"]="add", ["-"]="sub", ["*"]="mul", ["/"]="div"}
 i = 20; s = "-"
 a = {[i+0] = s, [i+1] = s..s, [i+2] = s..s..s}
 print(opnames[s])
 print(a[22])

 abc = {[1]="red", [2]="green", [3]="blue",}
 print(abc[1])
 efg = {x=10, y=45; "one", "two", "three"}
 print(efg[2])

 test1={x=3578, y=4}
 test2={"r","g","b"}
 print(test1[2]) --nil
 print(test1[x]) --nil
 print(test1["x"]) --3578
 print(test2[3])
--ex1 나머지연산자 %의 음수처리 :
 for i=-10,10 do
  print(i, i%3)
 end
--ex2 
print(2^3^4)
print(2^-3^4)
--ex3
polynominal = {1, 2, 3, 4, 5}
print("your polynominal is like this:")
print("a_n*x^n + a_(n-1)*x^(n-1) + ... + a_1*x^1+a_0*x^0")
print("n is polynominal order, a is coefficent, x is solution value")
print("input polynominal order:")
n = io.read()
n = n+1
print("input solution value x")
x_val = io.read()
sum_result = 0
coeflist={}
for i = 1, n do
  print("input", i-1 ,"th polynominal coefficent")
  coef = io.read()
  coeflist[i] = coef
  sum_result = x_val^(i-1) * coeflist[i] + sum_result
  --print(coeflist[i])  
 end

 print(sum_result)






[결과]

true
true
5
nil
false
4
5
true
true
false
true
false
nil
1
nil
3
555
4
555
b : 1
3
nil
sunday
0
another field
another field
0.90929742682568
blue
-10
1
sub
---
red
two
nil
nil
3578
b
-10 2
-9 0
-8 1
-7 2
-6 0
-5 1
-4 2
-3 0
-2 1
-1 2
0 0
1 1
2 2
3 0
4 1
5 2
6 0
7 1
8 2
9 0
10 1
2.4178516392293e+024
4.1359030627651e-025
your polynominal is like this:
a_n*x^n + a_(n-1)*x^(n-1) + ... + a_1*x^1+a_0*x^0
n is polynominal order, a is coefficent, x is solution value
input polynominal order:
4
input solution value x
2
input 0 th polynominal coefficent
1
input 1 th polynominal coefficent
2
input 2 th polynominal coefficent
3
input 3 th polynominal coefficent
4
input 4 th polynominal coefficent
5
129



[예제]
sunday = "monday"; monday = "sunday"
t = {sunday = "monday", [sunday] = monday}
print(t.sunday, t[sunday], t[t.sunday])
print(t[monday])
print(t["monday"])



[결과]

monday sunday sunday
monday
sunday
[LUA] Chap. 3 Expression and arthimetic [LUA] Chap. 3 Expression and arthimetic Reviewed by kukanuc on 3월 07, 2019 Rating: 5

댓글 없음:

Powered by Blogger.