【问题标题】:Can you explain how the formulas implemented in the two functions work?你能解释一下这两个函数中实现的公式是如何工作的吗?
【发布时间】:2018-02-18 20:24:08
【问题描述】:

这两个函数是基于一个产品分类系统,基于0.5到5分的投票,即:(0.5, 1, 1.5, 2, 2.5 3, 3.5, 4, 4.5, 5)。

但是数学或算术计算是如何工作的呢?

function calculate_stars($rating){
    // step starting from 0
    return (($rating/0.5)-1);
}

这告诉我们它是如何工作的:

function display_star($rating){
    $output="";
    $step = calculate_stars($rating);
    $output ='<ul class="c-rating">';
    for ($i=0;$i<10;$i++)
    {
        $class = '';
        if($i<=$step)  $class = ' is-active';
        if($i%2 == 0) $class .= ' left';
        $output .= '<li class="c-rating__item'. $class.'" data-index="'.$i.'"></li>';
    }

    $output .='</ul>';
    return $output;

}

echo display_star(0.5);

您能解释一下每个算术或数学过程,以了解和了解这些函数如何正确工作吗?

【问题讨论】:

  • 出于某种原因,它将 4.5 变成 8 个“步骤”,然后打印 html 以表示半星。它通过检查每个步骤是否被 2 整除来做到这一点。
  • calculate_stars() 中的 4.5 / 0.5 等于 9 减 1 是 8 的最终结果——这在数学上是正确的。
  • 它还将把 0.5 星变成 0 步,将 0 星变成 -1

标签: php function


【解决方案1】:

使用交互模式看看它做了什么:

php -a
Interactive mode enabled

php > function calculate_stars($rating){
php {     // step starting from 0
php {     return (($rating/0.5)-1);
php { }
php > 
php > function display_star($rating){
php {     //$output="";
php {     $out = array();
php {     $step = calculate_stars($rating);
php {     //$output ='<ul class="c-rating">';
php {     for ($i=0;$i<10;$i++)
php {     {
php {         $class = '';
php {         if($i<=$step)  $class = ' is-active';
php {         if($i%2 == 0) $class .= ' left';
php {         //$output .= '<li class="c-rating__item'. $class.'" data-index="'.$i.'"></li>';
php {         $out[] = str_pad($class, 14, " ", STR_PAD_LEFT);
php {     }
php { 
php {     //$output .='</ul>';
php {     //return $output;
php {     return implode($out,' | ');
php { 
php { }
php > 
php > for($i=0.5; $i <= 5; $i+=0.5) {
php {   printf("% 3s: %d %s\n",
php (     $i,
php (     calculate_stars($i),
php (     display_star($i)
php (   );
php { }
0.5: 0  is-active left |                |           left |                |           left |                |           left |                |           left |               
  1: 1  is-active left |      is-active |           left |                |           left |                |           left |                |           left |               
1.5: 2  is-active left |      is-active |  is-active left |                |           left |                |           left |                |           left |               
  2: 3  is-active left |      is-active |  is-active left |      is-active |           left |                |           left |                |           left |               
2.5: 4  is-active left |      is-active |  is-active left |      is-active |  is-active left |                |           left |                |           left |               
  3: 5  is-active left |      is-active |  is-active left |      is-active |  is-active left |      is-active |           left |                |           left |               
3.5: 6  is-active left |      is-active |  is-active left |      is-active |  is-active left |      is-active |  is-active left |                |           left |               
  4: 7  is-active left |      is-active |  is-active left |      is-active |  is-active left |      is-active |  is-active left |      is-active |           left |               
4.5: 8  is-active left |      is-active |  is-active left |      is-active |  is-active left |      is-active |  is-active left |      is-active |  is-active left |               
  5: 9  is-active left |      is-active |  is-active left |      is-active |  is-active left |      is-active |  is-active left |      is-active |  is-active left |      is-active
php > 

您有 10 个可能的评分,以 0.5 为增量。所以display_star($rating) 遍历所有 10 个,给出适当的类。显示的星号取决于给定的类别。因此,对于每组星,有 10 个 &lt;li&gt; 具有不同的类。检查您的 CSS 以了解它们各自的作用。

【讨论】:

  • 感谢朋友的回答,抱歉可以补充更多的信息,我要跟老师解释,还有一些我不明白,我明白第二个函数创建10 li 从0 到 9。
  • 但是你可以更深入地向我解释它控制这个:return (($rating/0.5)-1);,根据你的口味举一个 1 星或 5 星评级的例子。
  • 如前所述,我知道 for 循环 (for ($i=0;$i&lt;10;$i++)) 创建了一个从 0 到 9 的数据编号。但是在那个 for 循环中,我不明白这两个 if 是什么验证是做什么的,if($i&lt;=$step) 和另一个检查 if($i%2 == 0) 的人是做什么的,请解释我以 1 或 5 的星级为例,以便能够理解它的作用。
【解决方案2】:

为了更好地掌握代码,我设计了以下内容:

<?php
const OFFSET = 0.5;
function calculate_stars($rating){
    return ( ($rating/OFFSET) - 1 );
}
function display_star($rating){
    $output="";
    $str = "";
    $step = calculate_stars($rating);

    for ($i=0, $max = 10; $i < $max; $i++)
    {
        echo str_pad($output,$i,"*"),"\n";
    }
}

display_star(0.5);

live code

然后,我发现了一个相关链接 here 并找到了 code 似乎构成了 OP 问题的基础。因此,以下备注与 OP 的示例代码有关。

函数 display_star() 通过采用评级参数来让球滚动。该函数调用calculate_stars(),初始$rating 为0.5。回调返回一个设置 $step 的值。 for 循环生成一行 5 颗星,每颗星由两半组成,这样可以方便地表示 0.5 的评分,其中一半填充星。

函数calculate_stars() 接受一个参数$rating 并返回一个设置$step 的值。为了确定要输出多少半星和/或全星,该函数使用一种算法,将评级除以 0.5 或半星,以适应 0.5 到 5.0 星评级系统中的二分之一增量值。接下来,从该结果中减去一个,以便最终结果确定有多少颗星是完全或部分活跃的,即 0.5 的评级转换为第 0 步,因此将只有半颗星,而其余四颗星是空白的.

在 display_star() 中,第一个 if 条件测试 $i 是否小于或等于步长值。如果该语句的计算结果为真,则变量$class 被分配一个“is_active”类。下一个 if 语句使用模运算,以便在 for 循环的每一次迭代中,$class 的值扩展为包含另一个类,即“left”。 PHP 代码生成由十个 li 元素组成的 HTML,伴随的 CSS 将其转换为一个半星和四个空白星;查看计算结果here

【讨论】:

  • 很好,如果你想重新定义一个函数或对象,你不必退出。我必须记住这一点。
  • @slevy1 感谢朋友的回答,抱歉可以补充更多信息,我要跟老师解释,还有一些我不明白的地方,我明白第二个函数创建10 li从 0 到 9 开始。
  • @slevy1 但是你可以更深入地向我解释它控制这个:return (($rating/0.5)-1);,根据你的口味举一个 1 星或 5 星评级的例子。跨度>
  • @slevy1 正如我前面提到的,我知道 for 循环 (for ($i=0;$i&lt;10;$i++)) 创建了一个从 0 到 9 的数据编号。但是在 for 循环中,我不明白这两个 @987654332 @ 验证是做什么的,if($i&lt;=$step) 和另一个检查 if($i%2 == 0) 的人是做什么的,请解释我以 1 或 5 的星级为例,以便能够理解它的作用。
  • @OneL 我在修改后的答案中包含了解释。我希望这会有所帮助。
猜你喜欢
  • 2011-02-16
  • 2013-03-13
  • 1970-01-01
  • 2017-03-19
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-18
相关资源
最近更新 更多