输出中的who; e序列(具有操作数,运算符和答案)是随机生成的,这是完美的。但我想以垂直方式展示 这是我想在单击生成后显示的输出 那么Q(2)会以相同的方式显示,但会在Q(1)之后出现。
这是我的html表单。
<form action="" method="POST">
<div class="row">
<div class="col-25">
<label for="qnum">Select no.of questions:</label>
</div>
<div class = "col-75"><input type="number" id="qnum" name="que" value="1" min="1" max="100"></div>
<br /><br />
</div>
<br />
<div class="row">
<div class="col-25">
<label for="int">How many numbers You want in a sequence:</label></div>
<div class="col-75">
<select name="select" id="int">
<option value="2"> 2 </option>
<option value="3"> 3 </option>
<option value="4"> 4 </option>
<option value="5"> 5 </option>
<option value="6"> 6 </option>
<option value="7"> 7 </option>
<option value="8"> 8 </option>
<option value="9"> 9 </option>
<option value="10"> 10 </option>
</select>
</div>
</div>
<br />
<div class="row">
<div class="col-25">
<label for="dig">Select no. of digits:</label></div>
<div class="col-75">
<select name="digits" id="dig">
<option value="1"> 1 digit</option>
</select>
</div>
<br /><br /><br /><br />
</div>
<div class="row">
<div class="col-25"><label>Select operations:</label><br /></div>
<div class="col-75">
<input type="checkbox" id="mix" value="all" class="toggle-button"><label>All</label>
<input type="checkbox" id="add" name="operation[]" value="Addition" checked><label>Addition</label>
<input type="checkbox" id="sub" name="operation[]" value="Subtraction"><label>Subtraction</label>
<input type="checkbox" id="mult" name="operation[]" value="Multiplication"><label>Multiplication</label>
<input type="checkbox" id="div" name="operation[]" value="Division"><label>Division</label>
</div>
<br /><br />
<br /><br />
</div><br />
<input type="submit" name="submit" value="Generate"><br>
<br />
</form>
</div>
<!---Toggle button to select all operations if user select checkbox named "All" -->
<script language="JavaScript">
$( '.col-75 .toggle-button' ).click( function () {
$( '.col-75 input[type="checkbox"]' ).prop('checked', this.checked)
})
</script>
基本上,我生成了一个随机的算术表达式序列,该序列可以完美运行而没有任何错误。 但是单击生成按钮后,输出将水平显示,而我想垂直生成。 那我该怎么办呢?
下面是我的php代码,它完美地生成了序列,没有任何错误。如何垂直生成输出?
<?php
error_reporting(E_ALL & ~E_NOTICE);
if(isset($_POST['submit']))
{
//validation for no of operation selected
$operation = $_POST['operation'];
if(empty($operation))
{
echo "Please Select at least one operation <br/>";
}
else
{
$N = count($operation); //count selected operation
echo "<br />";
echo "You have selected $N operation(s):";
for($i=0; $i < $N; $i++) //for loop for for operator array.
{
echo($operation[$i] . ", "); //display selected operation
}
echo "<br />";
//checkbox operations
if($N==1) //if only one operation is selected
{
if($operation[0]=='Addition')
{
$rOperators = array('+');
}
else if($operation[0]=='Subtraction')
{
$rOperators = array('-');
}
else if($operation[0]=='Multiplication')
{
$rOperators = array('*');
}
else
{
$rOperators = array('/');
}
}
else if ($N==2) //if only two operations are selected by the user
{
if($operation[0]=='Addition')
{
if($operation[1]=='Subtraction')
{
$rOperators = array('+','-');
}
else if($operation[1]=='Multiplication')
{
$rOperators = array('+','*');
}
else
{
$rOperators = array('+','/');
}
}
else if($operation[0]=='Subtraction')
{
if($operation[1]=='Multiplication')
{
$rOperators = array('-','*');
}
else
{
$rOperators = array('-','/');
}
}
else
{
$rOperators = array('*','/');
}
}
else if ($N==3) //if 3 operators gets selected by user
{
if($operation[0]=='Addition')
{
if($operation[1]=='Subtraction')
{
if($operation[2]=='Multiplication')
{
$rOperators = array('+','-','*');
}
else
{
$rOperators = array('+','-','/');
}
}
else
{
$rOperators = array('+','*','/');
}
}
else
{
$rOperators = array('-','*','/');
}
}
else
{
$rOperators = array('+','-','*','/');
}
}
//display sequence having only single digit numbers
if($_POST['digits'] == 1)
{
$q = $_POST['que'];
$previousInt = null; //Track the previous operand
$s = $_POST['select'];
for ($x = 1; $x<=$q; $x++) //for loop for no. of questions
{
$randomOperands = array();
$randomOperators = array();
// loop over $i n times
for ($i = 1; $i <=$s; $i++) //for loop for no. of integers user entered
{
$nextInt = rand(1, 9); // assign random operand to array slot
$randomOperands[] = $nextInt;
if($i < $s)
{
if($previousInt === 0) //No operator after last opearnd
{
//avoid division-by-zero
$randomOperators[] = $rOperators[rand(0, 2)];
}
else
{
$randomOperators[] = $rOperators[rand(0, $N-1)];
}
}
$previousInt = $nextInt;
}
// print array values
$exp = ''; //Generating the expression
foreach($randomOperands as $key=>$value1)
{
$exp .= $value1 . " " ;
if(isset($randomOperators[$key]))
{
$exp .= $randomOperators[$key] ." ";
}
}
$res = eval("return ($exp);");//evaluate the expression
//print expression
echo ("This is Q(".$x."):"), $exp."=". $res."<br>";
}
}
}
?>
答案 0 :(得分:0)
修改用于生成表达式的代码,该表达式用作eval()
函数的输入。创建另一个字符串$output_string
,该字符串可用于输出答案。在这个新创建的字符串中,将适当的HTML标记与生成的字符串一起使用,以按照您希望输出的样子对其进行格式化。
它应该看起来像这样:
$exp = ''; //Generating the expression
$output_string = ''; // new string which can be used for generating the output string
foreach ( $randomOperands as $key => $value1 ) {
$exp .= $value1 . " " ;
$output_string .= $value1 . " <br>" ;
if ( isset($randomOperators[$key]) ) {
$exp .= $randomOperators[$key] ." ";
$output_string .= $randomOperators[$key] ." <br> ";
}
}
$res = eval("return ($exp);");//evaluate the expression
//print expression
echo ("This is Q(".$x."): <br>"), $output_string."= <br>". $res."<br>";