Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class diamond
- {
- public $rst=array();
- public $n;
- public function __construct($n=3)
- {
- $this->set_n($n);
- }
- public final function set_n($n){
- $this->n=$n;
- }
- protected final function generate()
- {
- for($i=0;$i<$this->n*2;$i++){
- $this->rst[$i]=array();
- if($i<$this->n){
- for($j=0;$j<=$i;$j++){
- $this->rst[$i][]=($this->n*$i+1)-($this->n-1)*$j;
- }
- }else{
- for($j=2*$this->n-$i-1;$j>0;$j--){
- $this->rst[$i][]=($j+$i+1)*$this->n-($this->n*$this->n+$j)+1;
- }
- }
- }
- }
- public function show()
- {
- $this->generate();
- $space=strlen(($this->n*$this->n))+1;
- for($i=0;$i<$this->n*2;$i++)
- {
- $s=count($this->rst[$i]);
- $rst='';
- for($j=0;$j<$s;$j++)
- {
- $rst.=str_pad($this->rst[$i][$j], $space, " ", STR_PAD_BOTH);
- }
- echo str_pad($rst, $this->n*$space, " ", STR_PAD_BOTH)."\n";
- }
- }
- }
- class diamond_table extends diamond
- {
- public function __contruct($n=3)
- {
- parent::__contruct($n);
- }
- public function show()
- {
- $this->generate();
- $space=strlen(($this->n*$this->n))+1;
- $side=$this->n*2-1;
- $rst='<table>';
- for($i=0;$i<$side;$i++)
- {
- $s=count($this->rst[$i]);
- $rst.='<tr>';
- $c=0;
- for($j=0;$j<$side;$j++)
- {
- $rst.='<td>';
- if($j==round($side/2)-$s+2*$c && $c<$s){
- $rst.=$this->rst[$i][$c];
- $c++;
- }else{
- $rst.=' ';
- }
- $rst.='</td>';
- }
- $rst.="</tr>\n";
- }
- $rst.='</table>';
- echo $rst;
- }
- }
- $number=isset($_GET['number'])?$_GET['number']:3;
- ?>
- <!DOCTYPE html>
- <html>
- <head>
- <title>Diamond Square Number</title>
- <style>
- html,body {margin:0px;padding:0px;font-family:Arial,verdana,sans-serif;font-size:10px;color:#003;}
- td {text-align:center;width:10px;}
- </style>
- </head>
- <body>
- <nav>
- <form id='frm'>
- Input number [1-30] <input type="text" name="number" value="<?php echo $number;?>" />
- <input type="button" value="process" onClick="frm.submit();" />
- </form>
- </nav>
- <div id="content">
- <div id="tablebox">
- <?php
- $d=new diamond_table($number);
- $d->show();
- ?>
- </div>
- <div id="plainbox">
- <pre>
- <?php
- $d=new diamond($number);
- $d->show();
- ?>
- </pre>
- </div>
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement