Printing and vector allocation in php cli

Asked

Viewed 68 times

0

I am making a program in PHP that simulates the allocation of programs in a Memory. The problem is that it does not allocate the data in the vector. It prints the vector as if it were the same after its initialization

<?php
//  echo phpversion();
  define("MAX", 8);
  define("MAXMEM", 32);

  $tam;
  $valor;
  $memoria;
  $GLOBALS['$memoria'] = array();
  $inicio = 0;
  $Nalocacoes;

  Inicializar();
  Menu();

  return 0;



  //funcoes
  function AlocarProcesso(){

      echo "Digite o nome do processo:";
      $valor = fgets(STDIN);
      echo "Digite o tamanho do processo:";
      $tam = fgets(STDIN);

      $Nalocacoes=NumAlocacoes($tam);
      PreencherVetor($GLOBALS['$memoria'],$valor,$Nalocacoes);
      BubbleSort($GLOBALS['$memoria'], MAXMEM);

      return $GLOBALS['$memoria'];

      Menu();
  }


  function Inicializar(){
      for($i=0;$i<MAXMEM;$i++){
         $GLOBALS['$memoria'][$i]="VAZIO";
       }
  }
  function Listar($vetor, $n){
      for($i=0;$i<$n;$i++){
        echo "\n[".$i."] ".$vetor[$i];
      }
      Menu();
  }

  function NumAlocacoes($tam){
    if(($tam % MAX) == 0){
      $espacos = $tam/MAX;
    }
    else {
      $espacos = ((int) $tam/MAX)+1;
    }

    echo "Numero de Alocacoes: " . $espacos;
    return $espacos;


  }

  function PreencherVetor($vetor, $programa, $tam){

    for($i=0;$i<((MAXMEM/MAX)-1);$i++){
        if(strcmp($vetor[$i], "VAZIO") == 0){
          for($j=$i;$j<$tam;$j++){
            $vetor[$j] = $programa;


          }
        }


    }
    return $vetor;


  }
  function Menu(){
    $menu = 1;
    while ($menu) {
      echo "\n\n\n---------MENU----------\n";
      echo "1. Alocar Memoria -----\n";
      echo "2. Listar Memoria -----\n";
      echo "3. Desalocar Memoria --\n";
      echo "\n\n\n";

      $opt = fgets(STDIN);

      switch ($opt) {
        case 1:
          AlocarProcesso();
          break;
        case 2:
          //Listar($GLOBALS['$memoria'], MAXMEM);
          print_r($GLOBALS['$memoria']);
          Menu();
          break;
        case 3:
            DesalocarProcesso();
            break;
        default:
          $menu = 0;
          break;
      }
    }
  }
  function Desalocar($vetor, $valor){
      for($i = 0;$i<MAXMEM;$i++){
        if($vetor[$i] == $valor){
          $vetor[$i] = "VAZIO";
          $inicio --;
        }

      }
      BubbleSort($vetor, MAXMEM);
  }
  function DesalocarProcesso(){
    echo "Digite o processo: ";
    $valor = fgets(STDIN);
    Desalocar($GLOBALS['$memoria'],$valor);

  }

  function BubbleSort($data){
    $nowData = null;
    for ($i = 0; $i < count($data); $i++){
            for ($j = 0; $j < count($data); $j++){
                    if($data[$i] < $data[$j]){
                            $nowData = $data[$i];
                            $data[$i] = $data[$j];
                            $data[$j] = $nowData;
                    }
            }
    }
    return $data;
  }



?>

1 answer

0

There was a mistake in my Return that caused the php, although filling, did not return the updated vector. The question code is fully functional.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.