语言小结
  • program-language-note
  • contact
  • common
    • 代码风格
    • 概念语法
      • 类型
      • 注释
      • 字符
      • 语句
      • 操作符
      • 函数
        • 递归
    • 格式化参数
    • 源码结构
    • 数据结构
    • 名词
  • 电路
    • 内存地址
    • Untitled
    • Code
  • C
    • note
    • overview
      • helloworld.c
      • c标准
      • 关键字
      • tips
      • util.c
    • 语法
      • 函数
        • main
      • const
      • static
      • 作用域
    • 编译和运行
      • c代码内存模型
      • 预处理
        • include
        • define
    • 头文件
    • 基本数据类型
      • 整型
      • 枚举
      • 浮点型
      • 指针
      • 数组
      • 结构和联合
    • 指针&数组、指针&函数
    • API
    • 存储结构
    • 操作符
      • sizeof
    • typedef
    • 输入输出
    • 格式化参数
    • 左値右値
    • 性能思考
    • volatile
    • 字符串
      • find_char.c
    • 动态分配
      • alloc.h
      • alloc.c
      • alloc_usage.c
    • note
  • cpp
    • 资源
    • note
    • 数据结构
    • 智能指针
    • 编译过程
  • shell
    • usage
    • Untitled
  • Rust
    • overview
  • Lisp
    • Untitled
  • web
    • overview
      • index
      • 软件工具
      • ARIA规范
      • SEO
    • style
    • html
      • 标签、元素
        • 标签快记
        • 联系信息
        • 引用
        • 列表
        • 语言设置
        • meta
      • 页面结构
        • 图片
        • 视频
        • 引用css、js文件
      • 等价字符
      • 链接
        • 邮件
      • 表单
        • note
      • 表格
    • css
      • 字体
      • 布局
        • position
        • float
        • display
        • flexbox
    • js
    • note
  • java
    • note
    • java语言程序设计
    • 设计模式
      • 大话设计模式-吴强-2010
      • 大话设计模式-程杰
      • 设计模式-gof
      • 设计模式解析
      • 原则
      • 单例
    • java程序设计第10版-基础
    • java程序设计第10版-进阶
    • java核心技术第9版-I
    • jar包
    • 安全
    • 反射
  • python
    • note
    • index
    • 个人记忆点
    • 疑惑
    • simple
    • 精通Python爬虫框架scrapy
    • 语法
    • scrapy
      • notice
      • index
  • 汇编
    • Untitled
  • kotlin
    • index
    • note
    • by android
      • note
      • index
  • groovy
    • gradle
Powered by GitBook
On this page

Was this helpful?

  1. C
  2. overview

helloworld.c


#include <stdlib.h>
#include <stdio.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() {

    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;
}

/**
 * 读取列标号,如果超出规定范围则不予理会
 * @param columns
 * @param max
 * @return
 */
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;
    }
    //因为后续需要,num必须是偶数
    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;
    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';

}
PreviousoverviewNextc标准

Last updated 5 years ago

Was this helpful?