题目:
编写一个程序,打印输入中单词的直方图。
[CODE]
[amro@localhost C]$ vim pt1.13.c
1 #include <stdio.h>
2
3 #define MAXHIST 15 /*max length of histogram*/
4 #define MAXWORD 11 /*max length of a word*/
5 #define IN 1
6 #define OUT 0
7
8 main()
9 {
10 int c, i, nc, state;
11 int len; /*maxium value for wl[]*/
12 int maxvalue;
13 int ovflow;
14 int wl[MAXWORD]; /*为什么这里是MAXWORD
?不是wl[i]呢?*/ /*word length counters*/
15
16 state = OUT;
17 nc = 0;
18 ovflow = 0;
19 for (i = 0; i < MAXWORD; ++i)
20 wl[i] = 0;
21 while ((c = getchar()) != EOF) {
22 if (c == ‘ ‘||c == ‘\n’||c == ‘\t’){
23 state = OUT;
24 if (nc > 0)
25 if (nc < MAXWORD)
26 ++ wl[nc]; /*这里的wl[nc]和35行的nc
有什么区别?看不明白*/
27 else
28 ++ ovflow;
29
30 nc = 0; /*这里的nc = 0 是什么意思?*/
31 }else if (state = OUT){
32 state = IN;
33 nc = 1; /*beginning of a new
word*/
34 }else
35 ++nc; /*inside a word*/
36 }
37 maxvalue = 0;
38 for (i = 1; i < MAXWORD; ++i)
39 if (wl[i] > maxvalue)
40 maxvalue = wl[i];
41
42 for (i = 1; i < MAXWORD; ++i){
43 printf(“%5d – %5d :”, i, wl[i]);
44 if (wl[i] > 0){
45 if ((len = wl[i] * MAXHIST / maxvalue) <= 0)
46 len = 1;
47 }else
48 len = 0;
49 while (len > 0){
50 putchar (‘*’);
51 – len;
52 }
53 putchar (‘\n’);
54
55
56 }
57 if (ovflow > 0)
58 printf (“There are %d words >= %d \n”, ovflow,
MAXWORD);
59
60
61 }
62
~
[/CODE]
上面是程序,已经有3个地用中文方标出了问题,请解释一下,
谢谢。
ps:
我的专业不是学计算机的,当然也不包括编程,我买了一本《C
语言程序设计》Brian W. Kernighan和Dennis M. Ritchie那
本。自学起来感觉很困难,不知道有什么好办法学好这本书么?
谢谢。
>> 本文固定链接: http://www.vcgood.com/archives/3252
14 int wl[MAXWORD]; /*为什么这里是MAXWORD ?不是wl呢?*/ /*word length counters*/