Guest User

Untitled

a guest
Apr 16th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.62 KB | None | 0 0
  1. -- Praca s polom
  2. -- Autor: Branislav Blaskovic
  3. -- asi netreba rozsirenia :) snazil som sa
  4.  
  5. function strlen(slovo)
  6.  
  7.     local i;
  8.     local typ;
  9.    
  10.     i = 0;
  11.    
  12.     -- overenie datoveho typu
  13.     typ = type(slovo);
  14.     if typ ~= "string" then
  15.         write("CHYBA: Toto '",slovo,"' nie je string!\n");
  16.         return nil;
  17.     else end;
  18.    
  19.     -- zacnime pocitat
  20.     local sub;
  21.    
  22.     while sub ~= "" do
  23.         i = i + 1;
  24.         sub = substr(slovo, i, i);
  25.     end;
  26.    
  27.     -- posledny prechod
  28.     i = i - 1;
  29.  
  30.     return i;
  31.    
  32. end
  33.  
  34. function array_valid(array, separator)
  35.  
  36.     -- overenie typu matice
  37.     local typ;
  38.     typ = type(array);
  39.     if typ ~= "string" then write("CHYBA: Matica nie je string.\n"); return false; else end;
  40.     -- overenie separatora
  41.     local sep_len;
  42.     local sep_typ;
  43.     sep_len = strlen(separator);
  44.     sep_typ = type(separator);
  45.     if sep_typ ~= "string" then write("CHYBA: Separator nie je string.\n"); return false; else end;
  46.     if sep_len ~= 1 then write("CHYBA: Separator musi mat 1 znak.\n"); return false; else end;
  47.    
  48.     -- vsetko ok
  49.     return true;
  50.    
  51. end
  52.  
  53. function array_print(array, separator, return_len, get_value)
  54.    
  55.     -- return_len == ak nastavime na TRUE, nevypisujeme, ale vraciame velkost pola
  56.    
  57.     -- overenie validnosti
  58.     local valid;
  59.     valid = array_valid(array, separator);
  60.     if valid ~= true then return false; else end;
  61.    
  62.     local pos;
  63.     local array_len;
  64.     local word;
  65.     local i;
  66.     local want_get_value;
  67.     i = 0;
  68.    
  69.     -- chceme ziskat prvok? get_value je number
  70.     local typ_get_value;
  71.     typ_get_value = type(get_value);
  72.     if typ_get_value == "number" then
  73.         want_get_value = true;
  74.     else
  75.         want_get_value = false;
  76.     end;
  77.    
  78.     -- prazdna matica
  79.     array_len = strlen(array);
  80.     if array_len == 0 then return 0; else end;
  81.    
  82.     -- aby to vbehlo do cyklu
  83.     pos = 1;
  84.     pos = find(array, separator);
  85.     -- a ideme vypisovat
  86.     while pos > 0 do
  87.         pos = find(array, separator);
  88.         pos = pos - 1;
  89.         word = substr(array, 1, pos);
  90.         if want_get_value == true then
  91.             if get_value == i then
  92.            
  93.                 return word;
  94.             else end;
  95.         else       
  96.             if return_len ~= true then
  97.                 write(i, ". ", word,"\n");
  98.             else end;
  99.         end;
  100.        
  101.         pos = pos + 2;
  102.         array = substr(array, pos, array_len);
  103.         pos = pos - 1;
  104.         i = i + 1;
  105.     end;
  106.    
  107.     if i == 0 then
  108.         if array ~= "" then
  109.             if return_len ~= true then
  110.                 write(i, ". ", array,"\n");
  111.             else end;
  112.         else end;
  113.     else end;
  114.    
  115.     -- ak chceme velkost pola
  116.     if return_len == true then
  117.        
  118.         if i == 0 then
  119.             if array == "" then
  120.                 return 0;
  121.             else
  122.                 return 1;
  123.             end;
  124.         end;
  125.        
  126.         return i;
  127.        
  128.     else end;
  129.    
  130. end
  131.  
  132. function array_get(array, separator, id)
  133.     -- overenie validnosti
  134.     local valid;
  135.     valid = array_valid(array, separator);
  136.     if valid ~= true then return array; else end;
  137.    
  138.     -- overenie pozicie
  139.     local typ;
  140.     typ = type(id);
  141.     if typ ~= "number" then write("CHYBA: Pozicia nie je cislo!\n"); return array; else end;
  142.     if id < 0 then write("CHYBA: Pozicia nemoze byt mensia ako 0!\n"); return array; else end;
  143.    
  144.     local value;
  145.    
  146.     value = array_print(array, separator, false, id);
  147.    
  148.     return value;
  149.    
  150. end
  151.  
  152. function array_add(array, separator, id, word)
  153.    
  154.     -- overenie validnosti
  155.     local valid;
  156.     valid = array_valid(array, separator);
  157.     if valid ~= true then return array; else end;
  158.    
  159.     local typ;
  160.    
  161.     -- overenie vkladaneho slova
  162.     typ = type(word);
  163.     if typ ~= "string" then write("CHYBA: Nevkladate slovo!\n"); return array; else end;
  164.     -- overenie pozicie
  165.     typ = type(id);
  166.     if typ ~= "number" then write("CHYBA: Pozicia nie je cislo!\n"); return array; else end;
  167.     if id < 0 then write("CHYBA: Pozicia nemoze byt mensia ako 0!\n"); return array; else end;
  168.    
  169.     -- a zaciname
  170.     local pos;
  171.     local array_len;
  172.     local i;
  173.     local before;
  174.     local after;
  175.     local tmp;
  176.     local array_num; -- pocet prvkov
  177.     local want_num;
  178.     local pom;
  179.     want_num = true;
  180.     i = 0;
  181.    
  182.     array_len = strlen(array);
  183.    
  184.     array_num = array_print(array, separator, want_num);
  185.     -- ak mame prazdnu maticu
  186.     if array == "" then
  187.         if id == 0 then return word; else end;
  188.     else end;
  189.     -- ak ma matica 1 prvok a presne tam chcem vlozit, zmenime
  190.     if array_num == 1 then
  191.        
  192.         if id == 0 then
  193.             return word;
  194.         else end;
  195.     else end;
  196.    
  197.     -- vkladanie ZA maticu
  198.     local dec;
  199.     if array_num == 0 then
  200.         dec = id;
  201.         while dec > 0 do
  202.             array = array .. separator;
  203.             dec = dec - 1;
  204.         end;
  205.         array = array .. word;
  206.         return array;
  207.     else
  208.         if (array_num-1) < id then
  209.             dec = id;
  210.             while dec > (array_num-1) do
  211.                 array = array .. separator;
  212.                 dec = dec - 1;
  213.             end;
  214.             array = array .. word;
  215.             return array;
  216.         else end;
  217.     end;
  218.    
  219.     -- aby to vbehlo do cyklu
  220.     pos = 1;
  221.     pos = find(array, separator);
  222.    
  223.     before = "";
  224.     after = "";
  225.  
  226.     -- a ideme pridavat
  227.     while pos > 0 do
  228.         pos = find(array, separator);
  229.        
  230.         -- ulozenie do before
  231.         pos = pos - 1;
  232.         tmp = substr(array, 1, pos);
  233.        
  234.         -- ulozenie do after
  235.         pos = pos + 2;
  236.         array = substr(array, pos, array_len);
  237.         after = array;
  238.        
  239.         -- pre dalsi prechod (overenie konca)
  240.         pos = find(array, separator);
  241.        
  242.         if i == id then
  243.             -- separator nedame na zaciatok
  244.             if i == 0 then
  245.                 before = word;
  246.             else
  247.                 before = before .. separator .. word;
  248.             end;
  249.         else
  250.             -- separator nedame na zaciatok
  251.             if i == 0 then
  252.                 before = tmp;
  253.             else
  254.                 before = before .. separator .. tmp;
  255.             end;
  256.         end;
  257.        
  258.        
  259.         i = i + 1;
  260.     end;
  261.    
  262.     local result;
  263.    
  264.     result = before .. separator .. after;
  265.    
  266.     return result;
  267. end
  268.  
  269. function help()
  270.     write("\n[ HELP ]\n");
  271.     write("0: Vypisat help\n");
  272.     write("1: Vypisat pole\n");
  273.     write("2: Vlozit do pola\n");
  274.     write("3: Ziskat hodnotu z pola\n");
  275.     write("q: Ukoncit program\n");
  276. end
  277.  
  278. function main()
  279.  
  280.     local a;
  281.     local separator;
  282.     local array;
  283.    
  284.     array = "";
  285.  
  286.     local pozicia;
  287.     local slovo;
  288.     local input;
  289.     local typ;
  290.    
  291.     write("Zadajte separator: (1 znak, taky, ktory sa nebude nachadzat v data. Napriklad '@'): ");
  292.     separator = read(1);
  293.    
  294.     a = help();
  295.    
  296.     while 1 do
  297.        
  298.         write("Prikaz $ ");
  299.         input = read("*l");
  300.         write("\n");
  301.        
  302.         -- moznosti
  303.         if input == "1" then
  304.             a = array_print(array, separator);
  305.         else end;
  306.        
  307.         if input == "2" then
  308.            
  309.             write("\nZadajte text: ");
  310.             slovo = read("*l");
  311.             write("Zadajte poziciu: ");
  312.             pozicia = read("*n");
  313.             write(slovo, " - ", pozicia,"\n");
  314.             array = array_add(array, separator, pozicia, slovo);
  315.             write("\n... Vlozene ...\n");
  316.         else end;
  317.        
  318.         if input == "3" then
  319.            
  320.             write("Zadajte poziciu: ");
  321.             pozicia = read("*n");
  322.            
  323.             a = array_get(array, separator, pozicia);
  324.             write("\nNa pozicii ", pozicia," sa nachadza: ", a,"\n");
  325.         else end;
  326.        
  327.         if input == "0" then
  328.             a = help();
  329.         else end;
  330.        
  331.         if input == "q" then
  332.             return 0;
  333.         else end;
  334.        
  335.     end;
  336. end;
Add Comment
Please, Sign In to add comment