2011-08-29 31 views
0

這些是我的兩個方法,我需要包括他們在我的課,但我不確定如何做到這一點,因爲我不是一個程序員OO包括這些方法到類

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()); 

我需要包括那些並且讓它們在$ parser-> algorithm的輸出上運行幫助非常讚賞

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); 

感謝您的幫助!

回答

0

我對PHP知之甚少,但是,如果我理解正確,你所問的問題看起來很直白。我原以爲有人會回答這個問題。

以下可能是像你所追求的:

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())); 

`