you are in: codestackercodes [RSS] → tag: anonymous [RSS]

estrutura de dados Delicious Email

exercícios sobre pilhas e filas

show/hide lines
   1  public class exercicio_pilha_fila {
   2  
   3      int topo = 0;
   4      int maxPilha = 0;
   5      int pilha[] = new int[maxPilha];
   6      int comeco = 0;
   7      int fim = 0;
   8      int total = 0;
   9      int maxFila = 0;
  10      int[] fila = new int[maxFila];
  11      
  12  	// 15/03/2010
  13      // Desenvolva um método para remover uma determinada quantidade de dados
  14   	// em uma pilha estática. Caso nao haja inserida, remova o possivel
  15      public void remove() {
  16          if (topo == 0) {
  17              System.out.println("Pilha Vazia");
  18          } else {
  19              int qtde = Leitura.readInteger();
  20              if (qtde > topo) {
  21                  System.out.println("Quantidade: " + (qtde - topo));
  22                  topo = 0;
  23              } else {
  24                  topo = topo - qtde;
  25              }
  26          }
  27      }
  28  
  29      // 15/03/2010
  30  	// Desenvolva um método que através de uma ED tipo pilha verifique
  31  	// se uma palavra é palíndroma. Ex.: ovo, ama, asa
  32      public void Palavra() {
  33  		if (topo == 0) {
  34  			System.out.println("Lista Vazia");
  35  		} else {
  36  			int cont = topo - 1;
  37  			int aux = 0;
  38  			while ((pilha[aux].equals(pilha[cont])) && (cont > aux)) {
  39  				cont--;
  40  				aux++;
  41  			}
  42  			
  43  			if (aux > cont) {
  44  				System.out.println("Palíndroma");
  45  			} else {
  46  				System.out.println("Não palíndroma");
  47  			}
  48  		}
  49      }
  50  
  51      // 22/03/2010
  52  	// Exemplo do caderno: Insere fila
  53      public void InsereFila() {
  54          if (total == maxFila) {
  55              System.out.println("fila cheia");
  56          } else {
  57              if (fim == maxFila) {
  58                  fim = 0;
  59                  int tes = Leitura.readInteger(" Digite algo::");
  60                  fila[fim] = tes;
  61                  fim++;
  62                  total++;
  63              }
  64          }
  65      }
  66  
  67      // 22/03/2010
  68  	// Exemplo do caderno: Remove fila
  69      public void RemovaFila() {
  70          if (total == 0) {
  71              System.out.println("Fila vazia");
  72          } else {
  73              if (comeco == maxFila) {
  74                  comeco = 0;
  75              }
  76  			comeco++;
  77              total--;
  78          }
  79      }
  80  
  81      // 24/03/2010 
  82  	// Considere 2 estruturas de fila estática. Verifica se 
  83  	// ambas sequências de remoção e quantidade são iguais
  84      public void Verifica_Igualdade() {
  85  		if (total1 != total2) {
  86  			System.out.println("Tamanhos diferentes");
  87  		} else {
  88  			if (com1 != max) {
  89  				int aux1 = com1;
  90  			} else {
  91  				int aux1 = 0;
  92  			}
  93  			
  94  			if (com2 != max) {
  95  				int aux2 = com2;
  96  			} else {
  97  				int aux2 = 0;
  98  			}
  99  			
 100  			int cont = 0;
 101  			while ((cont != total1) && (fila1[aux1] == fila2[aux2])) {
 102  				aux1++;
 103  				aux2++;
 104  				
 105  				if (aux1 == max) {
 106  					aux1 = 0;
 107  				}
 108  				if (aux2 == max) {
 109  					aux2 = 0;
 110  				}
 111  				cont++;
 112  			}
 113  			
 114  			if (cont == total) {
 115  				System.out.println("Filas iguais");
 116  			} else {
 117  				System.out.println("Filas diferentes");
 118  			}
 119  		}
 120      }
 121  
 122      // 24/03/2010 
 123  	// Desenvolva um método para verificar qual posição
 124  	// da fila um determinado valor foi encontrado
 125      public void LocalizaPosicao() {
 126          if (total == 0) {
 127              System.out.println("Fila vazia");
 128          } else {
 129              int valor = Leitura.readInteger("Digite valor a ser localizado: ");
 130              if (comeco == maxFila) {
 131                  int aux = 0;
 132              } else {
 133                  int aux = comeco;
 134                  int cont = 0;
 135                  while ((cont != total) && (valor != fila[aux])) {
 136                      aux++;
 137                      cont++;
 138                      if (aux == maxFila) {
 139                          System.out.println("Não encontrado");
 140                      } else {
 141                          System.out.println("Encontrado:  " + (cont + 1));
 142                      }
 143                  }
 144              }
 145          }
 146      }
 147  
 148      // 24/03/2010     
 149  	// Crie uma ED fila que ao receber a multiplicação dos valores cadastrados
 150  	// em 2 filas estáticas que possuem a mesma quantidade de dados
 151      public void Multiplica() {
 152          int maxFila2 = 0;
 153          int comeco2 = 0;
 154  
 155          if (total == 0) {
 156              System.out.println("fila vazia");
 157          } else {
 158              int[] fila3 = new int[total];
 159              if (comeco == maxFila) {
 160                  int aux = 0;
 161              } else {
 162                  int aux = comeco;
 163              }
 164              if (comeco2 == maxFila2) {
 165                  int aux2 = 0;
 166              } else {
 167                  int aux2 = comeco2;
 168                  int cont = 0;
 169                  while (cont != total) {
 170                      fila3[fim3] = (fila[aux] * fila2[aux2]);
 171                      aux++;
 172                      aux2++;
 173                      fim3++;
 174                      total3++;
 175                      if (aux == maxFila) {
 176                          aux = 0;
 177                      }
 178                      if (aux2 == maxFila) {
 179                          aux2 = 0;
 180                          cont++;
 181                      }
 182                  }
 183              }
 184          }
 185      }
 186  
 187      // Exemplo do caderno
 188  	// Desenvolva um método para apresentar o primeiro e o ultimo elemento da fila.
 189      public void Apresenta() {
 190          if (total == 0) {
 191              System.out.println("Fila vazia");
 192          } else {
 193              System.out.println("Começo: " + fila[comeco] + " último: " + fila[fim - 1]);
 194          }
 195      }
 196  
 197      // 31/03/2010 
 198  	// Remova os elementos de uma pilha e uma fila simultaneamente,
 199  	// enquanto ambos estruturas forem iguais
 200      public void RemovePilhaFila() {
 201          if ((topo == 0) || (total == 0)) {
 202              System.out.println("Fila ou pilha vazias");
 203          } else {
 204              while ((total > 0) && (topo > 0) && (pilha[topo - 1] == fila[comeco])) {
 205                  topo--;
 206                  comeco++;
 207                  total--;
 208  
 209                  if (comeco == maxFila) {
 210                      comeco = 0;
 211                  }
 212              }
 213          }
 214      }
 215  
 216      // 31/03/2010 
 217      // Considere uma agência de viagens, a qual trabalha com
 218   	// fila de espera. Considere o cliente cod 9783. Verifique 
 219  	// em 2 filas, as quais este cliente esta cadastrado, qual
 220  	// apresenta a maior possibilidade de conseguir a passagem
 221      public void Localiza() {
 222  		if (comeco1 == max) {
 223  			int aux1 = 0;
 224  		} else {
 225  			int aux1 = comeco1;
 226  		}
 227  		
 228  		if (comeco2 == max) {
 229  			int aux2 = 0;
 230  		} else {
 231  			int aux2 = comeco2;
 232  		}
 233  		
 234  		int cont1 = 0;
 235  		int cont2 = 0;
 236  		
 237  		while (fila1[aux1] != 9783) {
 238  			cont1++;
 239  			aux1++;
 240  			if (aux1 == max) {
 241  				aux1 = 0;
 242  			}
 243  		}
 244  		
 245  		while (fila2[aux2] != 9783) {
 246  			cont2++;
 247  			aux2++;
 248  			if (aux2 == max) {
 249  				aux2 = 0;
 250  			}
 251  		}
 252  		
 253  		if (cont1 > cont2) {
 254  			System.out.println("Fila 1 menor");
 255  		} else if (cont2 < cont1) {
 256  			System.out.println ("Fila 2 menor");
 257  		} else {
 258  			System.out.println ("Tanto faz");
 259  		}
 260      }
 261  
 262      // 31/03/2010 
 263      // Considere uma pilha a qual foi inserida de forma intermediária valores
 264  	// positivos e negativos altere a ED de forma a possuir somente os 
 265  	// valores positivos. Não há a necessidade de manter a pilha original
 266      public void PilhaPositiva() {
 267          if (topo == 0) {
 268              System.out.println("Pilha esta vazia!!");
 269          } else {
 270  			int PilhaN = new Int[(topo/2) + 1];
 271  			int TopoN = 0;
 272  			
 273  			while (TopoN > 0) {
 274  				if (pilha[topo - 1] > 0) {
 275  					PilhaN[TopoN] = pilha[topo -1];
 276  					TopoN++;
 277  				}
 278  				topo--;
 279  			}
 280  		}
 281      }
 282  
 283  	// Outro semestre
 284      // Desenvolva um método para remover com exceção do 
 285  	// primeiro, todos os outros elementos da fila
 286      public void RestaUm() {
 287          if (total == 0) {
 288              System.out.println("fila vazia");
 289          } else {
 290              total = 1;
 291              comeco = fim - 1;
 292          }
 293      }
 294  
 295      // Outro semestre
 296  	// Crie um método para apresentar todos os dados
 297  	// inseridos em uma fila (do primeiro ao ultimo)
 298      public void ApresentaTodos() {
 299          if (total == 0) {
 300              System.out.println("Fila vazia");
 301          } else {
 302              int aux = comeco;
 303              int cont = 0;
 304  
 305              while (cont != total) {
 306                  if (aux == maxFila) {
 307                      aux = 0;
 308  				}
 309                  System.out.println("Retorno:  " + fila[aux]);
 310                  aux++;
 311                  cont++;
 312              }
 313          }
 314      }
 315  
 316  	// Outro semestre
 317  	// Se pilha e fila são formas de listas por que existem especifidades para ambas?
 318  	// Porque existem situações computacionais que exigem restrições.
 319  	// Ex. Pilha: CTRL + Z, voltar do browser, recurvidade.
 320  	// Ex. Fila: Impressora
 321  
 322      // Outro semestre
 323  	// Desenvolva um método para preencher todas alocacoes vazias 
 324  	// de uma ED tipo pilha com um determinado valor digitado pelo usuário
 325      public void PreencherPilha() {
 326          if (total == maxPilha) {
 327              System.out.println("Pilha vazia");
 328          } else {
 329              int texto = Leitura.readInteger(" Digite algo::");
 330  
 331              while (topo < maxPilha) {
 332                  pilha[topo] = texto;
 333                  topo++;
 334              }
 335          }
 336      }
 337  
 338      // Outro semestre
 339  	// Crie um método para realizar o backup de uma lista ED tipo fila
 340      public void BackupFilaOutro() {
 341          if (total == 0) {
 342              System.out.println("Fila vazia");
 343          } else {
 344              int filaBackup[] = new int[total];
 345              int aux = comeco;
 346              int totalBackup = 0;
 347          	int fimBackup = 0;
 348              
 349  			while (totalBackup != total) {
 350                  filaBackup[totalBackup] = fila[aux];
 351                  totalBackup++;
 352                  aux++;
 353                  fimBackup++;
 354  
 355                  if (aux == maxFila) {
 356                      aux = 0;
 357                  }
 358                  int comecoBackup = 0;
 359              }
 360          }
 361      }
 362  
 363      // Outro semestre
 364  	// Desenvolva um método para apresentar na sequência de remoção
 365  	// todos os dados inseridos em uma ED tipo fila estática
 366      public void apresentaFila() {
 367          if (total == 0) {
 368              System.out.println("Fila vazia");
 369          } else {
 370              int cont = comeco;
 371              int aux = 0;
 372  
 373              while (aux != total) {
 374                  if (cont == maxFila) {
 375                      cont = 0;
 376                      System.out.println("Apresenta Fila" + fila[cont]);
 377                      cont++;
 378                      aux++;
 379                  }
 380              }
 381          }
 382      }
 383  }
created by anonymous — 06 April 2010 — get a short url — tags: java embed

trabalho de criptografia do michel Delicious Email

show/hide lines
   1  public class trabalho {
   2  	
   3  	public static int gerencia_chave(String chave) { // retorna o ASCII do menor caracter da chave
   4  		
   5  		char[] curinga = chave.toCharArray();
   6  		
   7  		int min = 256;
   8  		
   9  		for (int k=0; k<curinga.length; k++) {
  10  			if ((int) curinga[k] < min) { 
  11  				min = curinga[k];
  12  			}
  13  		}
  14  		
  15  		return min;
  16  		
  17  	}
  18  	
  19  	public static String inverte(String string) { // inverte a string de trás pra frente
  20  	    int i, len = string.length();
  21  	    StringBuffer dest = new StringBuffer(len);
  22  
  23  	    for (i = (len - 1); i >= 0; i--){
  24  	    	dest.append(string.charAt(i));
  25  	    }
  26  	    	
  27  	    return dest.toString();
  28  	  }
  29  	
  30  	public static void criptografa (String string, String chave) {
  31  
  32  		char[] lista = string.toCharArray();
  33  		String holder = "";
  34  		
  35  		for (int i = 0; i<lista.length; i++){
  36  			
  37  			int novochar = (int) lista[i] + gerencia_chave(chave) - 65; 
  38  
  39  			holder += (char) novochar;
  40  		}
  41  		System.out.println(string + " = " + inverte(holder));
  42  	}
  43  	
  44  	public static void descriptografa (String string, String chave) {
  45  
  46  		char[] lista = string.toCharArray();
  47  		String holder = "";
  48  		
  49  		for (int i = 0; i<lista.length; i++){
  50  			
  51  			int novochar = (int) lista[i] - gerencia_chave(chave) + 65; 
  52  
  53  			holder += (char) novochar;
  54  		}
  55  		System.out.println(string + " = " + inverte(holder));
  56  		
  57  	}
  58  	
  59  	public static void main(String args[]) {
  60  		
  61  		criptografa("Computador", "Dinei");
  62  		
  63  		descriptografa("urgdwxsprF", "Dinei");
  64  		
  65  		criptografa("Computação", "Dinei");
  66  		
  67  		descriptografa("ræêdwxsprF", "Dinei");
  68  		
  69  		criptografa("Programador", "Dinei");
  70  		
  71  		descriptografa("urgdpdujruS", "Dinei");
  72  		
  73  		criptografa("Programação", "Dinei");
  74  		
  75  		descriptografa("ræêdpdujruS", "Dinei");
  76  		
  77  		criptografa("ABCDEFGHIJKLMNOPQRSTUVWXYZ ÁÉÍÓÚÂÊÃÕ 0123456789 abcdefghijklmnopqrstuvwxyz áéíóúâêãõ", "Dinei"); // teste básico: não retorna nenhum caracter não imprimível
  78  	}
  79  }
created by anonymous — 10 May 2009 — get a short url embed

Tamanho mínimo da DIV Delicious Email

show/hide lines
   1  #container {
   2    min-height: 500px; /* isso o FF e outros conseguem exergar e aumentam quando necessário o tamanho da div */
   3  }
   4  html>body #container {height:auto;height:500px;} /* mas o IE não lê o min-height. E aumenta a div com o height. */
created by anonymous — 09 July 2008 — get a short url — tags: css div min-height embed

test Delicious Email

show/hide lines
   1  class Teste
   2    def teste
   3      "teste"
   4      Constant
   5    end
   6  end
created by anonymous — 06 July 2008 — get a short url — tags: ruby embed

resize window bookmarklet Delicious Email

create a link with this:

show/hide lines
   1  javascript: resizeTo(800,600);
   2  
   3  javascript: resizeTo(1024,768);
created by anonymous — 05 July 2008 — get a short url — tags: bookmarklet javascript embed

resize image Delicious Email

show/hide lines
   1  <?
   2  ////////////////////////////////////////////////////////////////////////////////////
   3  // generate a croped-image from a picture
   4  // source: http://mediumexposure.com/techblog/smart-image-resizing-while-preserving-transparency-php-and-gd-library
   5  // resizes a image, without crop
   6  // usage: resizeImage("test/6.jpg", "540", "720", true, "test/output.jpg")
   7  ////////////////////////////////////////////////////////////////////////////////////
   8  
   9  function resizeImage( $file, $width = 0, $height = 0, $proportional = false, $output = 'file') {
  10  	$info = getimagesize($file);
  11  	$image = '';
  12  	$final_width = 0;
  13  	$final_height = 0;
  14  	list($width_old, $height_old) = $info;
  15  	if ($proportional) {
  16  		if ($width == 0) $factor = $height/$height_old;
  17  		elseif ($height == 0) $factor = $width/$width_old;
  18  		else $factor = min ( $width / $width_old, $height / $height_old);  
  19  		$final_width = round ($width_old * $factor);
  20  		$final_height = round ($height_old * $factor);
  21  	}
  22  	else {
  23  		$final_width = ( $width <= 0 ) ? $width_old : $width;
  24  		$final_height = ( $height <= 0 ) ? $height_old : $height;
  25  	}
  26  	switch ( $info[2] ) {
  27  		case IMAGETYPE_GIF:
  28  			$image = imagecreatefromgif($file);
  29  		break;
  30  		case IMAGETYPE_JPEG:
  31  			$image = imagecreatefromjpeg($file);
  32  		break;
  33  		case IMAGETYPE_PNG:
  34  			$image = imagecreatefrompng($file);
  35  		break;
  36  		default:
  37  			return false;
  38  	}
  39          
  40  	$image_resized = imagecreatetruecolor( $final_width, $final_height );
  41  			
  42  	if ( ($info[2] == IMAGETYPE_GIF) || ($info[2] == IMAGETYPE_PNG) ) {
  43  		$trnprt_indx = imagecolortransparent($image);
  44  
  45  		if ($trnprt_indx >= 0) {
  46  			$trnprt_color    = imagecolorsforindex($image, $trnprt_indx);
  47  			$trnprt_indx    = imagecolorallocate($image_resized, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
  48  			imagefill($image_resized, 0, 0, $trnprt_indx);
  49  			imagecolortransparent($image_resized, $trnprt_indx);
  50  		} 
  51  		elseif ($info[2] == IMAGETYPE_PNG) {
  52  			imagealphablending($image_resized, false);
  53  			$color = imagecolorallocatealpha($image_resized, 0, 0, 0, 127);
  54  			imagefill($image_resized, 0, 0, $color);
  55  			imagesavealpha($image_resized, true);
  56  		}
  57  	}
  58  	imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old);    
  59          
  60  	switch ( strtolower($output) ) {
  61  		case 'browser':
  62  			$mime = image_type_to_mime_type($info[2]);
  63  			header("Content-type: $mime");
  64  			$output = NULL;
  65  		break;
  66  		case 'file':
  67  			$output = $file;
  68  		break;
  69  		case 'return':
  70  			return $image_resized;
  71  		break;
  72  		default:
  73  		break;
  74  	}
  75  	switch ( $info[2] ) {
  76  		case IMAGETYPE_GIF:
  77  			imagegif($image_resized, $output);
  78  		break;
  79  		case IMAGETYPE_JPEG:
  80  			imagejpeg($image_resized, $output);
  81  		break;
  82  		case IMAGETYPE_PNG:
  83  			imagepng($image_resized, $output);
  84  		break;
  85  		default:
  86  			return false;
  87  	}
  88  	return true;
  89  }
  90  ?>
created by anonymous — 05 July 2008 — get a short url — tags: gd php resize embed

resize image with crop Delicious Email

show/hide lines
   1  ////////////////////////////////////////////////////////////////////////////////////
   2  // generate a croped-image from a picture
   3  // source: http://www.seaton-online.com/forum/index.php?showtopic=545
   4  // usage: cropImage("300", "225", "test/5.jpg", "jpg", "test/output.jpg");
   5  ////////////////////////////////////////////////////////////////////////////////////
   6  
   7  function cropImage($nw, $nh, $source, $stype, $dest) {
   8           $size = getimagesize($source);
   9           $w = $size[0];
  10           $h = $size[1];
  11  
  12            switch($stype) {
  13                case 'gif':
  14                $simg = imagecreatefromgif($source);
  15                break;
  16                case 'jpg':
  17                $simg = imagecreatefromjpeg($source);
  18                break;
  19                case 'png':
  20                $simg = imagecreatefrompng($source);
  21                break;
  22            }
  23  
  24            $dimg = imagecreatetruecolor($nw, $nh);
  25            $wm = $w/$nw;
  26            $hm = $h/$nh;
  27            $h_height = $nh/2;
  28            $w_height = $nw/2;
  29  
  30            if($w> $h) {
  31                $adjusted_width = $w / $hm;
  32                $half_width = $adjusted_width / 2;
  33                $int_width = $half_width - $w_height;
  34                imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
  35            } elseif(($w <$h) || ($w == $h)) {
  36                $adjusted_height = $h / $wm;
  37                $half_height = $adjusted_height / 2;
  38                $int_height = $half_height - $h_height;
  39                imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
  40            } else {
  41                imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
  42            }
  43  
  44            imagejpeg($dimg,$dest,80);
  45  }
created by anonymous — 05 July 2008 — get a short url — tags: gd php resize embed

hello kitty em php Delicious Email

show/hide lines
   1  <?php
   2  
   3  echo "hello kitty";
   4  
   5  ?>
created by anonymous — 05 July 2008 — get a short url — tags: hellokitty php embed

mysql one line all database backup Delicious Email

show/hide lines
   1  mysqldump --al-databases -u -p > backup.sql
created by anonymous — 04 July 2008 — get a short url — tags: backup mysql shell embed

activing php_auth on dreamhost Delicious Email

by default, dreamhost runs php as cgi.
the follow line switchs from cgi to apache module execution, supporting php_auth.

show/hide lines
   1  // create a .htaccess with this: 
   2  AddHandler application/x-httpd-php .php
created by anonymous — 04 July 2008 — get a short url — tags: apache dreamhost htaccess php embed
Displaying records 1 - 10 of 13