调试程序,实现了命令行下输入字符串,支持修改,回调

#include <iostream>
#include <string>
#include <termios.h>
#include <unistd.h>

// 获取终端输入模式
termios getTerminalMode() {
    termios mode;
    tcgetattr(STDIN_FILENO, &mode);
    return mode;
}

// 设置终端输入模式
void setTerminalMode(termios mode) {
    tcsetattr(STDIN_FILENO, TCSANOW, &mode);
}

// 禁用终端回显和行缓冲
void disableTerminalEcho() {
    termios mode = getTerminalMode();
    mode.c_lflag &= ~(ICANON | ECHO);
    setTerminalMode(mode);
}

// 启用终端回显和行缓冲
void enableTerminalEcho() {
    termios mode = getTerminalMode();
    mode.c_lflag |= ICANON | ECHO;
    setTerminalMode(mode);
}

// 获取终端输入的字符
int getChar() {
    int ch;
    disableTerminalEcho();
    ch = getchar();
    enableTerminalEcho();
    return ch;
}

// 移动光标到指定位置
void moveCursor(int pos) {
    std::cout << "\033[" << pos << "G";
}

// 删除指定位置的字符
void deleteChar(std::string& input, int pos) {
    if (pos > 0 && pos <= input.length()) {
        input.erase(pos - 1, 1);
    }
}

int main() {
    std::string input;
    std::cout << "dd: "<<std::endl;
    std::cout.flush();

    int ch;
    int cursorPos = 0;

   moveCursor(0 + 1);
    while ((ch = getChar()) != '\n') {
        if (ch == 27) { // 方向键
            ch = getChar();
            if (ch == 91) {
                ch = getChar();
                if (ch == 68) { // 左箭头
                    if (cursorPos > 0) {
                        cursorPos--;
                        moveCursor(cursorPos + 1);
                    }
                } else if (ch == 67) { // 右箭头
                    if (cursorPos <= input.length()) {
                        cursorPos++;
                        moveCursor(cursorPos + 1);
                    }
                }
            }
        } else if (ch == 127) { // 退格键
            if (cursorPos > 0) {
                moveCursor(cursorPos);
                deleteChar(input, cursorPos );
                cursorPos--;
                std::cout << input.substr(cursorPos) << " ";
                moveCursor(cursorPos + 1);
            }
        } else {
            input.insert(cursorPos , 1, ch);
            cursorPos++;
	    moveCursor(cursorPos);
            std::cout << input.substr(cursorPos - 1);
	    moveCursor(cursorPos + 1);
        }
    }

    std::cout << std::endl;
    input.push_back('\0');
    std::cout << "Input: " << input << std::endl;
    std::cout << "Exiting the program. Goodbye!" << std::endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
一个字符串是回文数,当且仅当其正反两个方向的读法完全相同。一个简单的方法是将字符串反转,并与原始字符串进行比较,如果相同,则是回文数。 以下是使用 LC-3 汇编程序实现判断输入字符串是否为回文数的示例代码: ``` ; 判断输入字符串是否为回文数 .ORIG x3000 ; 初始化 LD R0, STR ; 读入字符串 LD R3, LEN ; 读入字符串长度 ADD R3, R3, #-1 ; 计算字符串末尾位置 ADD R4, R0, R3 ; 计算字符串末尾地址 LOOP: ; 读取前一个字符 LDR R1, R0, #0 ADD R0, R0, #1 ; 指向下一个字符 ; 读取后一个字符 LDR R2, R4, #-0 ADD R4, R4, #-1 ; 指向前一个字符 ; 比较字符是否相等 NOT R2, R2 ADD R2, R2, #1 ; 取反,变成 -c ADD R5, R1, R2 ; 如果字符相等,R5 == 0 BRz NEXT ; 字符不相等,不是回文数 LEA R0, NOT_PALINDROME ; 输出 "Not Palindrome" PUTS BR END NEXT: ; 继续比较下一个字符 ADD R3, R3, #-1 BRp LOOP ; 字符全部比较完,是回文数 LEA R0, PALINDROME ; 输出 "Palindrome" PUTS END: ; 停机 HLT ; 字符串 STR .STRINGZ "ABBA" ; 字符串长度 LEN .FILL #-1 ; 注意:需要减去字符串结尾的空字符 ; 输出消息 PALINDROME .STRINGZ "Palindrome" NOT_PALINDROME .STRINGZ "Not Palindrome" .END ``` 在上面的示例代码中,`LOOP` 标签用于循环比较字符串的每一个字符,`NEXT` 标签用于比较下一个字符。如果字符不相等,则输出 "Not Palindrome",并跳转到 `END` 标签停机。如果所有字符都比较完毕,则输出 "Palindrome"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值