C和指针 第1章 快速上手 1.1 简介

1.1 简介
    1.1.1 空白和注释
    1.1.2 预处理指令
    1.1.3 main函数
    1.1.4 read_column_numbers
    1.1.5 rearrange函数
快速上手
1.1 简介
程序是从标准输入读取文本并对其进行修改,然后把它写到标准输出中。
程序1.1首先读取一串列标号。这些列标号成对出现,表示输入行的列范围。这串列标号以一个负值结尾,作为结束标志。剩余的输入行被程序读入并打印,然后输入行中被选中范围的字符串被提取出来打印。注意,每行第1列的列标号为零。
输入: 
4 9 12 20 -1
abcdefghijklmnopqrstuvwxyz
Hello there, how are you?
I am fine, thanks.
See you!
Bye
/*
**这个程序从标准输入中读取输入行并在标准输出中打印这些输入行,
**每个输入行的后面一行是该行内容的一部分。
**输入的第1行是一串列标号,串的最后以一个负数结尾。
**这些列标号成对出现,说明需要打印的输入行的列的范围
**例如,0 3 10 12 -1表示第0列到第3列,第10列到第12列的内容将被打印。
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX_COLS 20     /*所能处理的最大列号*/
#define MAX_INPUT 1000 /*每个输入行的最大长度*/
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;           /*进行处理的列标号*/
    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( "Rearranged line: %s\n", output );
    } 
    return EXIT_SUCCESS;
}
/*
**读取列标号,如果超出规定范围则不予理会。 
*/
int read_column_numbers( int columns[], int max )
{
    int num = 0;
    int ch;
    /*
    **取得列标号,如果所读取的数小于0则停止。 
    */
    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; 
}
/*
**处理输入行,将指定列的字符连接在一起,输出行以NUL结尾。 
*/
void rearrange( char *output, char const *input, int n_columns, int const columns[] )
{
    int col;           /*columns数组的下标*/
    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'; 

/*程序1.1 重排字符                        rearrange.cpp */

/*输出:*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_40186813

你的能量无可限量。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值