gpt4 book ai didi

控制台不显示直方图

转载 作者:行者123 更新时间:2023-11-30 19:59:24 26 4
gpt4 key购买 nike

我运行了以下代码,但控制台上没有任何结果......

#include <stdio.h>

#define MAXWORDLEN 10

int main(void)
{
int c;
int inspace = 0;
long lengtharr[MAXWORDLEN + 1];
int wordlen = 0;

int firstletter = 1;
long thisval = 0;
long maxval = 0;
int thisidx = 0;
int done = 0;

for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
{
lengtharr[thisidx] = 0;
}

while(done == 0)
{
c = getchar();

if(c == ' ' || c == '\t' || c == '\n' || c == EOF)
{
if(inspace == 0)
{
firstletter = 0;
inspace = 1;

if(wordlen <= MAXWORDLEN)
{
if(wordlen > 0)
{
thisval = ++lengtharr[wordlen - 1];
if(thisval > maxval)
{
maxval = thisval;
}
}
}
else
{
thisval = ++lengtharr[MAXWORDLEN];
if(thisval > maxval)
{
maxval = thisval;
}
}
}
if(c == EOF)
{
done = 1;
}
}
else
{
if(inspace == 1 || firstletter == 1)
{
wordlen = 0;
firstletter = 0;
inspace = 0;
}
++wordlen;
}
}

for(thisval = maxval; thisval > 0; thisval--)
{
printf("%4d | ", thisval);
for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
{
if(lengtharr[thisidx] >= thisval)
{
printf("* ");
}
else
{
printf(" ");
}
}
printf("\n");
}
printf(" +");
for(thisidx = 0; thisidx <= MAXWORDLEN; thisidx++)
{
printf("---");
}
printf("\n ");
for(thisidx = 0; thisidx < MAXWORDLEN; thisidx++)
{
printf("%2d ", thisidx + 1);
}
printf(">%d\n", MAXWORDLEN);

return 0;
}

最佳答案

getchar 仍然是一个 int POSIX and C standards 。您是否忘记包含 stdio.h 或包含重新定义它的内容?这个例子对我有用:

#include <stdio.h>
int main()
{
int c;
c = getchar();
}

关于控制台不显示直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1413725/

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