K& R2,第1.6节,练习1-13 [英] K&R2, section1.6, exercise 1-13

查看:64
本文介绍了K& R2,第1.6节,练习1-13的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个程序运行没有任何错误,但它没有做我想要的事情

它要做:


--------- ---- PROGRAM --------------

/ * K& R2,1.6节数组;练习1-13。


声明:

编写一个程序,在

输入中打印单词长度的直方图。

水平条形图可以很容易地绘制直方图;垂直

方向更具挑战性。

方法:


1.)我们将每个wordn的lenth存储在一个数组中。

2.)为了保持简洁,数组大小为1000,即

它只能容纳1000个字。

3.)EOF之后是遇到然后屏幕上印有a Hostogram of starts(*)






4 。)每行直方图将包含多个星。 ==长度

这个词。


* /


#include< stdio.h>


#define IN 1

#define OUT 0

#define MAXWORDS 1000


int main()

{

int c;

int i = 0;

int aindex = 0 ;

int j = 0; / *i,j,count是一般指数计数器* /


int nc = 0; / *单词的长度或单词中的字符数* /


int lwords [MAXWORDS + 1];


int nw = 0 ; / *单词数* /


/ *每个单词的长度都存储在这个数组中

并将在最后打印* /


int state = IN;


while(((c = getchar())!= EOF)&&(nw< = MAXWORDS ))

{

++ nc;


if(c ==''''|| c ==' '\t''|| c ==''\ n'')

{

state = OUT;

- nc;

}


否则if(state == OUT)

{

lwords [ aindex ++] = nc;

++ nw;

州= IN;

}


}


printf(" ----------打印HISTOGRAM ----------- \ n");


for(i = 0; i< aindex; ++ i)

{

for(j = 0; j< lwords [i]; ++ j)

putchar(''*'');


putchar(''\ n'');

}


返回0;

}

----------- - OUTPUT ---------------

[arch @ voodo kr2] $ gcc -std = c99 -pedantic -Wall -Wextra ex_1-13.c

[arch @ voodo kr2] $ ./a.out

like < br $> b $ b ----------打印直方图-----------

[arch @ voodo kr2] $

this programme runs without any error but it does not do what i want
it to do:

------------- PROGRAMME --------------
/* K&R2, section 1.6 Arrays; Exercise 1-13.

STATEMENT:
Write a program to print a histogram of the lengths of words in its
input.
It is easy to draw the histogram with the bars horizontal; a vertical
orientation is more challenging.
Method:

1.) we will store lenth of each wordn an array.
2.) for keeping simplicity, array size is 1000, i.e.
it can hold only 1000 words.
3.) after EOF is encountered then the "a Hostogram of starts ( * )
"will be
printed on the screen.

4.) each line of histogram will contain number of "stars" == length
of that word.

*/

#include <stdio.h>

#define IN 1
#define OUT 0
#define MAXWORDS 1000

int main()
{
int c;
int i = 0;
int aindex = 0;
int j = 0; /* "i,j,count" are general index counters */

int nc = 0; /* length of word or number of characters in a word */

int lwords[MAXWORDS + 1];

int nw = 0; /* number of words */

/* length of each word is stored in this array
and will be printed in the end */

int state = IN;

while( ((c = getchar()) != EOF) && (nw <= MAXWORDS) )
{
++nc;

if(c == '' '' || c == ''\t'' || c == ''\n'')
{
state = OUT;
--nc;
}

else if(state == OUT)
{
lwords[aindex++] = nc;
++nw;
state = IN;
}

}

printf("---------- printing HISTOGRAM -----------\n");

for(i = 0; i < aindex; ++i)
{
for(j = 0; j < lwords[i]; ++j)
putchar(''*'');

putchar(''\n'');
}

return 0;
}
------------- OUTPUT ---------------
[arch@voodo kr2]$ gcc -std=c99 -pedantic -Wall -Wextra ex_1-13.c
[arch@voodo kr2]$ ./a.out
like
---------- printing HISTOGRAM -----------
[arch@voodo kr2]$

推荐答案

gcc -std = c99 -pedantic -Wall -Wextra ex_1-13.c

[arch @ voodo kr2]
gcc -std=c99 -pedantic -Wall -Wextra ex_1-13.c
[arch@voodo kr2]


./ a.out

喜欢

----------打印HISTOGRAM ----- ------

[arch @ voodo kr2]
./a.out
like
---------- printing HISTOGRAM -----------
[arch@voodo kr2]







这篇关于K&amp; R2,第1.6节,练习1-13的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆