接收用户输入的一个大写字母,如“C”,要求输出如下字母金字塔,其中“_”代表空格:
__A
_ABA
ABCBA
具体要求:
(1) 容许接收多次输入,直到输入为“0 (零)”;
(2) 如果输入非大写字母,则给出提示,并要求重新输入;
代码:#include <stdlib.h>
int main()
/*输出一个字母金字塔*/
{
int n=0,m=0,t=0;
char a='/0',b='/0',c='/0',d='/0';
printf ("If you want to play this game please input a character./nIf you want end it please input 0./n");
do
{
scanf("%c",&d);
printf("please input an uppercase :/n");
scanf(" %c",&a);
if (a<'A'||a>'Z')
{
printf("you input a wrong character/n");
}
/*若输入的不是大写字母 则给用户一个提示*/
else
{
m=a-'A'+1;/*计算总共该输出m行*/
for (n=1;n<=m;n++)
{
for (t=0;t<m-n;t++)
printf(" ");/*输出每行该有的空格*/
for (b='A';b<='A'+n-1;b++)
printf("%c",b);/*正序输出字母*/
for (c='A'+n-2;c>='A';c--)
printf("%c",c);/*逆序输出字母*/
printf("/n");
}
printf("If you want end please input 0,or input anther character/n");
scanf(" %c",&d);
}
}while (d!='0');/*当d==0时结束循环*/
printf("The end!");
return 0;
}