---
# System prepended metadata

title: 12094 - Coding in terminal
tags: [NTHUOJ]

---

# 12094 - Coding in terminal
## 題解:
在處裡input跟output方面, 跟11269 - Text Editor那題是相同的,
\s [x, y]可以用sscanf來讀取x跟y

Print答案時要注意每行最多只能有x個字元,
有x個字元了或是遇到換行字元就換行

**Reference:** http://tw.gitbook.net/c_standard_library/c_function_sscanf.html
## Code:
```c=1
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXN 100005

int x = 80, y = 24, cursor = 0;
char input[MAXN], output[MAXN], temp[MAXN];

void newChar(char ch){
    strcpy(temp, &output[cursor]);
    strcpy(&output[cursor + 1], temp);
    output[cursor++] = ch;
}

void backspace(){
    if(cursor > 0){
        strcpy(temp, &output[cursor]);
        strcpy(&output[--cursor], temp);
    }
}

void left(){
    if(cursor > 0)
        cursor--;
}

void right(){
    if(output[cursor] != 0)
        cursor++;
}

int main(){
    fgets(input, MAXN, stdin);
    for (int i = 0; i < MAXN; i++){
        if(input[i] == '\n' || input[i] == 0)
            break;
        if(input[i] == '\\'){
            if(input[i+1] == 'n'){
                newChar('\n');
                i++;
            }
            else if(input[i+1] == 'b'){
                backspace();
                i++;
            }
            else if(input[i+1] == 'l'){
                left();
                i++;
            }
            else if (input[i+1] == 'r') {
                right();
                i++;
            }
            else if (input[i+1] == 's'){
                sscanf(input + i + 3, "[%d %d]", &x, &y);
                while (input[i] != ']')
                    i++;
            }
        }
        else if(isalpha(input[i]) || input[i] == ' ')
            newChar(input[i]);
    }
    for (int i = 0, count = 0; i < strlen(output); i++){
        printf("%c", output[i]);
        count++;
        if(output[i] == '\n')
            count = 0;
        if(count >= x){
            if(output[i + 1] != '\n'){
                printf("\n");
                count = 0;
            }
        }
    }
    return 0;
}
```
如果不用sscanf的話, s的部分改成如下
```c
x = 0;
i += 4;
while (input[i] != ' '){
    x = (x * 10) + (input[i] - '0');
    i++;
}
y = 0;
i++;
while(input[i] != ']'){
    y = (y * 10)  + (input[i] - '0');
    i++;
}
```
###### tags: `NTHUOJ`