Untitled

 avatar
unknown
plain_text
5 months ago
1.8 kB
4
Indexable
#include <stdio.h>
#include <string.h>

#define MAX_SIZE 500

int main() {
    char content[MAX_SIZE] = "";  // 編輯器內容
    char input[MAX_SIZE];         // 輸入序列
    int cursor = 0;               // 游標位置

    fgets(input, MAX_SIZE, stdin);  // 讀取輸入

    for (int i = 0; input[i] != '\0'; i++) {
        if (input[i] == '/') {
            // 判斷特殊指令
            if (strncmp(&input[i], "/backspace", 10) == 0) {
                if (cursor > 0) {
                    // 刪除游標前一字符
                    memmove(&content[cursor - 1], &content[cursor], strlen(&content[cursor]) + 1);
                    cursor--;
                }
                i += 9;  // 跳過 "/backspace"
            } else if (strncmp(&input[i], "/newline", 8) == 0) {
                // 插入換行符
                memmove(&content[cursor + 1], &content[cursor], strlen(&content[cursor]) + 1);
                content[cursor] = '\n';
                cursor++;
                i += 7;  // 跳過 "/newline"
            } else if (strncmp(&input[i], "/left", 5) == 0) {
                // 游標左移
                if (cursor > 0) {
                    cursor--;
                }
                i += 4;  // 跳過 "/left"
            } else if (strncmp(&input[i], "/right", 6) == 0) {
                // 游標右移
                if (cursor < strlen(content)) {
                    cursor++;
                }
                i += 5;  // 跳過 "/right"
            }
        } else {
            // 普通字符輸入
            memmove(&content[cursor + 1], &content[cursor], strlen(&content[cursor]) + 1);
            content[cursor] = input[i];
            cursor++;
        }
    }

    // 輸出結果
    printf("%s", content);

    return 0;
}
Editor is loading...
Leave a Comment