a = 20 b = 10 c = 15 d = 5 e = (a + b) * c / d;-- ( 30 * 15 ) / 5 print("Value of (a + b) * c / d is :",e ) e = ((a + b) * c) / d; -- (30 * 15 ) / 5 print("Value of ((a + b) * c) / d is :",e ) e = (a + b) * (c / d);-- (30) * (15/5) print("Value of (a + b) * (c / d) is :",e ) e = a + (b * c) / d; -- 20 + (150/5) print("Value of a + (b * c) / d is :",e )
a = 21 b = 10 if( a == b ) then print("Line 1 - a is equal to b" ) else print("Line 1 - a is not equal to b" ) end if( a ~= b ) then print("Line 2 - a is not equal to b" ) else print("Line 2 - a is equal to b" ) end if ( a < b ) then print("Line 3 - a is less than b" ) else print("Line 3 - a is not less than b" ) end if ( a > b ) then print("Line 4 - a is greater than b" ) else print("Line 5 - a is not greater than b" ) end -- Lets change value of a and b a = 5 b = 20 if ( a <= b ) then print("Line 5 - a is either less than or equal to b" ) end if ( b >= a ) then print("Line 6 - b is either greater than or equal to b" ) end
a = 21 b = 10 c = a + b print("Line 1 - Value of c is ", c ) c = a - b print("Line 2 - Value of c is ", c ) c = a * b print("Line 3 - Value of c is ", c ) c = a / b print("Line 4 - Value of c is ", c ) c = a % b print("Line 5 - Value of c is ", c ) c = a^2 print("Line 6 - Value of c is ", c ) c = -a print("Line 7 - Value of c is ", c )
print(type("What is my type")) --> string t = 10 print(type(5.8*t)) --> number print(type(true)) --> boolean print(type(print)) --> function print(type(nil)) --> nil print(type(type(ABC))) --> string
-- Variable definition: local a, b -- Initialization a = 10 b = 30 print("value of a:", a) print("value of b:", b) -- Swapping of variables b, a = a, b print("value of a:", a) print("value of b:", b) f = 70.0/3.0 print("value of f", f)
local sentText = "abc" function splitString(s) local t = {} for i = 1,s:len() do table.insert(t, s:sub(i,i)) end return t end function toByteForm(c) -- Izveido dotā burta baita reprezentaciju local byteString = "00000000" local i = c:byte() if i >= 128 then byteString = "10000000" i = i - 128 end if i >= 64 then byteString = byteString:sub(1,1)..'1000000' i = i - 64 end if i >= 32 then byteString = byteString:sub(1,2)..'100000' i = i - 32 end if i >= 16 then byteString = byteString:sub(1,3)..'10000' i = i - 16 end if i >= 8 then byteString = byteString:sub(1,4)..'1000' i = i - 8 end if i >= 4 then byteString = byteString:sub(1,5)..'100' i = i - 4 end if i >= 2 then byteString = byteString:sub(1,6)..'10' i = i - 2 end if i >= 1 then byteString = byteString:sub(1,7)..'1' end return byteString end function printTable(t,indent) -- tiek drukāta tabula kopā ar tās iespējām. Tiek izmantota, lai pārbaudītu tabulu struktūru, kas rodas pēc gramatikas pieshkirshanas for k,v in pairs(t) do if type(v) == "table" then s = "" for i = 1, indent do s = s.." " end print (s..k..": ") printTable(v,indent+4) else s = "" for i = 1, indent do s = s.." " end print(s..k..":\t"..v) end end end --local t = splitString(sentText) --for i,c in ipairs(t) do t[i] = toByteForm(c) end --printTable(t,0) function toCodeword(s) --s burts ir baita attēlojums local codeword = "__"..s:sub(1,1).."_"..s:sub(2,4).."_"..s:sub(5,8) local count1 = 0 local count2 = 0 local count3 = 0 local count4 = 0 if codeword:sub(3,3) == "1" then count1, count2 = count1 + 1, count2 + 1 end if codeword:sub(5,5) == "1" then count1, count3 = count1 + 1, count3 + 1 end if codeword:sub(6,6) == "1" then count3, count2 = count3 + 1, count2 + 1 end if codeword:sub(7,7) == "1" then count1, count2, count3 = count1 + 1, count2 + 1, count3 + 1 end if codeword:sub(9,9) == "1" then count1, count4 = count1 + 1, count4 + 1 end if codeword:sub(10,10) == "1" then count4, count2 = count4 + 1, count2 + 1 end if codeword:sub(11,11) == "1" then count1, count2, count4 = count1 + 1, count2 + 1, count4 + 1 end if codeword:sub(12,12) == "1" then count4, count3 = count4 + 1, count3 + 1 end count1 = count1 % 2 count2 = count2 % 2 count3 = count3 % 2 count4 = count4 % 2 codeword = count1..count2..s:sub(1,1)..count3..s:sub(2,4)..count4..s:sub(5,8) return codeword end function toTransmission(t) local transmissionString = "" for i = 1, 12 do for j, codeword in ipairs(t) do transmissionString = transmissionString..codeword:sub(i,i) end --transmissionString = transmissionString.." " end return transmissionString end function burstError(s) --ieviesh burstError random pozīcijai un garumam local firstErr = math.random(s:len() - (s:len()/6)) --local lastErr = math.random(s:len() - firstErr) + firstErr local lastErr = math.random(s:len()/6) + firstErr --burstError nav garāks par 2n, kur n ir pārsūtītie koda vārdi print ("Transmisijas pirmā un pēdējā pārveidotā bita indekss:") print (firstErr, lastErr) return s:sub(1,firstErr-1)..flipBits(s:sub(firstErr, lastErr))..s:sub(lastErr+1) end function flipBits(s) -- s'ā apgriež visus bitus local s2 = "" for i = 1, s:len() do if s:sub(i,i) == "0" then s2 = s2.."1" else s2 = s2.."0" end end return s2 end function transmissionToCodewords(s) local t = {} local cwCount = s:len() / 12 for i = 1, cwCount do table.insert(t,"") end for i = 1, s:len() do local n = i % cwCount if n == 0 then n = cwCount end t[n] = t[n]..s:sub(i,i) end return t end function checkErrors(s) --s is Hamming codeword local errorLocation = 0 local ones = {0,0,0,0} if s:sub(1,1) == '1' then ones[1] = ones[1] + 1 end if s:sub(2,2) == '1' then ones[2] = ones[2] + 1 end if s:sub(3,3) == '1' then ones[1], ones[2] = ones[1] + 1, ones[2] + 1 end if s:sub(4,4) == '1' then ones[3] = ones[3] + 1 end if s:sub(5,5) == '1' then ones[1], ones[3] = ones[1] + 1, ones[3] + 1 end if s:sub(6,6) == '1' then ones[2], ones[3] = ones[2] + 1, ones[3] + 1 end if s:sub(7,7) == '1' then ones[1], ones[2], ones[3] = ones[1] + 1, ones[2] + 1, ones[3] + 1 end if s:sub(8,8) == '1' then ones[4] = ones[4] + 1 end if s:sub(9,9) == '1' then ones[1], ones[4] = ones[1] + 1, ones[4] + 1 end if s:sub(10,10) == '1' then ones[2], ones[4] = ones[2] + 1, ones[4] + 1 end if s:sub(11,11) == '1' then ones[1], ones[2], ones[4] = ones[1] + 1, ones[2] + 1, ones[4] + 1 end if s:sub(12,12) == '1' then ones[3], ones[4] = ones[3] + 1, ones[4] + 1 end if ones[1] % 2 == 1 then errorLocation = errorLocation + 1 end if ones[2] % 2 == 1 then errorLocation = errorLocation + 2 end if ones[3] % 2 == 1 then errorLocation = errorLocation + 4 end if ones[4] % 2 == 1 then errorLocation = errorLocation + 8 end if errorLocation ~= 0 then if s:sub(errorLocation, errorLocation) == '0' then return s:sub(1,errorLocation-1)..'1'..s:sub(errorLocation+1) end if s:sub(errorLocation, errorLocation) == '1' then return s:sub(1,errorLocation-1)..'0'..s:sub(errorLocation+1) end end return s end function discardChecksums(s) return s:sub(3,3)..s:sub(5,7)..s:sub(9,12) end function byteFormToLetter(s) local c = 0; if s:sub(1,1) == '1' then c = c + 128 end if s:sub(2,2) == '1' then c = c + 64 end if s:sub(3,3) == '1' then c = c + 32 end if s:sub(4,4) == '1' then c = c + 16 end if s:sub(5,5) == '1' then c = c + 8 end if s:sub(6,6) == '1' then c = c + 4 end if s:sub(7,7) == '1' then c = c + 2 end if s:sub(8,8) == '1' then c = c + 1 end return string.char(c) end function combineString(t) local s = "" for i, letter in ipairs(t) do s = s..letter end return s end --printTable(transmissionToCodewords("aaabbbcccdddeeefffggghhhiiijjjkkklll"),0) math.randomseed(os.time()) print ("Sūtāmā ziņa:") print (sentText) print ("\f") local t = splitString(sentText) for i,c in ipairs(t) do t[i] = toByteForm(c) end print ("\f") print ("Katra rindiņa ir ziņojuma baits:") printTable(t,0) print ("\f") print ("Baiti pārveidoti uz Hamming kodu:") for i,c in ipairs(t) do t[i] = toCodeword(c) end printTable(t,0) print ("\f") print ("Bitu pārraide (pārraušanas kļūdu apstrādei):") local s = toTransmission(t) print (s) print ("\f") s = burstError(s) print ("\f") print ("Bitu secība pēc nejauša garuma pārslēgšanās kļūdas ieviešanas [0; 2n], kur n ir sākotnējais ziņojuma garums baitos:") print (s) local t2 = transmissionToCodewords(s) print ("\f") print ("Bitu secība sadalīta Hamminga kodu:") printTable(t2,0) for i, codeword in ipairs(t2) do t2[i] = checkErrors(codeword) end print ("\f") print ("Kods pēc labojumiem:") printTable(t2,0) for i, codeword in ipairs(t2) do t2[i] = discardChecksums(codeword) end print ("\f") print ("Ziņojuma baiti pēc visu pārbaudes bitu norakstīšanas:") printTable(t2,0) for i, byteForm in ipairs(t2) do t2[i] = byteFormToLetter(byteForm) end print ("\f") print ("Saņemtais ziņojums:") print (combineString(t2))
a = "7n5m4m2p1d6" function trenne(s, i, j) b = string.sub(s,i,j) return b end i = 1 c = 0 while i < string.len(a) do x1 = tonumber(trenne(a, i, i)) x2 = tonumber(trenne(a, i+2, i+2)) if i < 3 then if trenne(a, i+1, i+1) == "p" then c = x1 + x2 elseif trenne(a, i+1, i+1) == "m" then c = x1 - x2 elseif trenne(a, i+1, i+1) == "n" then c = x1 * x2 elseif trenne(a, i+1, i+1) == "d" then c = x1 / x2 else print("invalid operation") end i = i +3 else x1 = c x2 = tonumber(trenne(a,i+1,i+1)) if trenne(a, i, i) == "p" then c = x1 + x2 elseif trenne(a, i, i) == "m" then c = x1 - x2 elseif trenne(a, i, i) == "n" then c = x1 * x2 elseif trenne(a, i, i) == "d" then c = x1 / x2 else print("invalid operation") end i = i +2 end end print(c)
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more