程序代码:
#include <locale.h> #include <stdio.h> #include <conio.h> void rhombus(size_t width); void main(void) { size_t number; setlocale(LC_ALL, "chs"); // 设定语言为中文输出 do { wprintf_s(L"\n菱形底宽(0-20, 0结束程序): "); scanf_s("%d", &number); if (number > 0 && number <= 20) { rhombus(number); } } while (number > 0); } void rhombus(size_t width) { const char character('*'); const char blank(' '); size_t i, j, k; int number; if (width % 2 == 0) { width++; } number = -1; for (i = 1; i <= width; i++) { if (i <= width / 2 + 1) { number += 2; } else { number -= 2; } for (j = 1; j <= (width - number) / 2; j++) { putch(blank); } for (k = 1; k <= number; k++) { putch(character); } putch('\n'); } }
授人以渔,不授人以鱼。