如何用C++编写控制台终端
How to write Console terminal with C++
我正在准备考试,需要你的帮助。我必须用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 **argv
和int argc
。argv
将是参数,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++编译不匹配的类型。
- 如何在linux终端中同时编译和运行c++代码
- C++:将控制台输出存储在宏中更好吗
- 终端不会为C++文件创建.exe文件吗
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- SSH通过/sbin/SSH无法读取RSA密钥文件(从控制台运行)
- C++控制台应用程序阻止退出
- 如何修复此iFile以将.txt输出到控制台
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- 为什么控制台要求输入,即使代码中没有输入
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- 用于窗口的 HID 终端
- 控制台输出在 Qt5 中未正确显示
- C++出现控制台错误.我无法识别源代码的问题
- 独立于平台的方式捕获控制台/终端关闭事件
- 如何用C++编写控制台终端
- C++程序,控制台/终端输出。如何实现"updating text"
- 如何创建控制台终端
- 有没有关于用c++编写控制台/终端应用程序的书?