简体   繁体   English

如何从用户那里获得输入并对输入进行冒泡排序?

[英]How to get an input from a user and bubble sort that input?

I'm new to php, I'm trying to create a program that asks the user for number of elements, then input these elements, then bubbles sort them. 我是php的新手,正在尝试创建一个程序,询问用户元素的数量,然后输入这些元素,然后对它们进行排序。

<?php
  function bubbleSort(array $arr)
{
    $n = sizeof($arr);   
    for ($i = 1; $i < $n; $i++) {
        $flag = false;
        for ($j = $n - 1; $j >= $i; $j--) {
            if($arr[$j-1] > $arr[$j]) {
                $tmp = $arr[$j - 1];
                $arr[$j - 1] = $arr[$j];
                $arr[$j] = $tmp;
                $flag = true;
            }
        }
        if (!$flag) {
            break;
        }
    }

    return $arr;
}

// Example:
$arr = array(255,1,'a',3,45,5);
$result = bubbleSort($arr);
print_r($result); 
?>

My code works fine if I store the array, What I'm trying to do is to ask the user for the input instead of storing it in the code. 如果我存储数组,我的代码可以正常工作。我想做的是询问用户输入,而不是将其存储在代码中。 Can anyone help me out of how to ask the user of how many elements needed and then input these elements? 谁能帮助我,问用户需要多少元素然后输入这些元素?

If you want to read user input from the command line, you need to read from STDIN (standard input, when the user is running your script in the CLI). 如果要从命令行读取用户输入,则需要从STDIN读取(用户在CLI中运行脚本时的标准输入)。 This might be a duplicate of this question: PHP standard input? 这可能是以下问题的重复: PHP标准输入?

If, however, you want to display a web page and have users insert data into your script from their browser, you need to create a form and then access the form information via $_POST. 但是,如果要显示网页并让用户从其浏览器将数据插入脚本,则需要创建一个表单,然后通过$ _POST访问该表单信息。

Try this html code. 试试这个HTML代码。

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Submit a Ticket</title>
        <style type="text/css" media="screen">
            .hidden {
                display: none;
            }
        </style>
    </head>

    <body>
        <form name="form1" id="form1" method="post" action="<?php $_SERVER['PHP_SELF']?>">
            <h5>Bubble Sort</h5>
            <h6>Fill the values</h6>
            <label>Value 1<input type="text" name="bubble_values[]" id="bubble_values1" size="10" maxlength="10"></label><br>
            <label>Value 2<input type="text" name="bubble_values[]" id="bubble_values2" size="10" maxlength="10"></label><br>
            <label>Value 3<input type="text" name="bubble_values[]" id="bubble_values3" size="10" maxlength="10"></label><br>
            <label>Value 4<input type="text" name="bubble_values[]" id="bubble_values4" size="10" maxlength="10"></label><br>
            <label>Value 5<input type="text" name="bubble_values[]" id="bubble_values5" size="10" maxlength="10"></label><br>
            <input type="submit" value="Sort it!" id="bt-submit">
        </form>

        <h3>Result</h3>
        <?php
        function bubbleSort(array $arr)
        {
            $n = sizeof($arr);   
            for ($i = 1; $i < $n; $i++) {
                $flag = false;
                for ($j = $n - 1; $j >= $i; $j--) {
                    if($arr[$j-1] > $arr[$j]) {
                        $tmp = $arr[$j - 1];
                        $arr[$j - 1] = $arr[$j];
                        $arr[$j] = $tmp;
                        $flag = true;
                    }
                }
                if (!$flag) {
                    break;
                }
            }

            return $arr;
        }

        // Example:
        if (isset($_POST['bubble_values']) && !empty($_POST['bubble_values'])) {
            echo '<pre>';
            echo 'Before sort: ';
            print_r($_POST['bubble_values']);
            echo '<br>--------------------<br>';
            echo 'After sort:' ;
            print_r(bubbleSort($_POST['bubble_values'])); 
            echo '</pre>';
        }
        ?>
    </body>
</html>

This is probably a 'homework' question, but I'll take a stab. 这可能是一个“家庭作业”问题,但我会采取行动。

I'd provide the user a . 我会为用户提供一个。 Indicate in the page's text that each value should be separated by a line break. 在页面的文本中指出每个值都应由换行符分隔。 Then, in the $_POST variables, use split('\\r\\n', $_POST['myvar']) to get an array where each line is an element in the array. 然后,在$ _POST变量中,使用split('\\ r \\ n',$ _POST ['myvar'])获得一个数组,其中每一行都是数组中的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM