Close
Faqja 2 prej 2 FillimFillim 12
Duke shfaqur rezultatin 11 deri 12 prej 12
  1. #11
    i/e regjistruar Maska e Gepardi
    Anėtarėsuar
    30-10-2002
    Vendndodhja
    Tiranė
    Postime
    169
    Problemi i hapesirave korrigjohet lehte.
    Une kisha marre parasysh ti pastroja hapesirat (meqe nuk i bera nje zgjidhje te plote sfides por thjesht krijova disa funksione qe zgjidhin problemin).

    Kam harruar dicka:

    Kodi PHP:

    //Tek rreshti
     
    str_replace(" ","",$shprehja// tek funksioni postfix

    //duhet 

     
    $shprehja str_replace(" ","",$shprehja); //kjo pastron hapesirat


    // Moderatori:  Korrigjova programin sipas udhėzimeve mė lart. 

    Gjithashtu sic e thashe scripti qe kam shkruar nuk kontrollon per gabime.
    Ndryshuar pėr herė tė fundit nga edspace : 28-08-2005 mė 15:30

  2. #12
    i/e regjistruar Maska e Gepardi
    Anėtarėsuar
    30-10-2002
    Vendndodhja
    Tiranė
    Postime
    169
    Ja dhe zgjidhja e plote e sfides. Kodi kete rradhe kontrollon dhe per gabime gjithashtu shprehja duhet shkruar duke perdorur hapesiren boshe per ndarese te simboleve, ne te kundert scripti do te shfaqe sinjal per gabim ne shprehje.
    Kjo realizohet nepermjet funksionit erregullt()

    Kodi PHP:
    <?
    /*---------------------------------------------------------------------------
        
        Emri: Sfida e rradhes nga Pr-Tech
        Pershkrimi: Krijimi dhe vleresimi i shprehjeve infix- postfix
        Punoi: Klesti Hoxha
        Data: 31-8-2005
        
    ---------------------------------------------------------------------------*/
        

        class nyje_peme { //kjo klase perdoret per te simuluar pemet ne PHP
            var $majte;    
            var $djathte;
            var $vlera;
        }
    //-----------Funksioni percakton vlefshmerine e shprehjes-------------    

        function erregullt($shprehja) {
            $shenjat = array("+","-","*","/",")","("," ");
            if (strpos($shprehja,"  ") !== false) die("Keni ndarė shenjat me mė shumė se njė hapėsirė!"); // a ka me shume se nje hapesire ndarese ?
            else {
                for ($i=0;$i<strlen($shprehja);$i++) { //kontrollo per shenja te palejuara
                    if (!in_array($shprehja[$i],$shenjat) && !is_numeric($shprehja[$i])) {
                        die("Keni shkruar shenja te palejuara");
                    }
                }
                $shprehja2 = $shprehja;
                for ($i=0;$i<strlen($shprehja2);$i++) { //kontrollo a jane vendosur mire kllapat
                    if ($shprehja2[$i] == '(') {
                        $ugjet = false;
                        for ($j=$i;$j<strlen($shprehja2);$j++) {
                            if ($shprehja2[$j] == ')') {
                                $shprehja2[$j] = '*';
                                $ugjet = true;
                                break;
                            }
                        }
                        if (!$ugjet) die("Keni Gabim ne vendosjen e kllapave!");
                    } //if
                } //for
                $shprehja = $shprehja;
                for ($i=strlen($shprehja);$i>=0;$i--) {
                    if ($shprehja[$i] == ')') {
                        $ugjet = false;
                        for ($j=$i;$j>=0;$j--) {
                            if ($shprehja[$j] == '(') {
                                $shprehja[$j] = '*';
                                $ugjet = true;
                                break;
                            }
                        }
                        if (!$ugjet) die("Keni Gabim ne vendosjen e kllapave!");
                    } //if
                } //for
                 //Kontrollo a jane vene ne rregull hapesirat
                 $sh = explode(" ",$shprehja);
                  
                 for ($i=0;$i<count($sh);$i++) {
                     if (!in_array($sh[$i],$shenjat) && !is_numeric($sh[$i])) die("Keni Gabim ne vendosjen e hapesirave!");
                 }
                 $nr_hap_lejuara = count($sh)-1;
                 
                if (substr_count($shprehja," ") != $nr_hap_lejuara) die("Keni Gabim ne vendosjen e hapėsirave!");
                
                //Kontrollo per vendosje te parregullt te operatoreve dhe numrave
                $operatoret = array("+","-","/","*");
                $nr_operatoreve = 0;
                for ($i=0;$i<count($sh);$i++) {
                    if (in_array($sh[$i],$operatoret)) $nr_operatoreve += 1; 
                }
                
                if ($nr_operatoreve != (count($sh)-1)/2) die("Keni gabim ne vendosjen e operatoreve!");
                
            }
            
        }
    //-------------funksioni percakton perparesine e operatoreve-----------------------
        function perparesi($op) { 
            switch ($op) {
                case "+":
                case "-":
                    return 1;
                break; 
                case "*":
                case "/": 
                    return 2;
                break;    
            }
        }
    //--------------------------funksioni kryen nje veprim    ----------------------------
        function veprim($x,$y,$v) { 
            switch ($v) {
                case "+": return $x+$y;
                case "-": return $y-$x;
                case "/": return $y/$x;
                case "*": return $x*$y;
            }
        }
    //------------------funksioni kthen shprehjen ne postfix duke perdorur algoritmin me stive-----
        function postfix($shprehja) { 
            $sh = explode(" ",$shprehja);
            $stiva = array(); //Array qe simulon stiven
            $pf = '';
            for ($i=0;$i<count($sh);$i++) {
                $s = $sh[$i];
                if (is_numeric($s)) $pf .= "$s ";
                elseif ($s == '(') array_push($stiva,$s);
                elseif ($s == ')') {
                    $st = array_pop($stiva);
                    while ($st != '(') {
                        $pf .= "$st ";
                        $st = array_pop($stiva);
                    }
                }
                else {
                    $kreu = $stiva[count($stiva)-1];
                    if ($kreu == '' || $kreu == '(' || (perparesi($s) > perparesi($kreu))) array_push($stiva,$s);
                    else {
                        $pf .= array_pop($stiva);
                        $pf .= " ";
                        array_push($stiva,$s);
                    }
                }
            } //for
            
            while (current($stiva) != false) {
                $pf .= array_pop($stiva);
                $pf .= " ";
            }
            $pf = substr($pf,0,-1);
            return $pf;
        }
    //-----------------Krijon pemen binare nga nje shprehje postfix-----------------------------
        function Pema($shprehja) { 
            $stiva = array(); //array qe simulon stiven
            $shp = explode(" ",$shprehja);
            for ($i=0;$i<count($shp);$i++) {
                $s = $shp[$i];
                if (is_numeric($s)) {
                    $p = new nyje_peme;
                    $p->vlera = $s;
                    $p->majte = NULL;
                    $p->djathte = NULL;
                    array_push($stiva,$p);
                }
                else {
                    $p = new nyje_peme;
                    $p->vlera = $s;
                    $p->djathte = array_pop($stiva);
                    $p->majte = array_pop($stiva);
                    array_push($stiva,$p);
                }
            }
            return array_pop($stiva);
        }
    //---------------------------Kthen vleren e shprehjes postfix    -----------------------
        function VleraShprehjes($shprehja) {  
            $stive = array(); //array qe simulon stiven
            $shp = explode(" ",$shprehja);
            for ($i=0;$i<count($shp);$i++) {
                $s = $shp[$i];
                if (is_numeric($s)) array_push($stive,$s);
                else {
                    $x = array_pop($stive);
                    $y = array_pop($stive);
                    $vl = veprim($x,$y,$s);
                    array_push($stive,$vl);
                }
            }
            return array_pop($stive);
        }
    //-------------------------Shprehja prefix (nga bredhja e pemes)-------------
        function prefix($p) {
            if ($p != NULL) {
                echo $p->vlera ." ";
                prefix($p->majte);
                prefix($p->djathte);
            }
        }    
    //----------------Fillimi i programit --------------------------------

        if (!isset($_POST['jepi'])) {
            echo "<form action='$PHP_SELF' method='post'>
            Shkruaj Shprehjen infix: <input type='text' name='shprehja'>
            <input type='submit' name='jepi' value='Llogarit Shprehjen'>
            </form>";
        }
        else {
            $shprehja = $_POST['shprehja'];
            erregullt($shprehja);
            $postfix = postfix($shprehja);
            $pema = Pema($postfix);
            echo "Shprehja postfix ėshtė: $postfix<p>
            Shprehja prefix ėshtė: 
            ";
            prefix($pema);
            echo "<p>";
            $vlera = VleraShprehjes($postfix);
            
            echo "Vlera e Saj ėshtė : $vlera";
        }

        
    ?>
    Skedarėt e Bashkėngjitur Skedarėt e Bashkėngjitur

Faqja 2 prej 2 FillimFillim 12

Regullat e Postimit

  • Ju nuk mund tė hapni tema tė reja.
  • Ju nuk mund tė postoni nė tema.
  • Ju nuk mund tė bashkėngjitni skedarė.
  • Ju nuk mund tė ndryshoni postimet tuaja.
  •