daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest Jan 11th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Created on 21-09-2011 by MáRIO CARNEIRO
  2. -- Algoritmos para separação de medicamentos p/ quantidade
  3.  
  4. declare
  5.   type t_l_char is table of char;
  6.   type t_ll_char is table of t_l_char;
  7.  
  8.  
  9.   i integer;
  10.   current_element char;
  11.   current_list  t_l_char;
  12.   lista t_l_char;
  13.   lista_final t_ll_char := t_ll_char();
  14.  
  15.   teste1 t_l_char := t_l_char('a', 'a', 'a', 'a', 'C', 'C', 'C', 'C', 'd', 'd', 'B', 'B', 'x', 'x', 'x');
  16.   teste2 t_l_char := t_l_char('a', 'a', 'a');
  17.   teste3 t_l_char := t_l_char('a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'b', 'X', 'X', 'X', 'c', 'c', 'Y', 'Y', 'Y', 'Y', 'Y');
  18.   teste4 t_l_char := t_l_char('a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'c', 'c', 'd', 'd',  'Y', 'Y', 'Y', 'Y', 'Y');
  19.  
  20.   -- unitário: upper case
  21.   function is_unitario(id char) return boolean is
  22.   begin
  23.     return id = upper(id);
  24.   end;
  25.  
  26.  
  27.   function l_repr(p_lista in t_l_char) return varchar2 is
  28.     temp varchar(100) := '[';
  29.     elem integer;
  30.   begin
  31.     elem := p_lista.first;
  32.     while elem is not null loop
  33.       temp := temp || ' ' || p_lista(elem);
  34.       elem := p_lista.next(elem);
  35.     end loop;
  36.     temp := temp || ' ]';
  37.     return temp;
  38.   end;
  39.  
  40.   function ll_repr(p_llista t_ll_char) return varchar2 is
  41.     temp varchar2(100) := '[';
  42.     elem integer;
  43.   begin
  44.     elem := p_llista.first;
  45.     while elem is not null loop
  46.       temp := temp || ' ' || l_repr(p_llista(elem));
  47.       elem := p_llista.next(elem);
  48.     end loop;
  49.     temp := temp || ' ]';
  50.     return temp;
  51.   end;
  52.  
  53.   -- retira o próximo elemento válido da lista.
  54.   -- retorna true se a lista de saída estiver completa.
  55.  
  56.   function passo_doble(p_lista in out t_l_char, p_current_list in out t_l_char) return boolean is
  57.     element_index int;
  58.    
  59.     -- retorna true ou false, conforme p_candidate possa vir a fazer parte de p_list
  60.     function is_valid_candidate(p_candidate char, p_list t_l_char) return boolean is
  61.       l_limit integer;
  62.       l_count integer := 0;
  63.       l_index integer;
  64.     begin
  65.    
  66.       -- retornar false se p_list estiver cheia.
  67.       if (p_list.count = 4) then
  68.         return false;
  69.       end if;    
  70.      
  71.       -- limite deste medicamento p/receita
  72.       if is_unitario(p_candidate) then
  73.         l_limit := 4;
  74.       else
  75.         l_limit := 2;
  76.       end if;
  77.      
  78.       -- contar quantos elementos já existem na receita
  79.       l_index := p_list.first;
  80.       while l_index is not null loop
  81.         if p_list(l_index) = p_candidate then
  82.           l_count := l_count + 1;
  83.         end if;
  84.         l_index := p_list.next(l_index);
  85.       end loop;
  86.      
  87.       if l_count < l_limit then
  88.         return true;
  89.       else
  90.         return false;
  91.       end if;
  92.      
  93.      
  94.     end;
  95.    
  96.     -- retorna o índice do próximo elemento válido da lista
  97.     -- ou -1 se não for apropriado preencher mais a lista
  98.     function next_valid_element_index(p_source in t_l_char, p_destination in t_l_char) return integer is
  99.       index_elem integer;
  100.       candidate char;
  101.     begin
  102.       index_elem := p_source.first;
  103.       while index_elem is not null loop
  104.         candidate := p_source(index_elem);
  105.         if is_valid_candidate(candidate, p_destination) then
  106.           return index_elem;
  107.         end if;
  108.         index_elem := p_source.next(index_elem);
  109.       end loop;
  110.       return -1;
  111.     end;
  112.    
  113.   begin
  114.  
  115.     element_index := next_valid_element_index(p_lista, p_current_list);
  116.    
  117.     if (element_index = -1) then
  118.       return true;
  119.     end if;
  120.      
  121.     p_current_list.extend(1);
  122.     p_current_list(p_current_list.count) := p_lista(element_index);
  123.    
  124.     p_lista.delete(element_index);
  125.    
  126.     return false;
  127.   end;
  128.  
  129. begin
  130.   ---
  131.   lista := teste2;
  132.   ---
  133.  
  134.   current_list := t_l_char();
  135.   while lista.count > 0 loop
  136.    
  137.    
  138.    
  139.    
  140.     if passo_doble(lista, current_list) then
  141.       lista_final.extend(1);
  142.       lista_final(lista_final.count) := current_list;
  143.       current_list := t_l_char();
  144.     end if;
  145.    
  146.     dbms_output.put_line('Input List: ' || l_repr(lista));
  147.     dbms_output.put_line('Current List: ' || l_repr(current_list));    
  148.     dbms_output.put_line('Final LList: ' || ll_repr(lista_final));
  149.     dbms_output.put_line('');
  150.    
  151.   end loop;
  152.  
  153.   --++ leftovers
  154.   if (current_list.count > 0) then
  155.     lista_final.extend(1);
  156.     lista_final(lista_final.count) := current_list;
  157.     current_list := t_l_char();
  158.    
  159.     dbms_output.put_line('Input List: ' || l_repr(lista));
  160.     dbms_output.put_line('Current List: ' || l_repr(current_list));    
  161.     dbms_output.put_line('Final LList: ' || ll_repr(lista_final));
  162.     dbms_output.put_line('');
  163.   end if;
  164.  
  165.  
  166. end;
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top