#include <stdio.h>
void draw_triangle_fun(unsigned int length, unsigned int width)
{
unsigned int t_w, t_l;
char space = ' ', ch;
for (t_w = 0; t_w <= width; t_w++)
{
for (t_l = 0; t_l < length - t_w; t_l++)
{
printf("%c", space);
}
for (t_l = 0; t_l <= t_w; t_l++)
{
ch = t_l + '0';
printf("%c", ch);
}
// for (t_l = 0; t_l <= t_w; t_l++)
for (t_l = length - t_w; t_l <= length; t_l++)
{
if (t_l > (length - t_w))
{
ch = length - t_l + '0';
printf("%c", ch);
}
}
printf("\n");
}
for (t_w = 0; t_w < width; t_w++)
{
for (t_l = 0; t_l <= t_w; t_l++)
{
printf("%c", space);
}
for (t_l = 0; t_l < (length - t_w); t_l++)
{
ch = t_l + '0';
printf("%c", ch);
}
// for (t_l = 0; t_l < (length - t_w); t_l++)
for (t_l = (length - t_w); t_l > 0; t_l--)
{
// ch = (length - t_l) + '0';
if (((length - t_w) > t_l) && (t_l > 0))
{
ch = t_l - 1 + '0';
printf("%c", ch);
}
}
printf("\n");
}
}
int main()
{
unsigned int length, width;
printf("\nPlease enter length/width(1 ~ 9):");
scanf("%d", &width);
if (width > 9)
width = 9;
length = width;
printf("\n=============================================\n");
draw_triangle_fun(length, width);
printf("\n=============================================\n");
return 0;
}
Please enter length/width(1 ~ 9):9
=============================================
0
010
01210
0123210
012343210
01234543210
0123456543210
012345676543210
01234567876543210
0123456789876543210
01234567876543210
012345676543210
0123456543210
01234543210
012343210
0123210
01210
010
0
=============================================
沒有留言:
張貼留言