IT’s Portfolio

[Rust] How to study Rust? - "Copycat" (3) 본문

Development Study/Rust

[Rust] How to study Rust? - "Copycat" (3)

f1r3_r41n 2023. 8. 28. 14:18
728x90
반응형

각 줄에 대하여 심볼, 명령어, 피연산 등 세 파트를 분리한 후, 심볼 10칸, 명령어 10칸, 피연산자 10칸으로 줄을 맞추어 출력(빈 파트는 공백 출력)

 

입력 파일

sample.txt
814 B

 

소스 코드

use std::{fs, io::Read, error::Error, process};

fn read_file() -> Result<String, Box<dyn Error>> {
    let mut f = fs::File::open("sample.txt")?;
    let mut contents = String::new();
    f.read_to_string(&mut contents)?;

    Ok(contents)
}

fn split_contents<'a>(contents: &'a str) -> (Vec<&'a str>, Vec<&'a str>, Vec<&'a str>) {
    let mut symbol = vec![];
    let mut command = vec![];
    let mut operand = vec![];
    for line in contents.lines() {
        let words = line.split("\t").collect::<Vec<&str>>();
        if words.len() == 4 {
            symbol.push(words[1]);
            command.push(words[2]);
            operand.push(words[3]);
        }
    }
    (symbol, command, operand)
}

fn print(args: Vec<&str>) {
    for i in 0..args.len() {
        if i%10==9 {
            println!("{:10}", args[i]);
        } else {
            print!("{:10} ", args[i]);
        }
    }
}

fn main() {
    let contents = read_file().unwrap_or_else(
        |err| {
            eprintln!("Error!\n{}", err);
            process::exit(1);
        }
    );
    let (symbol, command, operand) = split_contents(&contents);
    println!("Symbol:");
    print(symbol);
    println!("\nCommand:");
    print(command);
    println!("\nOperand:");
    print(operand);
}

 

결과

Symbol:
COPY       FIRST      CLOOP                                                             ENDFIL               
                                                       EOF        THREE      ZERO       RETADR     LENGTH    
BUFFER     RDREC                 RLOOP                                                                       
           EXIT                  INPUT      MAXLEN     WRREC      WLOOP                                      
                                 OUTPUT                
Command:
START      STL        JSUB       LDA        COMP       JEQ        JSUB       J          LDA        STA       
LDA        STA        JSUB       LDA        RSUB       BYTE       WORD       WORD       RESW       RESW      
RESB       LDX        LDA        TD         JEQ        RD         COMP       JEQ        STCH       TIX       
JLT        STX        RSUB       BYTE       WORD       LDX        TD         JEQ        LDCH       WD        
TIX        JLT        RSUB       BYTE       END        
Operand:
1000       RETADR     RDREC      LENGTH     ZERO       ENDFIL     WRREC      CLOOP      EOF        BUFFER    
THREE      LENGTH     WRREC      RETADR                C'EOF'     3          0          1          1         
4096       ZERO       ZERO       INPUT      TLOOP      INPUT      ZERO       EXIT       BUFFER,X   MAXLEN    
RLOOP      LENGTH                X'F1'      4096       ZERO       OUTPUT     WLOOP      BUFFER,X   OUTPUT    
LENGTH     WLOOP                 X'05'      FIRST      

 

출처

728x90
반응형
Comments