2011-08-29 6 views
0

Ce sont mes deux méthodes, je dois les inclure dans ma classe, mais je suis pas sûr comment le faire depuis que je ne suis pas un programmeur OOInclure ces méthodes dans ma classe

function gcd($x,$y) 
{ 
    do { 
     $rest=$x%$y; 
    $x=$y; 
    $y=$rest; 
    } while($rest!==0); 
    return $x; 
} 

function testCommonality($a) 
{ 
    $keys = array_keys($a[1]); 
    $common = array(); 
    foreach ($keys as $key) { 
    $v1 = $a[0][$key]; 
    $v2 = $a[1][$key]; 
    if ((gcd($v1, $v2)) != 1) $a[0]['m'] *= 1.5; 
    } 
    return $a; 
} 

print_r($parser->algorithm->testCommonality()); 

J'ai besoin d'inclure les dans cette classe, et leur demander de fonctionner sur la sortie de l'aide de l'algorithme $ parser-> est grandement apprécié

fORTEMENT
class CSVParser 
{ 

    public $output = NULL; 
    public $digits = NULL; 

    public function __construct($file) 
    { 


     if (!file_exists($file)) { 
      throw new Exception("$file does not exist"); 
     } 

     $this->contents = file_get_contents($file); 
     $this->output = array(); 
     $this->digits = array(); 
    } 

    public function parse($separatorChar1 = ',', $separatorChar2 = ';', $enclosureChar = '"', $newlineChar = "\n") 
    { 

     $lines = explode($newlineChar, $this->contents); 
     foreach ($lines as $line) { 
      if (strlen($line) == 0) continue; 
      $group = array(); 
      list($part1, $part2) = explode($separatorChar2, $line); 
      $group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part1), array("$enclosureChar \t")); 
      $group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part2), array("$enclosureChar \t")); 
      $this->output[] = $group; 
     } 
    } 

    private function trim_value($value, $chars) 
    { 
     return preg_replace("#^(|" . $chars . ")+#", '', $value); 
    } 

    public function algorithm() 
    { 
     $alpha = array(
      'c' => str_split('bcdfghjklmnpqrstvwxz'), 
      'v' => str_split('aeiouy') 
     ); 
     $i = 0; 
     $k = 0; 
     foreach ($this->output as $item) { 
      $cnt = 0; 
      $this->digits[$i] = array(); 
      foreach ($item as $part) { 
       $this->digits[$i][$cnt] = array(); 
       $new = array(); 
       foreach ($part as $str) { 
        $v = count(array_intersect(str_split($str), $alpha['v'])); 
        $c = count(array_intersect(str_split($str), $alpha['c'])); 
        $t = strlen(str_replace(' ', '', $str)); 
        $new = array('v' => $v, 'c' => $c, 't' => $t); 
        $this->digits[$i][$cnt][] = $new; 
       } 
       $cnt++; 
      } 
      $i++; 
     } 
    } 
} 


$parser = new CSVParser("file.txt"); 
$parser->parse(); 
print_r($parser->output); 
$parser->algorithm(); 
print_r($parser->digits); 

Merci pour l'aide!

Répondre

0

Je connais très peu de PHP, mais, si je comprends bien, ce que vous demandez semble être assez simple. J'aurais pensé que quelqu'un aurait déjà répondu à cela.

Ce qui suit peut être quelque chose comme ce que vous recherchez:

class CSVParser 
{ 

public $output = NULL; 
public $digits = NULL; 

public function __construct($file) 
{ 


    if (!file_exists($file)) { 
     throw new Exception("$file does not exist"); 
    } 

    $this->contents = file_get_contents($file); 
    $this->output = array(); 
    $this->digits = array(); 
} 

public function parse($separatorChar1 = ',', $separatorChar2 = ';', $enclosureChar = '"', $newlineChar = "\n") 
{ 

    $lines = explode($newlineChar, $this->contents); 
    foreach ($lines as $line) { 
     if (strlen($line) == 0) continue; 
     $group = array(); 
     list($part1, $part2) = explode($separatorChar2, $line); 
     $group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part1), array("$enclosureChar \t")); 
     $group[] = array_map(array($this, "trim_value"), explode($separatorChar1, $part2), array("$enclosureChar \t")); 
     $this->output[] = $group; 
    } 
} 

private function trim_value($value, $chars) 
{ 
    return preg_replace("#^(|" . $chars . ")+#", '', $value); 
} 

public function algorithm() 
{ 
    $alpha = array(
     'c' => str_split('bcdfghjklmnpqrstvwxz'), 
     'v' => str_split('aeiouy') 
    ); 
    $i = 0; 
    $k = 0; 
    foreach ($this->output as $item) { 
     $cnt = 0; 
     $this->digits[$i] = array(); 
     foreach ($item as $part) { 
      $this->digits[$i][$cnt] = array(); 
      $new = array(); 
      foreach ($part as $str) { 
       $v = count(array_intersect(str_split($str), $alpha['v'])); 
       $c = count(array_intersect(str_split($str), $alpha['c'])); 
       $t = strlen(str_replace(' ', '', $str)); 
       $new = array('v' => $v, 'c' => $c, 't' => $t); 
       $this->digits[$i][$cnt][] = $new; 
      } 
      $cnt++; 
     } 
     $i++; 
    } 
    return $this->digits; 
} 

private function gcd($x,$y) 
{ 
    do { 
     $rest=$x%$y; 
    $x=$y; 
    $y=$rest; 
    } while($rest!==0); 
    return $x; 
} 

public function testCommonality($a) 
{ 
    $keys = array_keys($a[1]); 
    $common = array(); 
    foreach ($keys as $key) { 
    $v1 = $a[0][$key]; 
    $v2 = $a[1][$key]; 
    if ((gcd($v1, $v2)) != 1) $a[0]['m'] *= 1.5; 
    } 
    return $a; 
} 
} 


$parser = new CSVParser("file.txt"); 
$parser->parse(); 
print_r($parser->output); 
$parser->algorithm(); 
print_r($parser->digits); 
print_r($parser->testCommonality($parser->digits())); 

`

Questions connexes