| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 461 人关注过本帖
标题:题目的意思不大理解
只看楼主 加入收藏
strayfish
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-3-17
结帖率:100%
收藏
 问题点数:0 回复次数:3 
题目的意思不大理解
题目这么说的:编写一个程序,打印输入中单词长度的直方图。水平方向的直方图比较容易绘制,垂直方向的直方图则要困难些。
我现在不是想知道代码怎么写,我是觉得我不懂这道题到底是什么意思。
搜索更多相关主题的帖子: 直方图 
2013-03-18 19:33
strayfish
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-3-17
收藏
得分:0 
#include <stdio.h>

#define MAXHIST 15
#define MAXWORD 11
#define IN        1
#define OUT        0

int main(){
    int c, i, nc, state;
    int len;
    int maxvalue;
    int ovflow;
    int wl[MAXWORD];
   
    state = OUT;
    nc = 0;
    ovflow = 0;
    for ( i = 0 ; i < MAXWORD ; ++i)
        wl[i] = 0;
    while ((c = getchar()) != EOF) {
        if ( c == ' ' || c == '\n' || c == '\t' ){
            state = OUT;
            if ( nc > 0 )
                if ( nc < MAXWORD )
                    ++wl[nc];
                else
                    ++ovflow;
                nc = 0;
        }
        else if ( state == OUT){
            state = IN;
            nc = 1;            
        }
        else
            ++nc;
    }
    maxvalue = 0;
    for ( i = 1; i < MAXWORD; ++i )
        if ( wl[i] > maxvalue )
            maxvalue = wl[i];
    for ( i = 1; i < MAXWORD ; ++i ){
        printf ( "%5d - %5d : ", i, wl[i] );
        if ( wl[i] > 0 ){
            if(( len = wl[i] * MAXHIST / maxvalue ) <= 0)
                len = 1;
        }
        else
            len = 0;
        while ( len > 0 ) {
            putchar ( '*' );
            --len;
        }
        putchar( '\n' );
    }
    if ( ovflow > 0 )
        printf ( "There are %d words >= %d\n", ovflow, MAXWORD );
}



这是参考代码,我读了一下,有点儿晕,还是不懂这道题的意思。。
2013-03-18 19:39
strayfish
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-3-17
收藏
得分:0 
我还拿来运行了。。表示还是看不明白啊啊啊啊啊啊
2013-03-18 19:41
strayfish
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2013-3-17
收藏
得分:0 
好吧,我把书上给的注释加上。。。

#include <stdio.h>

#define MAXHIST 15
#define MAXWORD 11
#define IN        1
#define OUT        0

int main(){
    int c, i, nc, state;
    int len;                //length of each bar
    int maxvalue;            //maximum value for wl[]
    int ovflow;                //number of overflow words
    int wl[MAXWORD];        //word length counters
   
    state = OUT;
    nc = 0;                    //number of chars in a word
    ovflow = 0;                //number of words >= MAXWORD
    for ( i = 0 ; i < MAXWORD ; ++i)
        wl[i] = 0;
    while ((c = getchar()) != EOF) {
        if ( c == ' ' || c == '\n' || c == '\t' ){
            state = OUT;
            if ( nc > 0 )
                if ( nc < MAXWORD )
                    ++wl[nc];
                else
                    ++ovflow;
                nc = 0;
        }
        else if ( state == OUT){
            state = IN;
            nc = 1;            //beginning of a new word
        }
        else
            ++nc;            //inside a word
    }
    maxvalue = 0;
    for ( i = 1; i < MAXWORD; ++i )
        if ( wl[i] > maxvalue )
            maxvalue = wl[i];
    for ( i = 1; i < MAXWORD ; ++i ){
        printf ( "%5d - %5d : ", i, wl[i] );
        if ( wl[i] > 0 ){
            if(( len = wl[i] * MAXHIST / maxvalue ) <= 0)
                len = 1;
        }
        else
            len = 0;
        while ( len > 0 ) {
            putchar ( '*' );
            --len;
        }
        putchar( '\n' );
    }
    if ( ovflow > 0 )
        printf ( "There are %d words >= %d\n", ovflow, MAXWORD );
}
2013-03-18 19:54
快速回复:题目的意思不大理解
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023300 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved