gpt4 book ai didi

c - 用 C99 构建字母金字塔

转载 作者:行者123 更新时间:2023-11-30 16:35:05 25 4
gpt4 key购买 nike

#include <stdio.h>
#define FINISH 'Z'
int main(void){
int spacenm, currentspacenm, i;
char ini, inirecord;
int rownm, currentrownm;
printf("PLease enter a random uppercase letter: ");
scanf("%c", &inirecord);
spacenm = rownm = FINISH - inirecord;
for (i = 0; i<= spacenm; i++)
printf(" ");
printf("%c\n", inirecord);
i = 0;
for (currentrownm = 1; currentrownm <= rownm; currentrownm ++){
for (currentspacenm = 1; currentspacenm <= spacenm; currentspacenm++)
printf(" ");
spacenm--;
for (ini = inirecord; ini<=inirecord+i; ini++)
printf("%c", ini);
i++;
for (; ini>=inirecord; ini--)
printf("%c", ini);
printf("\n");
}
return 0;
}

我想知道是否有更好的方法,需要更少的变量或者使用更少的冗余代码结构来提高效率。

最佳答案

我们初学者应该互相帮助。:)

您的程序中不会检查用户输入是否有效。

此外,如果我没有记错的话,并非所有编码表(例如 EBCDIC 表)都包含连续的大写字母。

无需像您所做的那样编写单独的代码片段来输出金字塔的第一行

for (i = 0; i<= spacenm; i++)
printf(" ");
printf("%c\n", inirecord);

使用格式化可以消除循环数量。

考虑到一般情况而不是这种格式规范

scanf("%c", &inirecord);
^^^^

您应该使用以下内容

scanf(" %c", &inirecord);
^^^^^

否则控制字符也会被读取。

这是我的三分钱。:)

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(void)
{
const char *letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const size_t N = strlen( letters );

while ( 1 )
{
char alpha[3];
_Bool exit_status;
const char *p;

do
{
printf( "PLease enter a random uppercase letter (Enter - exit ): " );
exit_status = fgets( alpha, sizeof( alpha ), stdin ) == NULL || alpha[0] == '\n';

if ( !exit_status )
{
p = NULL;
if ( alpha[1] == '\n' )
{
alpha[0] = toupper( ( unsigned char )alpha[0] );
p = strchr( letters, alpha[0] );
}
}
} while ( !exit_status && !p );

if ( exit_status ) break;

int n = &letters[N] - p;

putchar( '\n' );
for ( int i = 0; i < n; i++ )
{
printf( "%*c", n - i, alpha[0] );
int j = 0;
while ( j != i ) putchar( p[++j] );
while ( j != 0 ) putchar( p[--j] );
putchar( '\n' );
}
putchar( '\n' );
}

return 0;
}

程序输出可能如下所示

PLease enter a random uppercase letter (Enter - exit ): Z

Z

PLease enter a random uppercase letter (Enter - exit ): Y

Y
YZY

PLease enter a random uppercase letter (Enter - exit ): X

X
XYX
XYZYX

PLease enter a random uppercase letter (Enter - exit ): A

A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
ABCDEFEDCBA
ABCDEFGFEDCBA
ABCDEFGHGFEDCBA
ABCDEFGHIHGFEDCBA
ABCDEFGHIJIHGFEDCBA
ABCDEFGHIJKJIHGFEDCBA
ABCDEFGHIJKLKJIHGFEDCBA
ABCDEFGHIJKLMLKJIHGFEDCBA
ABCDEFGHIJKLMNMLKJIHGFEDCBA
ABCDEFGHIJKLMNONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQRQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQRSRQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQRSTSRQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQRSTUTSRQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQRSTUVUTSRQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQRSTUVWVUTSRQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQRSTUVWXWVUTSRQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQRSTUVWXYXWVUTSRQPONMLKJIHGFEDCBA
ABCDEFGHIJKLMNOPQRSTUVWXYZYXWVUTSRQPONMLKJIHGFEDCBA

PLease enter a random uppercase letter (Enter - exit ):

关于c - 用 C99 构建字母金字塔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49081591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com