brainfuck 程序设计续(一)

2021-09-07

最近正在设计一种类似汇编语言的中间语言,从而将其转化成 brainfuck,编译器还在设计中,先放个程序:

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]<.[-]>+>>>>>>>,><<<<[-]>>>[-<<<+>>>]<>++++++++++++++++++++++++++++++++++++++++++++++++><<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]<>[-]>[-]>[-]>[-]>[-]<<<<<[->>+<<]<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>[[-]<<<<<<->>>->>><<<<<<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>]<<<<<<[[-]>+<]+>[-<->]<>>>[-]<<<><<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]+++++++++++++++++++++++++++++++++++++++++++++++++++++++++><>[-]>[-]>[-]>[-]>[-]<<<<<[->>+<<]<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>[[-]<<<<<<->>>->>><<<<<<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-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-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>]<<<<<<[[-]>+<]+>[-<->]<>>>[-]<<<><<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<><<<[-]>>[-<<+>>]<><<[->>>+<<<]>>>[-<<<+>>>>+<]>[-<+>]<[-<+>]<>+<[->-<]>[-<+>]<><<[-]>[-<+>]<><[->>+<<]>>[-<<+>>>+<]>[-<+>]<[-<+>]<]<<[->>>+<<<]>>>[-<<<+>>>>+<]>[-<+>]<[-<+>]<[[-]<<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]++++++++++><[-<[->>+>+<<<]>>[-<<+>>]<<>]<[-]>>>[-<<<+>>>]<<<><<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]<[-<+>]<>++++++++++++++++++++++++++++++++++++++++++++++++><[-<->]<><<<<<[-]>>>>[-<<<<+>>>>]<>,><<<<[-]>>>[-<<<+>>>]<>++++++++++++++++++++++++++++++++++++++++++++++++><<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]<>[-]>[-]>[-]>[-]>[-]<<<<<[->>+<<]<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>[[-]<<<<<<->>>->>><<<<<<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>]<<<<<<[[-]>+<]+>[-<->]<>>>[-]<<<><<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]+++++++++++++++++++++++++++++++++++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><[->>+<<]>>[-<<+>>>+<]>[-<+>]<[-<+>]<[[-]<<[->>>+<<<]>>>[-<<<+>>>>+<]>[-<+>]<[-<+>]<<<<<<[->>>>>>>+<<<<<<<]>>>>>>>[-<<<<<<<+>>>>>>>>+<]>[-<+>]<[-<+>]<[-<+>]<><<<[-]>>[-<<+>>]<><<<<<[->>>>>>+<<<<<<]>>>>>>[-<<<<<<+>>>>>>>+<]>[-<+>]<[-<+>]+><[-<+>]<><<<<<<[-]>>>>>[-<<<<<+>>>>>]<><<<<<[->>>>>>+<<<<<<]>>>>>>[-<<<<<<+>>>>>>>+<]>[-<+>]<[-<+>]<<<<[->>>>>+<<<<<]>>>>>[-<<<<<+>>>>>>+<]>[-<+>]<[-<+>]<>[-]>[-]>[-]>[-]>[-]<<<<<[->>+<<]<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>[[-]<<<<<<->>>->>><<<<<<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>]<<<<<<[[-]>+<]+>[-<->]<>>>[-]<<<><<[-]>[-<+>]<><[->>+<<]>>[-<<+>>>+<]>[-<+>]<[-<+>]<]<[-]++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><.[-]<.[-]<.[-]<.[-]<.[-]<.[-]>><<<[->>>>+<<<<]>>>>[-<<<<+>>>>>+<]>[-<+>]<[-<+>]<<[-]>[-<+>]<>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[->>>+<<<]>>>[-<<<+>>>>+<]>[-<+>]<[-<+>]<>[-]>[-]>[-]>[-]>[-]<<<<<[->>+<<]<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>[[-]<<<<<<->>>->>><<<<<<[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<>>[>+<-]>[>+<<+>-]>[<+>-]<[[-]>+<]+>[-<->]<<<<[->>>+<<<]>>>[[-]>+<]+>[-<->]<[>+<-]>[>+<-]>]<<<<<<[[-]>+<]+>[-<->]<>>>[-]<<<><<<[-]>>[-<<+>>]<><<[->>>+<<<]>>>[-<<<+>>>>+<]>[-<+>]<[-<+>]<[[-]++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++><<[->>>+<<<]>>>[-<<<+>>>>+<]>[-<+>]<[-<+>]<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<<<>[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<<<>>><[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<[[-]<-<-><[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<]<[-]<[[-]>+<]+>[-<->]<[[-]<+<<[->>>+>+<<<<]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<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<[[-]<-<-><[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<]<[-]<[[-]>+<]+>[-<->]<[[-]<+<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<<<>>>[-<<->>]<<<[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<<<>[->>>+>+<<<<]>>>>[-<<<<+>>>>]<<<<>>><[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<[[-]<-<-><[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[->>+>+<<<]>>>[-<<<+>>>]<<<>>[[-]>+<]>[-<+>]<<[[-]>[[-]>+<]<][-]>[-]>[-<<+>>]<<]<[-]<[[-]>+<]+>[-<->]<]<<<[-]>>[-<<+>>]<<>><<<[-]>>[-<<+>>]<>++++++++++++++++++++++++++++++++++++++++++++++++><[-<+>]<><.[-]]<[->>+<<]>>[-<<+>>>+<]>[-<+>]<[-<+>]++++++++++++++++++++++++++++++++++++++++++++++++><[-<+>]<><.[-]<[-]<[-]

上文中这个 10KB 的程序的功能是输入一个整数 n ( n ≤ 20 ) n(n\leq 20) n(n20),利用循环计算 1 + 2 + ⋯ + n 1+2+\cdots+n 1+2++n 的值,并输出,运行效果图。

运行结果

在线的可视化解释器太慢了,顺便给一下解释器吧:

/// brainfuck 的 C 语言解释器
///  author: GGN_2015 
/// VERSION: 2021-09-06 alpha

/// 命名规范:所有函数/全局变量 必须采用下划线明名规则
/// 所有函数/头文件/全局变量按照名称字典序排列

#define CODE_EXIT           ( 1)                                /// 退出信号
#define CODE_ERROR          (-1)                                /// 运行时出错
#define DEBUG               ( 1)                                /// 采用调试模式

#define EMPTY()             (stack_top <= 0)                    /// 判断栈是否为空

#define if_debug            if(DEBUG)                           /// 输出调试信息
#define if_show             if(1)                               /// 输出最终状态
#define MAX_CODE_LENGTH     (1048576)                           /// 程序的最大长度
#define min(A,B)            ((A)<=(B) ? (A) : (B))

#define PUSH(POS)           (stack_arr[stack_top ++] = (POS))
#define POP()               (stack_top --)

#define TOP()               (stack_arr[stack_top - 1])          /// 返回栈顶元素

/// -------------------------------------------------------------------------------- 头文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/// -------------------------------------------------------------------------------- 全局变量

int        end_pos                           ; /// 记录程序文件结尾的位置
char       file_path[256]                    ; /// 程序文件路径
char*      memory                            ; /// 使用内存
int        memory_limit    = 1048576         ; /// 默认 1 MB 内存
const int  memory_max      = 1 << 30         ; /// 内存最大字节数
int        pointer_begin   = 0               ; /// 程序开始执行时,指针指向的内存坐标
FILE*      source          = NULL            ; /// 程序源代码文件
int        status_flag     = 0               ; /// 当前程序的运行状态, 只用来接收函数返回值,不允许在 main 函数外赋值
char*      source_code     = NULL            ; /// 储存源代码文件
int*       next_pos        = NULL            ; /// 储存 '[' 与 ']' 的对应关系
int        stack_arr[MAX_CODE_LENGTH]        ; /// 匹配栈
int        stack_top       = 0               ; /// 栈顶指针

/// -------------------------------------------------------------------------------- 所有函数

void check_error    (const char*)   ; /// 检查是否出错
void check_pos      (int)           ; /// 检查某个位置是否可访问
int  delta_argv     (int, char**)   ; /// 根据命令行参数调整全局变量
char get_ins        ()              ; /// 读取一条指令 (可能是注释)
void help_file      ()              ; /// 输出帮助文件
int  init_memory    ()              ; /// 初始化内存
int  run_code       ()              ; /// 运行指定的程序

/// -------------------------------------------------------------------------------- 函数实现

void check_error(const char* function_name) { /// 检查程序出错
    if(status_flag == CODE_EXIT) { /// 直接退出程序
        exit(0);
    }else if(status_flag != 0) {
        printf("Error When %s. status_flag = %d\n", function_name, status_flag);
        exit(status_flag);
    }
}

void check_pos(int ptr) {
    if(0 <= ptr && ptr <= memory_limit - 1) {
        /// 检查该位置是否可访问
    }else {
        if_debug printf("check_pos: runtime error at ptr = %d\n", ptr); /// 非法访问
        exit(-1);
    }
}

int delt_argv(int argc, char* argv[]) { /// 处理命令行参数
    for(int i = 1; i < argc; i ++) {
        if(strcmp(argv[i], "-f") == 0) { /// 指定源代码文件文件
            if(i + 1 < argc) {
                strcpy(file_path, argv[i + 1]); /// 设置程序路径
                i ++;
            }else {
                printf("delt_argv: -f parament must be followed by a file path.\n");
                return CODE_ERROR;
            }
        }else if(strcmp(argv[i], "-m") == 0) { /// 设置内存限制
            if(i + 1 < argc) {
                memory_limit = atoi(argv[i + 1]);
                i ++;
            }else {
                printf("delt_argv: -m parament must be followed by a integer (memory limit BYTEs).\n");
                return CODE_ERROR;
            }
        }else if(strcmp(argv[i], "-h") == 0) { /// 显示帮助文档
            help_file();
            return CODE_EXIT;
        }else if(argv[i][0] == '-') { /// 未知参数
            printf("delta_argv: %s parament not defined!\n", argv[i]);
            return CODE_ERROR;
        }else {
            strcpy(file_path, argv[i]);
        }
    }
    return 0;
}

char get_ins() { /// 读取一条指令
    if(file_path[0] == 0) return getchar();
    return fgetc(source);
}

void help_file() { /// 输出帮助文件
    printf("brainfuck -f [source_code = ''] -m [memory limit = '1048576']\n");
}

int init_code() {
    source_code = (char *) malloc(MAX_CODE_LENGTH * sizeof(char));
    next_pos    = (int  *) malloc(MAX_CODE_LENGTH * sizeof( int)); /// 申请内存
    return 0;
}

int init_memory() { /// 初始化 memory 数组
    if(memory_limit > memory_max) {
        return CODE_ERROR; /// 超过最大内存限制
    }else {
        memory = (char *)malloc(memory_limit); /// 分配内存
        memset(memory, 0x00, memory_limit);
    }
    return 0;
}

int main(int argc, char* argv[]) { /// 主程序
    status_flag = delt_argv(argc, argv);    check_error("delt_argv");
    status_flag = init_memory();            check_error("init_memory");
    status_flag = init_code();              check_error("init_code");
    status_flag = run_code();               check_error("run_code");
    return 0;
}

int read_func(int pos) { /// 读取一个函数进入内存
    for(;; pos ++) {
        char ctmp = get_ins();
        if(ctmp == EOF) {
            end_pos = pos; /// 记录文件结尾的位置
            return 0; /// 读到文件结尾
        }else {
            if(ctmp == '<' || ctmp == '>' || ctmp == '+' || ctmp == '-' || ctmp == ',' || ctmp == '.') {
                source_code[pos] = ctmp;
            }else if(ctmp == '[') {
                source_code[pos] = ctmp;
                PUSH(pos);
            }else if(ctmp == ']') {
                source_code[pos] = ctmp;
                if(!EMPTY()) {
                    int last_pos = TOP(); POP();
                    next_pos[pos] = last_pos;
                    next_pos[last_pos] = pos; /// 记录匹配位置
                }else {
                    if_debug printf("read_func: ']' is more than '[', pos = %d\n", pos); /// 右括号比左括号多
                    return -1;
                }
            }else pos --; /// 其他字符均视为注释,不计入内存
        }
    }
    if(!EMPTY()) { /// 没有完成匹配
        if_debug printf("read_func: '[' is more than ']' at the end of the file.\n"); /// 左括号比右括号多
        return -1;
    }
    return 0;
}

int run_code() { /// 解释运行 .brfk 源代码程序
    if(file_path[0] != 0) {
        source = fopen(file_path, "r"); /// 读取程序文件
        if(source == NULL) {
            if_debug printf("run_code: filepath = %s is not accessible.\n", file_path); /// 文件无法打开
            return CODE_ERROR;
        }
    }
    status_flag = read_func(0); check_error("read_func"); /// 读入整个程序
    int ptr = pointer_begin;
    for(int i = 0; i < end_pos; i ++) {
        char ctmp = source_code[i];
        switch(ctmp) {
            case '<': ptr --; break;
            case '>': ptr ++; break;
            case '+': check_pos(ptr); memory[ptr] ++;                       break;
            case '-': check_pos(ptr); memory[ptr] --;                       break;
            case ',': check_pos(ptr); memory[ptr] = getchar();              break;
            case '.': check_pos(ptr); putchar(memory[ptr]);                 break;
            case '[': check_pos(ptr); if(memory[ptr] == 0) i = next_pos[i]; break;
            case ']': check_pos(ptr); if(memory[ptr] != 0) i = next_pos[i]; break;
            default:  if_debug printf("run_code: error char(%d) found at i = %d\n", ctmp, i); return -1;
        }
    }
    if_debug printf("\n--------------------\nrun_code: program end successfully.\n"); /// 程序正常结束
    if_show {
        printf("ptr = %d\n", ptr);
        for(int i = 0; i < min(20, memory_limit); i ++) {
            printf("%3d ", (unsigned char)memory[i]);
        }
        putchar('\n');
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Brainfuck,是一种极小化的计算机语言,它是由Urban Müller在1993年创建的。由于fuck在英语中是脏话,这种语言有时被称为brainf*ck或brainf***,甚至被简称为BF。 【内含:BF解释器,BF解释器源码,BF写的几个小程序】 ReadMe: Brainfuck 编程语言 [图灵完全] [8条指令] 语法: > 指针加一 < 指针减一 + 指针指向的字节的值加一 - 指针指向的字节的值减一 . 输出指针指向的单元内容(ASCII码) , 输入内容到指针指向的单元(ASCII码) [ 如果指针指向的单元值为零,向后跳转到对应的]指令的次一指令处 ] 如果指针指向的单元值不为零,向前跳转到对应的[指令的次一指令处 特性: 8KB 环状内存(初始化为0) <>操作不会越界 加减操作环状 +-操作不会溢出(0xFF + 为 0x00) 文件说明: bf.exe 解释器 Usage: bf [-options] source where options include: -b buffered input (default mode) 缓冲输入(按回车才输入,默认) -i not buffered input 无缓冲输入 -e not buffered input without echo 无缓冲输入且不回显 bf.cpp 解释器的源代码(纯C实现) hello.txt HelloWorld程序 up.txt 这个程序将你的输入(小写字母)转换为大写(回车结束) add.txt 这个程序对两个一位数做加法,并输出结果(如果结果也只有一位数的话)(例如:输入2+3) mul.txt 这个程序对两个一位数做乘法,并输出结果(如果结果也只有一位数的话)(例如:输入2*3) factor.txt 这个程序分解多位数的因子,并输出结果(例如:输入1000) numwarp.txt 这个程序输入 ()-./0123456789abcdef 和空格的字符串,显示很有趣的排列结果(例如:输入520 1314) prime.txt 这个程序输入一个多位整数,输出从1到这个整数间的所有素数(例如:输入100) quine.txt 这个程序输出源代码本身 [以上程序,基本上依靠回车确认]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值