| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1192 人关注过本帖
标题:c和指针 第一章的不懂的1,output_col == MAX_INPUT - 1 这个output_col是怎 ...
只看楼主 加入收藏
Feiyiyi1
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2017-10-22
结帖率:62.5%
收藏
已结贴  问题点数:20 回复次数:4 
c和指针 第一章的不懂的1,output_col == MAX_INPUT - 1 这个output_col是怎么回事 2, if( output_col +
/*
** This program reads input lines from the standard input and prints
** each input line, followed by just some portions of the lines, to
** the standard output.
**
** The first input is a list of column numbers, which ends with a
** negative number.  The column numbers are paired and specify
** ranges of columns from the input line that are to be printed.
** For example, 0 3 10 12 -1 indicates that only columns 0 through 3
** and columns 10 through 12 will be printed.
*/
#include
#include
#include
#define    MAX_COLS    20    /* max # of columns to process */
#define    MAX_INPUT    1000    /* max len of input & output lines */
int    read_column_numbers( int columns[], int max );
void rearrange( char *output, char const *input, int n_columns, int const columns[] );
int main( void )
{
    int        n_columns;            /* # of columns to process */
    int        columns[MAX_COLS];    /* the columns to process */
    char    input[MAX_INPUT];    /* array for input line */
    char    output[MAX_INPUT];    /* array for output line */
    /*
    ** Read the list of column numbers
    */
    n_columns = read_column_numbers( columns, MAX_COLS );
    /*
    ** Read, process and print the remaining lines of input.
    */
    while( gets( input ) != NULL){
        printf( "Original input : %s\n", input );
        rearrange( output, input, n_columns, columns );
        printf( "Rearranged line: %s\n", output );
    }
    return EXIT_SUCCESS;
}
/*
** Read the list of column numbers, ignoring any beyond the specified
** maximum.
*/
int read_column_numbers( int columns[], int max )
{
    int    num = 0;
    int    ch;
    /*
    ** Get the numbers, stopping at eof or when a number is < 0.
    */
    while( num < max && scanf( "%d", &columns[num] ) == 1 && columns[num] >= 0 )//大于等于0的数字,这个数字所在数组columns中的列号小于20
        num += 1;
    /*
    ** Make sure we have an even number of inputs, as they are
    ** supposed to be paired.
    */
    if( num % 2 != 0 ){                             //判断输入的数字个数是否为奇数
        puts( "Last column number is not paired." );
        exit( EXIT_FAILURE );
    }
    /*
    ** Discard the rest of the line that contained the final
    ** number.
    */
    while( (ch = getchar()) != EOF && ch != '\n' )//EOF是个宏,在stdil.h中定义,值为 -1,也称结束标志。
        ;                                         //当输入 ctrl+z 或 ctrl+c等组合键,getchar返回结束标志。
    return num;                                   //返回符合条件的数的个数
}
/*
** Process a line of input by concatenating the characters from
** the indicated columns.  The output line is then NUL terminated.
*/
void rearrange( char *output, char const *input, int n_columns, int const columns[] )
{
    int    col;        /* subscript for columns array */
    int    output_col;    /* output column counter */
    int    len;        /* length of input line */
    len = strlen( input );
    output_col = 0;
    /*
    ** Process each pair of column numbers.
    */
    for( col = 0; col < n_columns; col += 2 ){
        int    nchars = columns[col + 1] - columns[col] + 1;  //字符列区间
        /*
        ** If the input line isn't this long or the output
        ** array is full, we're done.
        */
        if( columns[col] >= len || output_col == MAX_INPUT - 1 )  //如果输入行结束或者或者输出行数组已满,就结束任务
            break;
        /*
        ** If there isn't room in the output array, only copy
        ** what will fit.
        */
        if( output_col + nchars > MAX_INPUT - 1 )
            nchars = MAX_INPUT - output_col - 1;
        /*
        ** Copy the relevant data.
        */
        strncpy( output + output_col, input + columns[col],
            nchars );
        output_col += nchars;
    }
    output[output_col] = '\0';
}
搜索更多相关主题的帖子: input line the int output 
2017-10-22 20:34
Feiyiyi1
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2017-10-22
收藏
得分:0 
if( output_col + nchars > MAX_INPUT - 1 )
            nchars = MAX_INPUT - output_col - 1;
主要就是这个output_col 到底是怎么回事
2017-10-22 20:35
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10570
专家分:43038
注 册:2014-5-20
收藏
得分:20 
看注释
int    output_col;    /* output column counter */
2017-10-22 20:44
Feiyiyi1
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2017-10-22
收藏
得分:0 
while( num < max && scanf( "%d", &columns[num] ) == 1 && columns[num] >= 0 )//大于等于0的数字,这个数字所在数组columns中的列号小于20
        num += 1;
    /*
    ** Make sure we have an even number of inputs, as they are
    ** supposed to be paired.
    */
    printf("%d\n",num);                     在这里我加了一句printf("%d",num);请问当我输入 2 4 5 8 -1 这几个数的时候num的计数为4是怎么回事,因为num从0开始计数  最后的-1不满足while循环 所以我怎么看这个num的值做后应该是3,当然3对于整个程序是不满足要求的

2017-10-23 10:51
Feiyiyi1
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2017-10-22
收藏
得分:0 
回复 3楼 吹水佬
while( num < max && scanf( "%d", &columns[num] ) == 1 && columns[num] >= 0 )//大于等于0的数字,这个数字所在数组columns中的列号小于20
        num += 1;
    /*
    ** Make sure we have an even number of inputs, as they are
    ** supposed to be paired.
    */
    printf("%d\n",num);                     在这里我加了一句printf("%d",num);请问当我输入 2 4 5 8 -1 这几个数的时候num的计数为4是怎么回事,因为num从0开始计数  最后的-1不满足while循环 所以我怎么看这个num的值做后应该是3,当然3对于整个程序是不满足要求的
2017-10-23 10:51
快速回复:c和指针 第一章的不懂的1,output_col == MAX_INPUT - 1 这个output_co ...
数据加载中...
 
   



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

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