Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Praca s polom
- -- Autor: Branislav Blaskovic
- -- asi netreba rozsirenia :) snazil som sa
- function strlen(slovo)
- local i;
- local typ;
- i = 0;
- -- overenie datoveho typu
- typ = type(slovo);
- if typ ~= "string" then
- write("CHYBA: Toto '",slovo,"' nie je string!\n");
- return nil;
- else end;
- -- zacnime pocitat
- local sub;
- while sub ~= "" do
- i = i + 1;
- sub = substr(slovo, i, i);
- end;
- -- posledny prechod
- i = i - 1;
- return i;
- end
- function array_valid(array, separator)
- -- overenie typu matice
- local typ;
- typ = type(array);
- if typ ~= "string" then write("CHYBA: Matica nie je string.\n"); return false; else end;
- -- overenie separatora
- local sep_len;
- local sep_typ;
- sep_len = strlen(separator);
- sep_typ = type(separator);
- if sep_typ ~= "string" then write("CHYBA: Separator nie je string.\n"); return false; else end;
- if sep_len ~= 1 then write("CHYBA: Separator musi mat 1 znak.\n"); return false; else end;
- -- vsetko ok
- return true;
- end
- function array_print(array, separator, return_len, get_value)
- -- return_len == ak nastavime na TRUE, nevypisujeme, ale vraciame velkost pola
- -- overenie validnosti
- local valid;
- valid = array_valid(array, separator);
- if valid ~= true then return false; else end;
- local pos;
- local array_len;
- local word;
- local i;
- local want_get_value;
- i = 0;
- -- chceme ziskat prvok? get_value je number
- local typ_get_value;
- typ_get_value = type(get_value);
- if typ_get_value == "number" then
- want_get_value = true;
- else
- want_get_value = false;
- end;
- -- prazdna matica
- array_len = strlen(array);
- if array_len == 0 then return 0; else end;
- -- aby to vbehlo do cyklu
- pos = 1;
- pos = find(array, separator);
- -- a ideme vypisovat
- while pos > 0 do
- pos = find(array, separator);
- pos = pos - 1;
- word = substr(array, 1, pos);
- if want_get_value == true then
- if get_value == i then
- return word;
- else end;
- else
- if return_len ~= true then
- write(i, ". ", word,"\n");
- else end;
- end;
- pos = pos + 2;
- array = substr(array, pos, array_len);
- pos = pos - 1;
- i = i + 1;
- end;
- if i == 0 then
- if array ~= "" then
- if return_len ~= true then
- write(i, ". ", array,"\n");
- else end;
- else end;
- else end;
- -- ak chceme velkost pola
- if return_len == true then
- if i == 0 then
- if array == "" then
- return 0;
- else
- return 1;
- end;
- end;
- return i;
- else end;
- end
- function array_get(array, separator, id)
- -- overenie validnosti
- local valid;
- valid = array_valid(array, separator);
- if valid ~= true then return array; else end;
- -- overenie pozicie
- local typ;
- typ = type(id);
- if typ ~= "number" then write("CHYBA: Pozicia nie je cislo!\n"); return array; else end;
- if id < 0 then write("CHYBA: Pozicia nemoze byt mensia ako 0!\n"); return array; else end;
- local value;
- value = array_print(array, separator, false, id);
- return value;
- end
- function array_add(array, separator, id, word)
- -- overenie validnosti
- local valid;
- valid = array_valid(array, separator);
- if valid ~= true then return array; else end;
- local typ;
- -- overenie vkladaneho slova
- typ = type(word);
- if typ ~= "string" then write("CHYBA: Nevkladate slovo!\n"); return array; else end;
- -- overenie pozicie
- typ = type(id);
- if typ ~= "number" then write("CHYBA: Pozicia nie je cislo!\n"); return array; else end;
- if id < 0 then write("CHYBA: Pozicia nemoze byt mensia ako 0!\n"); return array; else end;
- -- a zaciname
- local pos;
- local array_len;
- local i;
- local before;
- local after;
- local tmp;
- local array_num; -- pocet prvkov
- local want_num;
- local pom;
- want_num = true;
- i = 0;
- array_len = strlen(array);
- array_num = array_print(array, separator, want_num);
- -- ak mame prazdnu maticu
- if array == "" then
- if id == 0 then return word; else end;
- else end;
- -- ak ma matica 1 prvok a presne tam chcem vlozit, zmenime
- if array_num == 1 then
- if id == 0 then
- return word;
- else end;
- else end;
- -- vkladanie ZA maticu
- local dec;
- if array_num == 0 then
- dec = id;
- while dec > 0 do
- array = array .. separator;
- dec = dec - 1;
- end;
- array = array .. word;
- return array;
- else
- if (array_num-1) < id then
- dec = id;
- while dec > (array_num-1) do
- array = array .. separator;
- dec = dec - 1;
- end;
- array = array .. word;
- return array;
- else end;
- end;
- -- aby to vbehlo do cyklu
- pos = 1;
- pos = find(array, separator);
- before = "";
- after = "";
- -- a ideme pridavat
- while pos > 0 do
- pos = find(array, separator);
- -- ulozenie do before
- pos = pos - 1;
- tmp = substr(array, 1, pos);
- -- ulozenie do after
- pos = pos + 2;
- array = substr(array, pos, array_len);
- after = array;
- -- pre dalsi prechod (overenie konca)
- pos = find(array, separator);
- if i == id then
- -- separator nedame na zaciatok
- if i == 0 then
- before = word;
- else
- before = before .. separator .. word;
- end;
- else
- -- separator nedame na zaciatok
- if i == 0 then
- before = tmp;
- else
- before = before .. separator .. tmp;
- end;
- end;
- i = i + 1;
- end;
- local result;
- result = before .. separator .. after;
- return result;
- end
- function help()
- write("\n[ HELP ]\n");
- write("0: Vypisat help\n");
- write("1: Vypisat pole\n");
- write("2: Vlozit do pola\n");
- write("3: Ziskat hodnotu z pola\n");
- write("q: Ukoncit program\n");
- end
- function main()
- local a;
- local separator;
- local array;
- array = "";
- local pozicia;
- local slovo;
- local input;
- local typ;
- write("Zadajte separator: (1 znak, taky, ktory sa nebude nachadzat v data. Napriklad '@'): ");
- separator = read(1);
- a = help();
- while 1 do
- write("Prikaz $ ");
- input = read("*l");
- write("\n");
- -- moznosti
- if input == "1" then
- a = array_print(array, separator);
- else end;
- if input == "2" then
- write("\nZadajte text: ");
- slovo = read("*l");
- write("Zadajte poziciu: ");
- pozicia = read("*n");
- write(slovo, " - ", pozicia,"\n");
- array = array_add(array, separator, pozicia, slovo);
- write("\n... Vlozene ...\n");
- else end;
- if input == "3" then
- write("Zadajte poziciu: ");
- pozicia = read("*n");
- a = array_get(array, separator, pozicia);
- write("\nNa pozicii ", pozicia," sa nachadza: ", a,"\n");
- else end;
- if input == "0" then
- a = help();
- else end;
- if input == "q" then
- return 0;
- else end;
- end;
- end;
Add Comment
Please, Sign In to add comment