如何用C++编写控制台终端

How to write Console terminal with C++

本文关键字:控制台 终端 何用 C++      更新时间:2023-10-16

我正在准备考试,需要你的帮助。我必须用C++编写我自己的控制台终端,它必须以这种方式工作:

示例:

:>plus 5 7 "hit ENTER"
:>12
:>minus 10 12 "hit ENTER"
:>-2
:>combine Hello World "hit ENTER"
:>HelloWorld
:>run netstat "hit ENTER"
:>runs netstat
:>help
:>plus int1 int2
minus int1 int2
combine string1 string2
run ?????
:>exit
program exits

对于主块,我认为它会像这个一样

int main(void) {
    string x;
    while (true) {
        getline(cin, x);
        detect_command(x);
    }
    return 0;
}

功能类似于

void my_plus(int a, int b) {
    cout << a + b;
}
void my_minus(int a, int b) {
    cout << a - b;
}
void my_combine(string a, string b) {
    ?????????????;
}
void my_run(?????????) {
    ???????????;
}

最后的detect_command

void detect_command(string a) {
    const int arr_length = 10;
    string commands[arr_length] = { "plus", "minus", "help", "exit" };
    for (int i = 0; i < arr_length; i++) {
        if (a.compare(0, commands[i].length(), commands[i]) == 0) {
            ?????????????????????;
        }
    }
}

????-意思是我不知道该写什么。

帮助使此程序正常工作。谢谢

我将使用减号运算作为示例。。。

制作这样的结构:

struct cmd_struct {
   const char *name;
   void (*func) (void *data);
};

由于你的函数参数不一样,你必须为每个参数制作一个struct ure,例如:

struct minus_op {
   int rhs;
   int lhs;
};

并使用cmd_struct作为数组,如下所示:

static cmd_struct commands[] = {
   { "minus", &my_minus },
    ...
};

my_minus则为:

void my_minus(void *data) {
    struct minus_op *mop = data;
     ... do the computation and return ...
}

并循环通过它来检测所使用的命令:

for (int i = 0; i < sizeof(commands) / sizeof(commands[0]); ++i) {
   if (strcmp(commands[i].name, a) == 0) {
        ... prepare the data ...
        commands[i].func(data);
    }
}

旁注:为了从命令行获取函数参数,请使用拆分器,例如空白。为此使用矢量,并将该矢量传递给detect_command

还要注意:去掉本例中使用的void参数,并像main()中那样使用char **argvint argcargv将是参数,argc将是传递给函数的参数数。例如,如果你对程序说:

>> minus 5 1

argc应为2(5和1),argv[0]="5",argv[1]="1"。

既然您知道了它背后的想法,那么实现一个更灵活的版本就交给您了。

调用相应的函数来处理每个单词。例如:

enum commands {
  PLUS,
  MINUS,
  HELP,
  EXIT
  //....
};
int detect_command(string a) {
    const int arr_length = 10;
    string commands[arr_length] = { "plus", "minus", "help", "exit" };
    for (int i = 0; i < arr_length; i++) {
        if (a.compare(0, commands[i].length(), commands[i]) == 0)
         return i;    
    }
    return -1; //unknow word
}

将字符串赋给detect_command(),函数将相应的整数返回给enum commands(这是我们的i值),如果单词未知,则返回-1。然后,您可以编写这样的函数来使用和处理detect_command():确定的值

void run_command(int cmd)
{
   switch(cmd) {
    case PLUS: run_plus(); break;
    case MINUS: run_minus(); break;
    // and so on to all comamnds available
   default: error("unknow command");
   }
}

每个函数run_*()都应该根据自己的规则继续进行命令解析,即"加号"命令后面应该跟着一个整数、一个空格,然后是另一个整数,对吗?run_plus()必须对其进行验证,然后计算结果。例如:

//pseudo code
void run_plus()
{
   //input here is one after "plus" word
   //here we must validate our valid input: two digits split by a white-spaces
  int x = parse_digit();
  check(white-space);
  int y = parse_digit();
  int r = x + y;
  display_result(r);
}

注意:我不是C++程序员;我做了detect_command()代码修改,让你明白我的想法。我甚至不知道它是否会用C++编译不匹配的类型。