Willkommen zu unserem umfassenden Leitfaden zum Erstellen verschiedener Hohlmuster mithilfe von Schleifen in der C-Programmierung! In diesem Tutorial gehen wir Schritt für Schritt durch, wie man 18 verschiedene Hohlmuster zeichnet. Diese Muster reichen von Grundformen wie Quadraten und Dreiecken bis hin zu komplexeren Formen wie Rauten, Sechsecken und Fünfecken. Jedes Muster wird mithilfe verschachtelter Schleifen erstellt, was es zu einer hervorragenden Übung für Anfänger macht, um Kontrollstrukturen in C zu üben. Lassen Sie uns eintauchen!
Den gesamten Code finden Sie in unserem GitHub-Repository.
Bevor wir mit den Mustern beginnen, ist es wichtig, das Konzept der verschachtelten Schleifen zu verstehen. Eine verschachtelte Schleife ist eine Schleife innerhalb einer anderen Schleife. Diese Struktur ist besonders nützlich für die Handhabung mehrdimensionaler Arrays und für die Generierung von Mustern. In C sieht eine typische verschachtelte Schleifenstruktur so aus:
for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { // Code to execute } }
Erklärung:
int n = 5; // size of the square char ch = '*'; printf("1. Hollow Square:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == 0 || i == n - 1 || j == 0 || j == n - 1) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Ausgabe:
* * * * * * * * * * * * * * * *
Erklärung:
printf("2. Hollow Right Triangle:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < i + 1; j++) { if (i == n - 1 || j == 0 || j == i) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Ausgabe:
* * * * * * * * * * * *
Erklärung:
printf("3. Hollow Inverted Right Triangle:\n"); for (int i = 0; i < n; i++) { for (int j = n; j > i; j--) { if (i == 0 || j == n || j == i + 1) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Ausgabe:
* * * * * * * * * * * *
Erklärung:
printf("4. Hollow 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 + 1; j++) { if (i == n - 1 || j == 0 || j == i) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Ausgabe:
* * * * * * * * * * * *
Erklärung:
printf("5. Hollow Right Aligned Inverted Triangle:\n"); for (int i = 0; i < n; i++) { for (int j = 1; j < i + 1; j++) { printf(" "); } for (int j = n; j > i; j--) { if (i == 0 || j == n || j == i + 1) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Ausgabe:
* * * * * * * * * * * *
Erklärung:
printf("6. Hollow Right Pascal Triangle:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < i + 1; j++) { if (j == 0 || j == i) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); } for (int i = 0; i < n; i++) { for (int j = n; j > i + 1; j--) { if (j == n || j == i + 2) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Ausgabe:
* * * * * * * * * * * * * * * *
Erklärung:
printf("7. Hollow 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++) { if (j == 0 || j == i) { printf("%c ", ch); } else { printf(" "); } } 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--) { if (j == n - 1 || j == i + 1) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Ausgabe:
* * * * * * * * * * * * * * * *
Erklärung:
printf("8. Hollow Equilateral Triangle:\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++) { if (j == 0 || j == 2 * i || i == n - 1) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Ausgabe:
* * * * * * * * * * * * * * * *
Erklärung:
printf("9. Hollow Inverted Equilateral Triangle:\n"); for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { printf(" "); } for (int j = 2 * n - 1; j > 2 * i; j--) { if (j == 2 * n - 1 || j == 2 * i + 1 || i == 0) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Output:
* * * * * * * * * * * * * * * *
Explanation:
printf("10. Hollow Pyramid:\n"); for (i = 0; i < n; i++) { for (j = n - 1; j > i; j--) { printf(" "); } for (j = 0; j < (2 * i + 1); j++) { if (i == n - 1 || j == 0 || j == i * 2 ) { printf("%c", ch); } else { printf(" "); } } printf("\n"); }
Output:
* * * * * * * *********
Explanation:
printf("11. Hollow Inverted Pyramid:\n"); for (i = n; i > 0; i--) { for (j = n - i; j > 0; j--) { printf(" "); } for (j = 0; j < (2 * i - 1); j++) { if (j == 0 || i == n || j == (i-1) * 2 ) { printf("%c", ch); } else { printf(" "); } } printf("\n"); }
Output:
********* * * * * * * *
Explanation:
printf("12. Hollow Diamond:\n"); for (i = 0; i < n; i++) { for (j = n - 1; j > i; j--) { printf(" "); } for (j = 0; j < i + 1; j++) { if (j == 0 || j == i) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); } for (i = 0; i < n; i++) { for (j = 0; j < i + 1; j++) { printf(" "); } for (j = n - 1; j > i; j--) { if (j == n - 1 || j == i + 1) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Output:
* * * * * * * * * * * * * * * *
Explanation:
printf("13. Hollow Hourglass:\n"); for (i = 0; i < n; i++) { for (j = 0; j < i; j++) { printf(" "); } for (j = 0; j < (n - i) ; j++) { if (j == 0 || i == 0 || j == n - i - 1) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); } for (i = 1; i < n; i++) { for (j = n - 1; j > i; j--) { printf(" "); } for (j = 0; j < (i + 1); j++) { if (i == n - 1 || j == 0 || j == i) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Output:
* * * * * * * * * * * * * * * * * * * * * * *
Explanation:
printf("14. Hollow Rhombus:\n"); for (int i = 0; i < n; i++) { for (int j = n - 1; j > i; j--) { printf(" "); } for (int j = 0; j < n; j++) { if (i == 0 || i == n - 1 || j == 0 || j == n - 1) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Output:
* * * * * * * * * * * * * * * *
Explanation:
printf("15. Hollow Parallelogram:\n"); for (i = 0; i < n; i++) { for (j = 0; j < i; j++) { printf(" "); } for (j = 0; j < n * 2; j++) { if (i == n - 1 || i == 0 || j == 0 || j == n * 2 - 1) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * *
Explanation:
printf("16. Hollow Hexagon:\n"); for (i = 0; i < n / 2; i++) { for (j = n / 2 - i; j > 0; j--) { printf(" "); } for (j = 0; j < n + 1 * i; j++) { if ( i == 0 || j == 0 || j == n * i) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); } for (i = n / 2; i >= 0; i--) { for (j = 0; j < n / 2 - i; j++) { printf(" "); } for (j = 0; j < n + i; j++) { if (i == n - 1 || i == 0 || j == 0 || j == n + i - 1) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Output:
* * * * * * * * * * * * * * * *
Explanation:
printf("17. Hollow Pentagon:\n"); for (i = 0; i < n+1; i++) { for (j = n ; j > i; j--) { printf(" "); } for (j = 0; j < (i + 1); j++) { if ( j == 0 || i == 0 || j == i ) { printf(" %c", ch); } else { printf(" "); } } printf("\n"); } for (i = n / 2; i >= 0; i--) { for (j = 0; j < n / 2 - i; j++) { printf(" "); } for (j = 0; j < n + i; j++) { if (i == n - 1 || i == 0 || j == 0 || j == n + i - 1) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Output:
* * * * * * * * * * * * * * * * * * * *
Explanation:
printf("18. Hollow 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++) { if (i == n - 1 || i == 0 || j == 0 || j == n + i - 1) { printf("%c ", ch); } else { printf(" "); } } 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++) { if ( j == 0 || j == i - 1) { printf("%c ", ch); } else { printf(" "); } } printf("\n"); }
Output:
* * * * * * * * * * * * * * * * * * * *
In conclusion, we have explored a variety of patterns using loops and conditional statements in C, each producing different geometric shapes and designs. These patterns include solid and hollow variants of squares, triangles, pyramids, diamonds, hourglasses, rhombuses, parallelograms, hexagons, and pentagons. Understanding and implementing these patterns helps to strengthen programming logic, loop constructs, and conditionals, which are fundamental concepts in computer science.
By practicing these patterns, you can enhance your problem-solving skills and improve your ability to visualize and implement complex patterns in code. These exercises also provide a solid foundation for more advanced programming tasks and algorithms.
Das obige ist der detaillierte Inhalt vonHohlmuster beherrschen: Ein umfassender Leitfaden mit Codebeispielen. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!