Exemplo n.º 1
0
                $o1 = array_pop($stack);
                $r = apply_operator($t, $o1, null);
                array_push($stack, $r);
            } else {
                // binary
                $o1 = array_pop($stack);
                $o2 = array_pop($stack);
                $r = apply_operator($t, $o1, $o2);
                array_push($stack, $r);
            }
        } else {
            // operand
            array_push($stack, $t);
        }
    }
    if (count($stack) != 1) {
        die("invalid token array");
    }
    return $stack[0];
}
// $input = array("A", "and", "B", "or", "C", "and", "(", "D", "or", "F", "or", "not", "G", ")");
$input = array("false", "and", "true", "or", "true", "and", "(", "false", "or", "false", "or", "not", "true", ")");
// math or math or math and other and other
$input = array("(", "true", "or", "false", "or", "false", ")", "and", "true", "and", "true");
$input = array("true", "and", "false");
$tokens = shunting_yard($input);
$result = eval_rpn($tokens);
/*foreach($input as $t)
    echo $t." ";
echo "==> ".($result ? "true" : "false")."\n";
*/
Exemplo n.º 2
0
function evalFullPrereq($token, $studentId, $courseId)
{
    $result = "false";
    try {
        if (!validateToken($token, $studentId)) {
            return 403;
        }
        $conn = new PDO(DBCONNECTSTRING, DBUSER, DBPASSWORD);
        $sql = 'SELECT expression FROM prereqs WHERE prereqs.courseId=:courseId';
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':courseId', $courseId);
        $stmt->execute();
        $prereq = $stmt->fetch();
        //echo "pcount: ".$stmt->rowCount()."<br />";
        if ($stmt->rowCount() <= 0) {
            //double check no prereqs
            //echo "courseId: ".$courseId;
            $sql = 'SELECT prereqs FROM courses WHERE courses.id=:courseId';
            $stmt = $conn->prepare($sql);
            $stmt->bindParam(':courseId', $courseId);
            $stmt->execute();
            $course = $stmt->fetch();
            if ($stmt->rowCount() <= 0) {
                //echo "if";
                return 404;
                //course not found
            } else {
                if ($course['prereqs'] == 0) {
                    //echo "else";
                    return true;
                    //no prereqs
                }
            }
            //echo "neither";
        }
        //echo "expression: ".$prereq['expression']." ;";
        $tokens = explode(" ", $prereq['expression']);
        $evaledTokens = [];
        foreach ($tokens as $token) {
            //echo "<p>token:".$token;
            if (is_numeric($token)) {
                $eval = evalSinglePrereq($token, $studentId, $token);
                // echo "=>".$eval;
                $evaledTokens[] = $eval;
            } else {
                $evaledTokens[] = $token;
            }
            // echo "</p>";
        }
        //end foreach token
        //echo "<p>";
        foreach ($evaledTokens as $t) {
            //echo $t." ";
        }
        // echo "</p>";
        //send boolean expression to shunting yard function
        $input = shunting_yard($evaledTokens);
        $result = eval_rpn($input);
        foreach ($evaledTokens as $t) {
            //echo $t." ";
            //echo "==> ".($result ? "true" : "false")."\n";
            $sql = "";
        }
        $stmt = null;
        /*
            if($prereq['type'] == 2) // grade of X or above in X course
            {
        
        
                //Did student take course and make grade?
                $sql = 'SELECT id, grade from course_records WHERE studentId=:stuId AND courseId=:courseId AND type=1';
                $stmt = $conn->prepare($sql);
                $stmt->bindParam(':stuId',$studentId);
                $stmt->bindParam(':courseId',$prereq['courseId']);
                $stmt->execute();
                $taken = $stmt->fetchAll();
                $count=0;
                $count = $stmt->rowCount();
                echo "taken: ".$count."<br />";
        
                $meetsPrereq = false;
                if($count>0) {
                  foreach($taken as $c) {
                     if($c['grade'] >= $prereq['minGrade'])
                         $meetsPrereq = true;
                  }
                }
                $result = $meetsPrereq? "true":"false";
                echo "<p> Meets Prereq? ".$result."</p>";
        
        
            }//end if type=2
            else  if($prereq['type'] == 3) // at least X hours in X group
            {
              //TODO
            }
        */
    } catch (PDOException $e) {
        //echo $sql . "<br>" . $e->getMessage();
        return 500;
    }
    $conn = null;
    return $result;
}
Exemplo n.º 3
0
 public function calc($expression)
 {
     return calc_rpn(shunting_yard($expression));
 }