Rumah >pembangunan bahagian belakang >C++ >Menguasai Corak Penuh: Panduan Komprehensif dengan Contoh Kod
Selamat datang ke panduan komprehensif kami untuk mencipta pelbagai corak terisi menggunakan gelung dalam pengaturcaraan C! Dalam tutorial ini, kami akan melalui arahan langkah demi langkah tentang cara melukis 18 corak terisi yang berbeza. Corak ini terdiri daripada bentuk asas seperti segi empat sama dan segi tiga kepada bentuk yang lebih kompleks seperti berlian, heksagon dan pentagon. Setiap corak dicipta menggunakan gelung bersarang, menjadikannya latihan yang sangat baik untuk pemula untuk mempraktikkan struktur kawalan dalam C. Mari selami!
Anda boleh menemui semua kod dalam repositori GitHub kami.
Sebelum kita mula dengan corak, adalah penting untuk memahami konsep gelung bersarang. Gelung bersarang ialah gelung di dalam gelung lain. Struktur ini amat berguna untuk mengendalikan tatasusunan berbilang dimensi dan untuk menjana corak. Dalam C, struktur gelung bersarang biasa kelihatan seperti ini:
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { // Code to execute } }
int n = 5; // size of the square char ch = '*'; printf("1. Filled Square:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * * * * * * * * * * * *
printf("2. Filled Right Triangle:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j <= i; j++) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * *
printf("3. Filled Inverted Right Triangle:\n"); for (int i = 0; i < n; i++) { for (int j = n; j > i; j--) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * *
printf("4. Filled Right Aligned Triangle:\n"); for (int i = 0; i < n; i++) { for (int j = n - 1; j > i; j--) { printf(" "); } for (int j = 0; j <= i; j++) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * *
printf("5. Filled Right Aligned Inverted Triangle:\n"); for (int i = 0; i < n; i++) { for (int j = 1; j <= i; j++) { printf(" "); } for (int j = n; j > i; j--) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * *
printf("6. Filled Right Pascal Triangle:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < i + 1; j++) { printf("%c ", ch); } printf("\n"); } for (int i = 0; i < n; i++) { for (int j = n; j > i + 1; j--) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * * * * * * * * * * * *
printf("7. Filled Left Pascal Triangle:\n"); for (int i = 0; i < n; i++) { for (int j = n - 1; j > i; j--) { printf(" "); } for (int j = 0; j < i + 1; j++) { printf("%c ", ch); } printf("\n"); } for (int i = 0; i < n; i++) { for (int j = 0; j < i + 1; j++) { printf(" "); } for (int j = n - 1; j > i; j--) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * * * * * * * * * * * *
printf("8. Filled Equilateral Triangle:\n"); for (int i = 0; i < n; i++) { for (int j = n - 1; j > i; j--) { printf(" "); } for (int j = 0; j <= i; j++) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * *
printf("9. Filled Inverted Equilateral Triangle:\n"); for (int i = n - 1; i >= 0; i--) { for (int j = n - 1; j > i; j--) { printf(" "); } for (int j = 0; j <= i; j++) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * *
printf("10. Filled Pyramid:\n"); for (int i = 0; i < n; i++) { for (int j = n - 1; j > i; j--) { printf(" "); } for (int j = 0; j < (2 * i + 1); j++) { printf("%c", ch); } printf("\n"); }
* *** ***** ******* *********
printf("11. Filled Inverted Pyramid:\n"); for (int i = n; i > 0; i--) { for (int j = n - i; j > 0; j--) { printf(" "); } for (int j = 0; j < (2 * i - 1); j++) { printf("%c", ch); } printf("\n"); }
********* ******* ***** *** *
printf("12. Filled Diamond:\n"); for (int i = 0; i < n; i++) { for (int j = n - 1; j > i; j--) { printf(" "); } for (int j = 0; j <= i; j++) { printf("%c ", ch); } printf("\n"); } for (int i = 0; i < n - 1; i++) { for (int j = 0; j <= i; j++) { printf(" "); } for (int j = n - 1; j > i; j--) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * * * * * * * * * * * *
printf("13. Filled Hourglass:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { printf(" "); } for (int j = 0; j < (n - i); j++) { printf("%c ", ch); } printf("\n"); } for (int i = 1; i < n; i++) { for (int j = n - 1; j > i; j--) { printf(" "); } for (int j = 0; j <= i; j++) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
printf("14. Filled Rhombus:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n - i - 1; j++) { printf(" "); } for (int j = 0; j < n; j++) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * * * * * * * * * * * *
printf("15. Filled Parallelogram:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { printf(" "); } for (int j = 0; j < n * 2; j++) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
printf("16. Filled Hexagon:\n"); for (int i = 0; i < n / 2; i++) { for (int j = n / 2 - i; j > 0; j--) { printf(" "); } for (int j = 0; j < n + 1 * i; j++) { printf("%c ", ch); } printf("\n"); } for (int i = n / 2; i >= 0; i--) { for (int j = 0; j < n / 2 - i; j++) { printf(" "); } for (int j = 0; j < n + i; j++) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
printf("17. Filled Pentagon:\n"); for (int i = 0; i < n + 1; i++) { for (int j = n; j > i; j--) { printf(" "); } for (int j = 0; j < (i + 1); j++) { printf(" %c", ch); } printf("\n"); } for (int i = n / 2; i >= 0; i--) { for (int j = 0; j < n / 2 - i; j++) { printf(" "); } for (int j = 0; j < n + i; j++) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
printf("18. Filled Inverted Pentagon:\n"); for (int i = 0; i <= n / 2; i++) { for (int j = 0; j < n / 2 - i; j++) { printf(" "); } for (int j = 0; j < n + i; j++) { printf("%c ", ch); } printf("\n"); } for (int i = n + 1; i > 0; i--) { for (int j = n + 2; j > i; j--) { printf(" "); } for (int j = 0; j < i; j++) { printf("%c ", ch); } printf("\n"); }
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Learning to create these filled patterns in C is an excellent way to practice using nested loops and enhance your understanding of how loops work. By experimenting with different values and shapes, you can deepen your understanding of control structures in C and develop a keen eye for detail and logic. Whether you're a beginner or looking to brush up on your skills, these patterns provide a solid foundation for mastering loops in C programming.
We hope this guide has been helpful and encourages you to explore more complex patterns and designs. Happy coding!
For more tutorials and coding tips, be sure to subscribe to our blog and follow us on social media!
Atas ialah kandungan terperinci Menguasai Corak Penuh: Panduan Komprehensif dengan Contoh Kod. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!