帮助PHP算法类

时间:2011-08-26 17:37:32

标签: php algorithm oop class

我正在尝试构建一个以某种方式解析CSV文件的类。 不幸的是,我不是任何形式或形式的OO程序员,我的 公司要求我为未来的某些功能编写课程 我认真地,认真地需要一些帮助。所以......

我们的$ value将通过计算文件中的第二个标记来制定 这是一个分号,并为每个回车创建一个新的Feed。

输入是这样的:

代码:

  

Jeff Goes,Mika Enrar;三重威胁,骑自行车的狗

     

Sonny Ray,Lars McGarvitch,Jason McKinley; Kasabian,酸之王,Hard-Fi

因此,为了便于理解,我们可以说名字是$ name,the 乐队是$ item,等于分数是$ value。

跑下来: 如果$ item中的字母数是偶数(Triple Threat = 12) 然后$ value等于$ name时间中的元音数量 一个半(Jeff Goes [3x1.5] = 4.50)

如果$ item的数字是奇数(Dogs on Bikes = 11)那么$ value 是$ name中的辅音数量(Mika Enrar = 5)

如果$ name中的字母数除了一个之外在$ item中共享一个共同点, 将输出乘以一个半。所以(Sonny Ray = 8; Kasabian = 8; 那么6x1.5 = 9;或者说$ name = 112; $ item = 12;我们有共同点12)

这个想法是实现一个类,它为每个$ name指定一个以某种方式提供的$ item 最大化所有$ item的总$ $值。作为故障安全, 可能有不同数量的$ name和$ item。

我的输出应该是两位小数的最大$值,如 12.00 38.50

1 个答案:

答案 0 :(得分:1)

更新的ANSER
如果你正在寻找更多的OOP,并且使用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++;
        }
    }
}


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