LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 670|回复: 8

请教各位下面程序!

[复制链接]
发表于 2005-4-20 12:18:35 | 显示全部楼层 |阅读模式
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_COLS 20
#define MAX_INPUT 1000

int read_column_numbers( int column[], int max );
void rearrange( char *output, char const *input,
                int n_columns, int const columns[] );

int main( void )
{
        int n_columns;
        int columns[MAX_COLS];
        char input[MAX_INPUT];
        char output[MAX_INPUT];

        n_columns = read_column_numbers( columns, MAX_COLS );

        while( gets( input ) != NULL ){
                printf( "Original input: %s\n", input );
                rearrange( output, input, n_columns, columns );
                printf( "Rearrangde line: %s\n", output );
        }

        return EXIT_SUCCESS;
}

int read_column_numbers( int columns[], int max )
{
        int num = 0;
        int ch;

        while( num < max && scanf( "%d", &columns[num] ) == 1
                && columns[num] >= 0 )
                num += 1;
        if( num % 2 != 0 ){
                puts( "Last column number is not paired." );
                exit( EXIT_FAILURE );
        }

        while( (ch = getchar()) != EOF && ch != '\n' )//不理解为什么要这个句子!!
                ;

        return num;
}

void rearrange( char *output, char const *input,
                int n_columns, int const columns[] )
{
        int col;
        int output_col;
        int len;

        len = strlen( input );
        output_col = 0;

        for( col = 0; col < n_columns; col += 2 ){
                int nchars = columns[col + 1] - columns[col] + 1;

                if( columns[col] >= len ||
                        output_col == MAX_INPUT - 1 )
                        break;

                if( output_col + nchars > MAX_INPUT - 1 )
                        nchars = MAX_INPUT - output_col - 1;

                strncpy( output + output_col, input + columns[col],
                        nchars );
                output_col += nchars;
        }
        output[output_col] = '\0';
}
这个程序是用来重新排列输入的内容,首先输入需要读取的列号,然后根据列号打印出内容:
例如列号为4 7
则把这个列号内的内容打印,但是我对上面注释的地方不理解,麻烦各位解释一下,谢谢!
发表于 2005-4-20 14:30:43 | 显示全部楼层
越过空行。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-4-20 16:01:11 | 显示全部楼层
书上说这句话是“丢弃该行中包含最后一个数字的那部分内容”,我不理解这是什么意思!!!
这段程序是<<C和指针>>中的。
回复 支持 反对

使用道具 举报

发表于 2005-4-21 13:40:51 | 显示全部楼层
Post by jovesky
书上说这句话是“丢弃该行中包含最后一个数字的那部分内容”,我不理解这是什么意思!!!
这段程序是<<C和指针>>中的。

说的很清楚呀
比如你说的输入“4 7”,用户也可能输入“4 7dfcce”等等的,这句的作用就是把7后面的多余输入全部扔掉。
回复 支持 反对

使用道具 举报

发表于 2005-4-21 14:20:51 | 显示全部楼层
Post by doubleelec
越过空行。

sorry, 等号不等号看错了。应该是丢弃从当前位置开始直到行尾的内容。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-4-21 17:13:43 | 显示全部楼层
Post by libinary
说的很清楚呀
比如你说的输入“4 7”,用户也可能输入“4 7dfcce”等等的,这句的作用就是把7后面的多余输入全部扔掉。

我知道是丢弃后面的内容,我的意思是说我不理解为什么这个语句代表这个含义。
回复 支持 反对

使用道具 举报

发表于 2005-4-21 17:50:05 | 显示全部楼层
while( (ch = getchar()) != EOF && ch != '\n' )
;
语句的意思就是如果读入的字符不是'\n'或EOF就循环,而循环体什么也不做,达到的效果就是一直读入字符,直到行尾或输入结束
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-4-22 12:04:25 | 显示全部楼层
Post by libinary
while( (ch = getchar()) != EOF && ch != '\n' )
;
语句的意思就是如果读入的字符不是'\n'或EOF就循环,而循环体什么也不做,达到的效果就是一直读入字符,直到行尾或输入结束

对的,你说的我明白,不过既然他会一直读到行尾或到输入结束,那怎么丢弃后面的字符呢
回复 支持 反对

使用道具 举报

发表于 2005-4-22 16:22:37 | 显示全部楼层
程序读入了后面的字符但是什么也没有做,就等于丢弃了这些字符。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表