# C語言 圖形問題??

Rating
• Jeremy
Lv 4

E-mail: tsungjung411@yahoo.com.tw

---------------------------------------------------------

程式下載觀摩：

#include <stdio.h>

void upsideDownTriangle(int n) {

int i, j, k;

printf("等邊倒立三角形:\n");

for (i = n, j = 0; i > 0; i--, j++) {

for ( k = 0; k < j; k++) {

printf(" ");

}

for ( k = 0; k < i; k++) {

printf("* ");

}

printf("\n");

}

printf("\n");

}

void triangle(int n) {

int i, j, k;

printf("等邊正立三角形:\n");

for (i = 1, j = n - 1; i <= n; i++, j--) {

for ( k = 0; k < j; k++) {

printf(" ");

}

for ( k = 0; k < i; k++) {

printf("* ");

}

printf("\n");

}

printf("\n");

}

void rhomb(int n) {

int i, j, k;

printf("等邊菱形:\n");

for (i = 1, j = n - 1; i <= n; i++, j--) {

for ( k = 0; k < j; k++) {

printf(" ");

}

for ( k = 0; k < i; k++) {

printf("* ");

}

printf("\n");

}

for (i = n - 1, j = 1; i > 0; i--, j++) {

for ( k = 0; k < j; k++) {

printf(" ");

}

for ( k = 0; k < i; k++) {

printf("* ");

}

printf("\n");

}

printf("\n");

}

void trapezium(int n) {

int i, j, k;

printf("等邊梯形:\n");

for (i = 1, j = n - 1; i <= n; i++, j--) {

for ( k = 0; k < j; k++) {

printf(" ");

}

for ( k = 0; k < i + n - 1; k++) {

printf("* ");

}

printf("\n");

}

printf("\n");

}

void main() {

int n;

printf("Please input aa positive integer n= ");

scanf("%d", &n);

upsideDownTriangle(n);

triangle(n);

rhomb(n);

trapezium(n);

}

執行結果：

Please input aa positive integer n= 5

等邊倒立三角形:

＊　＊　＊　＊　＊

＊　＊　＊　＊

＊　＊　＊

＊　＊

＊

等邊正立三角形:

＊

＊　＊

＊　＊　＊

＊　＊　＊　＊

＊　＊　＊　＊　＊

等邊菱形:

＊

＊　＊

＊　＊　＊

＊　＊　＊　＊

＊　＊　＊　＊　＊

＊　＊　＊　＊

＊　＊　＊

＊　＊

＊

等邊菱形:

＊　＊　＊　＊　＊

＊　＊　＊　＊　＊　＊

＊　＊　＊　＊　＊　＊　＊

＊　＊　＊　＊　＊　＊　＊　＊

＊　＊　＊　＊　＊　＊　＊　＊　＊

Press any key to continue

Source(s): me