C语言 练手代码——(1)棱形

#include<stdio.h>
#define H 6           //上半部分行数
int main()
{
 void top(char a);
 void bottom(char a);
 char elem;
 scanf("%c", &elem);
 top(elem);
 bottom(elem);
 return 0;
}
/*实现上半部分*/
void top(char a)
{
 int temp1 = H - 1;
 int temp2 = 0;
 int h = 0;
 for (; h < H; h++, temp1--, temp2++)
 {
  if (h == 0)
  {
   int nub1 = temp1;
   while (nub1--)
    printf(" ");
   printf("%c\n", a);
  }
  else
  {
   int nub1 = temp1;
   int nub2 = temp2 * 2 - 1;
   while (nub1--)
    printf(" ");
   printf("%c", a);
   while (nub2--)
    printf(" ");
   printf("%c\n", a);
  }
 }
}
void bottom(char a)
{
 int temp1 = 1;
 int temp2 = H - 2;
 int h = H - 1;
 for (; h > 0; h--, temp1++, temp2--)
 {
  if (h == 1)
  {
   int nub1 = temp1;
   while (nub1--)
    printf(" ");
   printf("%c\n", a);
  }
  else
  {
   int nub1 = temp1;
   int nub2 = temp2 * 2 - 1;
   while (nub1--)
    printf(" ");
   printf("%c", a);
   while (nub2--)
    printf(" ");
   printf("%c\n", a);
  }
 }
}



/*********************************************************/

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/* * *                            修缮一下                                 * * */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*********************************************************/



/* * * * * * * * * * * * * * * * */
/* * * 修改时间 2018 7 月 9 日 * */
/* * * 作者 段振杰 * * * * * * * */
/* * * * * * * * * * * * * * * * */
#include<stdio.h>
int H;
int main3()
{
 void top(char a);
 //void bottom(char a);
 printf("请依次输入棱形的 组成符号 和 一半的 行数(通过回车确认)\n");
 char elem = getchar();
 fflush(stdin);
 scanf("%d", &H);
 top(elem);
 //bottom(elem);
 return 0;
}
/* * * * * * * * * * * * * * * * * */
/* * * 函数名: top        * * * * */
/* * * 功能:打印空心棱形  * * * * */
/* * * * * * * * * * * * * * * * * */
void top(char a)
{
 /*  *  *  *  *  *  */
 /*  实现上半部分  */
 /*  *  *  *  *  *  */
 int temp1 = H - 1;
 int temp2 = 0;
 int h = 0;
 for (; h < H; h++, temp1--, temp2++)
 {
  int nub1 = temp1;
  int nub2 = temp2 * 2 - 1;
  if (h == 0)
  {
   while (nub1--)
    printf(" ");
   printf("%c\n", a);
  }
  else
  {
   while (nub1--)
    printf(" ");
   printf("%c", a);
   while (nub2--)
    printf(" ");
   printf("%c\n", a);
  }
 }
 /*  *  *  *  *  *  */
 /*  实现下半部分  */
 /*  *  *  *  *  *  */
 temp1 = 1;
 temp2 = H - 2;
 h = H - 1;
 for (; h > 0; h--, temp1++, temp2--)
 {
  if (h == 1)
  {
   int nub1 = temp1;
   while (nub1--)
    printf(" ");
   printf("%c\n", a);
  }
  else
  {
   int nub1 = temp1;
   int nub2 = temp2 * 2 - 1;
   while (nub1--)
    printf(" ");
   printf("%c", a);
   while (nub2--)
    printf(" ");
   printf("%c\n", a);
  }
 }
}

猜你喜欢

转载自blog.csdn.net/weixin_42510998/article/details/80956668