Apakah itu Pengaturcaraan Corak dalam PHP? Ia adalah satu seni pengaturcaraan untuk mencetak sejenis corak pada skrin. Ini boleh menjadi satu siri nombor, huruf atau aksara khas untuk membentuk corak. Contoh paling mudah bagi corak ialah siri Fibonacci (1, 1, 2, 3, 5, 8, 13, 21, 34 dan seterusnya). Kemudian terdapat corak lain yang merupakan reka bentuk pada skrin, katakan piramid bintang. Jadi, pada asasnya, pengaturcaraan corak hanyalah mencetak corak pada skrin.
IKLAN Kursus Popular dalam kategori ini PEMBANGUN PHP - Pengkhususan | 8 Siri Kursus | 3 Ujian Olok-olokMulakan Kursus Pembangunan Perisian Percuma Anda
Pembangunan web, bahasa pengaturcaraan, ujian perisian & lain-lain
Untuk artikel ini, kami akan menggunakan PHP untuk mengekodkan corak. Jangan risau. Sebaik sahaja anda memahaminya, ia hanyalah sintaks yang berbeza dari bahasa ke bahasa. Logiknya adalah sama, sentiasa.
Jadi, mengingati semua ini, mari cuba kodkan corak sekarang.
Ini adalah corak paling mudah untuk dicetak. Ia mencetak bilangan bintang yang semakin meningkat dalam baris berikutnya. 1 bintang di baris pertama, 2 bintang di baris kedua dan seterusnya.
Mari kodkan corak ini untuk lima baris. Dengan mengingati logik, gelung luar kami akan berjalan lima kali. Oleh kerana bilangan bintang dalam setiap baris adalah bergantung secara langsung pada nombor baris, gelung dalaman kami akan menjadi fungsi pembolehubah kawalan dalam gelung luar kami. Mari lihat caranya.
Our outer control variable is i and inner control variable is j. Outer loop iteration 1 –> i = 0 Inner loop iteration 1 –> <em>j </em>=<em> 0</em> Print star Outer loop iteration 2 –> i<em> = 1</em> Inner loop iteration 1 –> <em>j</em><em> =</em><em> 0</em> Print Star Inner loop iteration 2 -> <em>j</em> =<em> 1 </em>Print Star Outer loop iteration 3 –> <em>i</em> =<em> 2 </em>Inner loop iteration 1 –> <em>j =</em> 0 Print Star Inner loop iteration 2 -> <em>j = 1 </em>Print Star Inner loop iteration 3 -> <em>j = 2 </em>Print Star
Dan seterusnya. Beginilah cara kita mengawal gelung dalam kita berdasarkan pembolehubah kawalan gelung luar. Mari kita lihat program beraksi sekarang.
Kod:
<?php function print_pattern($num) { // Outer loop handles number of rows for ($i = 0; $i < $num; $i++) { // inner loop handles number of columns for($j = 0; $j <= $i; $j++ ) { // Print stars echo "* "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 5; print_pattern($num); ?>
Output:
Ini serupa dengan Piramid Separuh Bintang, kecuali bintang dijajarkan ke kanan.
Untuk mencapai lekukan yang betul, kami akan menggunakan ruang dan kemudian mencetak bintang. Jadi, akan ada dua gelung dalam– satu untuk mengawal bilangan ruang dan yang lain untuk mengawal bilangan bintang.
Nota: Perlu diingat bahawa bilangan ruang dalam gelung k ialah ruang berganda. Ini kerana kami mencetak satu ruang bersama dengan bintang juga. Ini memberikan rupa yang siap pada corak kami dan bukannya cetakan yang sesak. Kami akan menggunakan ini untuk memanfaatkan kami apabila kami mencetak piramid penuh.Kod:
<?php function print_pattern($num) { // Outer loop handles number of rows for ($i = 0; $i < $num; $i++) { // inner loop handles indentation for($k = $num; $k > $i+1; $k-- ) { // Print stars echo " "; } // inner loop handles number of stars for($j = 0; $j <= $i; $j++ ) { // Print stars echo "* "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 5; print_pattern($num); ?>
Output:
Untuk corak piramid ini, bilangan bintang terus berkurangan dengan setiap baris baharu. Baris pertama ada 5 bintang, baris kedua ada 4 bintang dan seterusnya.
Mengingat logik, kita tahu bahawa gelung luar sentiasa perlu mengawal bilangan garisan dan gelung dalam perlu mengawal bilangan bintang. Logik ini tidak boleh diubah. Walaupun, apa yang boleh diubah ialah bagaimana kita memulakan gelung, meningkatkan atau mengurangkan susunan. Ini bermakna kita boleh sama ada gelung dari 0 hingga 5 atau kita boleh gelung dalam susunan menurun daripada 5 kepada 0. Jadi, untuk corak songsang seperti ini, kita tahu bahawa bilangan bintang lebih banyak dalam baris pertama. Jadi, kami memilih untuk mengurangkan gelung pesanan.
Kod:
<?php function print_pattern($num) { // Outer loop handles number of rows for ($i = $num; $i > 0; $i--){ // inner loop handles number of stars for($j = 0; $j < $i; $j++ ) { // Print stars echo "* "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 5; print_pattern($num); ?>
Output:
This pattern is an indented inverted half pyramid. The number of stars decreases with each line and stars are right-aligned.
I believe by now you would be able to guess the logic for this one.
Code:
<?php function print_pattern($num) { // Outer loop handles number of rows for ($i = $num; $i > 0; $i--) { // inner loop handles indentation for($k = $i; $k < $num; $k++ ) { // Print stars echo " "; } // inner loop handles number of stars for($j = 0; $j < $i; $j++ ) { // Print stars echo "* "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 5; print_pattern($num); ?>
Output:
This pattern prints the full pyramid. Or in other words, it prints a triangle of stars on the screen.
This pattern is essentially a combination of Half pyramid and its mirror. Although there is a slight twist when we code it. Revisit the Note in Mirrored Half Pyramid. Remember, how we used double spacing to give a finished look to our pattern? Here we would use single spacing so that the stars are alternately aligned in odd and even number of rows, giving us a true triangular pattern.
Code:
<?php function print_pattern($num) { // Outer loop handles number of rows for ($i = 0; $i < $num; $i++) { // inner loop handles indentation for($k = $num; $k > $i+1; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 0; $j <= $i; $j++ ) { // Print stars echo "* "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 5; print_pattern($num); ?>
Output:
This pattern prints a complete diamond shape on the screen. The number of stars increases until the maximum defined and then decrease back to 1, giving us a full diamond shape.
To print this pattern, we would need to divide the pattern into two halves. The upper half – which prints the increasing number of stars. The lower half– which prints the decreasing number of stars. To print both the halves, we would use two outer loops and corresponding inner loops.
Code:
<?php function print_pattern($num) { // The Upper Half Pattern // Outer loop handles number of rows for ($i = 0; $i < $num; $i++) { // inner loop handles indentation for($k = $num; $k > $i+1; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 0; $j <= $i; $j++ ) { // Print stars echo "* "; } // go to new line after each row pattern is printed echo "\n"; } // The Lower Half Pattern // Outer loop handles number of rows for ($i = $num-1; $i > 0; $i--) { // inner loop handles indentation for($k = $num-1; $k >= $i; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 0; $j < $i; $j++ ) { // Print stars echo "* "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 5; print_pattern($num); ?>
Output:
For this number pattern, we will print the numbers in relation to the row number. Thus, digit 1 would be printed once, 2 twice, 3 thrice and so on.
If you would have followed this tutorial line by line, by now you must have understood very well the working of nested loops to print patterns. This pattern also follows the same logic. Instead of stars, we print numbers. Now you ask how do we get the numbers? The answer is- simply through our control variables i and j.
Code:
<?php function print_pattern($num) { // Outer loop handles number of rows for ($i = 1; $i <= $num; $i++) { // inner loop handles indentation for($k = $num; $k > $i; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 1; $j <= $i; $j++ ) { // Print numbers echo $i." "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 5; print_pattern($num); ?>
Output:
In this pattern, we would print the alphabets ABCDE in a pattern. Starting with A, the subsequent rows would introduce a new alphabet sandwiched between the previous alphabets.
The only trick in this pattern is to get the characters from our control variables. We do this by leveraging the ASCII value of the characters. The ASCII value of A to Z is 65 to 90. So, we calculate the ASCII value in each iteration and print the corresponding character. The chr() function in PHP is used to print a character from the ASCII code.
Code:
<?php function print_pattern($num) { // Outer loop handles number of rows for ($i = 1; $i <= $num; $i++) { // inner loop handles indentation for($k = $num; $k > $i; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 1; $j <= $i; $j++ ) { // Print characters echo chr(64+$j)." "; } for($j = $i-1; $j >= 1; $j-- ) { // Print characters echo chr(64+$j)." "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 5; print_pattern($num); ?>
Output:
Print for full alphabets and the pattern looks pretty cool.
This pattern is a dynamic pattern that prints the hourglass relative to the time elapsed, not an actual calculation though. For e.g., if one hour has elapsed, it will print one line of 0s in the upper half and one line of 1s in the lower half.
Code:
<?php function print_pattern($num, $hour) { // Outer loop handles number of rows for ($i = $num; $i > 0; $i--) { // inner loop handles indentation for($k = $num; $k > $i; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 0; $j < $i; $j++ ) { // Print characters if($num-$i < $hour) echo "0 "; else echo "1 "; } // go to new line after each row pattern is printed echo "\n"; } for ($i = 1; $i < $num; $i++) { // inner loop handles indentation for($k = $num-1; $k > $i; $k-- ) { // Print spaces echo " "; } // inner loop handles number of stars for($j = 0; $j <= $i; $j++ ) { // Print characters if($num-$i <= $hour) echo "1 ";else echo "0 "; } // go to new line after each row pattern is printed echo "\n"; } } //Call function and send number of lines as parameter $num = 8; $hour = 3; print_pattern($num, $hour); ?>
Output: 1 hour has elapsed.
Output: 2 hours have elapsed.
Output: 3 hours have elapsed.
And so on.
There is a lot to play with patterns. It’s all about keeping the code logic in mind. Once you get the code logic completely understood, there is no pattern you can’t print.
Atas ialah kandungan terperinci Corak dalam PHP. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!