dpus81500574 2015-08-30 10:48
浏览 27
已采纳

打印成员之间的关系

I've a university project in which I've to print the relations between students in different classes level by level. The idea is if we have John and Kris studying in the same class they are friends of first level, if Kris studies with Math in same class then John and Math are friends of second level. I researched the problem and I found algorithms like this, but my main problem is that I use objects as input data :

<?php
class Student {

 private $id = null;
 private $classes = [];

 public function __construct($id) {
   $this->id = $id;
 }

 public function getId() {
   return $this->id;
 }

 public function getClasses() {
   return $this->classes;
 }

 public function addClass(UClass $class) {
   array_push($this->classes, $class);
 }

}

class UClass {

 private $id = null;
 private $students= [];

 public function __construct($id) {
   $this->id = $id;
 }

 public function getId() {
   return $this->id;
 }

 public function getStudents() {
   return $this->students;
 }

 public function addStudent(Student $student) {
   array_push($this->students, $student);
   $student->addClass($this);
 }

}

function getRelations(Student $start_student, &$tree = array(), $level = 2, &$visited) {
 foreach ($start_student>Classes() as $class) {    
   foreach ($class->Students() as $student) {
     if($start_student->getId() != $student->getId() && !is_int(array_search($student->getId(), $visited))) {
         $tree[$level][] = $student->getId();
         array_push($visited, $student->getId());
         getRelations($student, $tree, $level+1, $visited); 
     }
   }
 }
}

$class = new UClass(1);
$class2 = new UClass(2);
$class3 = new UClass(3);

$student = new Student(1);
$student2 = new Student(2);
$student3 = new Student(3);
$student4 = new Student(4);
$student5 = new Student(5);
$student6 = new Student(6);

$class->addStudent($student);
$class->addStudent($student2);
$class->addStudent($student4);

$class2->addStudentr($student2);
$class2->addStudent($student4);
$class2->addStudent($student5);

$class3->addStudent($student4);
$class3->addStudent($student5);
$class3->addStudent($student6);

$tree[1][] = $student->getId();
$visited = array($student->getId());
getRelations($student, $tree, 2, $visited);
print_r($tree);

I'm stuck at writing getRelations() function that should create an array that is something like

Array ( [1] => Array ( [0] => 1 ) [2] => Array ( [0] => 2 [1] => 4 ) [3] => Array ( [0] => 5 [1] => 6 ) ) 

but I can't get the recursion right(or probably the whole algorithm). Any help will be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • doukong1897 2015-08-30 17:29
    关注

    I come up with that function(not sure if it's the best solution, but it works with the class objects)

    function print_students(Student $start_student, &$tree = array(), $lvl = 1) {
      if (!$start_student) {
        return;
      }
      $tree[$lvl][] = $start_student->getId();
      $q = array();
      array_push($q, $start_student);
      $visited = array($start_student->getId());
    
      while (count($q)) {
        $lvl++;
        $lvl_students = array();
        foreach ($q as $current_student) {
          foreach ($current_student->getClasses() as $class) {
            foreach ($class->getStudents() as $student) {
              if (!is_int(array_search($student->getId(), $visited))) {
                array_push($lvl_students, $student);
                array_push($visited, $student->getId());
                $tree[$lvl][] = $student->getId();
              }
            }
          }
        }
        $q = $lvl_students;
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线