Quick actions

cmd+k|ctrl+k

Navigation

Languages

aseskdddd

Snippet info

Language

C

Visibility

public

Author

hyrious

Created

2020-01-09T11:25:08Z

Updated

2020-01-09T11:25:08Z

#include <ctype.h>
#include <stdio.h>

int char2int(int ch) {
    if (ch == EOF) return -1;
    // 假设当前是 ASCII 环境
    return ch - '0';
}

int main() {
    // 当前字符
    int ch = EOF;
    // 上一个字符
    int prev = EOF;
    // 保存整数,假设它们都不超过 int
    int token_digit = 0;
    // 循环读取一个字符
    while ((ch = getchar()) != EOF) {
        // scan \d+ 尝试读一个整数
        if (isdigit(ch)) {
            token_digit = token_digit * 10 + char2int(ch);
        } else {
            // 处理之前读到的整数
            if (token_digit > 0) {
                // 输出 token_digit % 6 遍前一个字符
                for (int i = 0; i < token_digit % 6; ++i) {
                    putchar(prev);
                }
                token_digit = 0;
            }
            // 输出当前字符
            putchar(ch);
            prev = ch;
        }
    }
    // 处理之前读到的整数
    if (token_digit > 0) {
        // 输出 token_digit % 6 遍前一个字符
        for (int i = 0; i < token_digit % 6; ++i) {
            putchar(prev);
        }
        token_digit = 0;
    }
}
INFO