是否可以使用输入作为对字符串的调用
Is it possible to use input as a call to a string?
我正在创建一个命令行符号生成器。这个想法是当你用符号名称调用程序时,它会输出符号。我想知道我是否可以将输入字符串作为命令传递给 cout。
对于我添加的每个符号,我必须手动添加"else if"语句。
#include <bits/stdc++.h>
using namespace std;
string const notfound = R"~(
______
_________ .---""" """---.
:______.-': : .--------------. :
| ______ | | : : |
|:______B:| | | Little Error: | |
|:______B:| | | | |
|:______B:| | | Symbol not | |
| | | | found. | |
|:_____: | | | | |
| == | | : : |
| O | : '--------------' :
| o | :'---...______...---'
| o |-._.-i___/' ._
|'-.____o_| '-. '-...______...-' -._
:_________: .____________________ -.___.-.
.'.eeeeeeeeeeeeeeeeee.'. :___:
.'.eeeeeeeeeeeeeeeeeeeeee.'.
:____________________________:
)~";
string const butterfly = R"~(
$¶¶$¶¶$¶$
$¶¶$ø¢¢øø17¶$
¶¢1 7oøoø o7¶1
11¶¶¶¶¶¶¶¶¶ø ø¶ø 1oø¢o1ø o¶¢
¶¶¢¶ø¢¢¢¢77oø¶¶¶ ¶¢7ø$øoo7o$77¶o
¶¶7 7o77177777oø¶¶1 ¶øooo77777oø7¶¶
¶¶¶7o¢øø77ø¢ooooø¶¶¶¶¶¶¢oooo7177¢7o¶7
¶¶7 oooooooo77177o7øø¢¢ø¢ooooøø¢¶
7¶¢o7¢øoo7717oøø¶¶øoø¢ooo¢¢ooooo$7
7¶¶ø17oo7oø¶øøooøooooo777o¢oo71 o¶1
1¶¶$oø$$¢111¢1o¶¶ø7oøøo7ooooø7¢1¶
ø177 o1 ooo ¢ø ¶ø7oooø¢oo1¢1ø7¶
¶¢¢7o7oo¢oøo ø¶ ¶¶ooo77o7ø¶1 o¶
1ø$oøo1øø¢¢7o ¶ø ø¶¢$$¢$¶77oø¶7
7¶17ø77¢7711¶7¶ ¢¢ø 71¢¶1
ø¶$¢øø71oøø¢¢¶ øø¶¶¶¶¶o
7¶oø¶¶¶¢77¶¶
$¶¶ø¢¶¶¶7
)~";
void Print_Symbol(string x){
if(x == "butterfly")
cout<<butterfly<<endl;
// Add else if for every new symbol
else
cout<<notfound<<endl;
}
int main(int argc,char* argv[]){
if(argc < 2){
cout<<"To get a symbol enter: sym Symbol_Name"<<'n';
cout<<"Available Symbols: "<<'n';
cout<<"hello, hi,butterfly,.."<<'n';
}
for(int i=1;i<argc;i++){
string x = argv[i];
Print_Symbol(x);
}
return 0;
}
符号很少,这不是问题。但是当我将符号移动到另一个文件时,我希望能够在不重新编译主程序的情况下添加符号
有没有可能这样做
int main(int argc,char* argv[]){
if(argc < 2){
cout<<"To get a symbol enter: sym Symbol_Name"<<'n';
cout<<"Available Symbols: "<<'n';
cout<<"hello, hi,butterfly,.."<<'n';
}
for(int i=1;i<argc;i++){
string x = argv[i];
// pass the string to cout
cout<<x<<'n';
}
return 0;
}
C++是一种静态语言。我不明白你怎么能做到这一点,至少现在还没有。但是要绕过else/if语句,有多种方法。
解决方案 1:使用 std::map
通过使用 std::map
模板类和一个简单的std::find
可以避免使用 else if 语句。
void Print_Symbol(string x){
static const std::map<string, string> symbs ({
{"butterfly", butterfly} // add more here
});
auto res = symbs.find(x);
if (res == symbs.end())
cout<< notfound <<endl;
else
cout << *res << endl;
}
解决方案 2:使用 std::vector
或 ...
实际上与解决方案 1 相同。
解决方案 3:使用文件
在您的情况下,您可以将每个符号放在其自己的文件中,用户输入的是文件名。
解决方案 4:使用开关盒
我知道在 c++ 中你不能在开关情况下使用字符串,但您可以将每个字符串转换为一个数字,然后将其放入开关大小写中。不过,我不建议在这里使用它。
还有其他解决方案,但恐怕它们不适合您遇到的这个问题(与解决方案 4 相同(。
您可以以自己的方式改进此代码,也可以使用其他方式,例如 eval。 重要的是要说,当您允许执行文字注入的内容(代码(时,安全方面可能至关重要。
#include <bits/stdc++.h>
using namespace std;
string const notfound = R"~(
______
_________ .---""" """---.
:______.-': : .--------------. :
| ______ | | : : |
|:______B:| | | Little Error: | |
|:______B:| | | | |
|:______B:| | | Symbol not | |
| | | | found. | |
|:_____: | | | | |
| == | | : : |
| O | : '--------------' :
| o | :'---...______...---'
| o |-._.-i___/' ._
|'-.____o_| '-. '-...______...-' -._
:_________: .____________________ -.___.-.
.'.eeeeeeeeeeeeeeeeee.'. :___:
.'.eeeeeeeeeeeeeeeeeeeeee.'.
:____________________________:
)~";
string const butterfly = R"~(
$¶¶$¶¶$¶$
$¶¶$ø¢¢øø17¶$
¶¢1 7oøoø o7¶1
11¶¶¶¶¶¶¶¶¶ø ø¶ø 1oø¢o1ø o¶¢
¶¶¢¶ø¢¢¢¢77oø¶¶¶ ¶¢7ø$øoo7o$77¶o
¶¶7 7o77177777oø¶¶1 ¶øooo77777oø7¶¶
¶¶¶7o¢øø77ø¢ooooø¶¶¶¶¶¶¢oooo7177¢7o¶7
¶¶7 oooooooo77177o7øø¢¢ø¢ooooøø¢¶
7¶¢o7¢øoo7717oøø¶¶øoø¢ooo¢¢ooooo$7
7¶¶ø17oo7oø¶øøooøooooo777o¢oo71 o¶1
1¶¶$oø$$¢111¢1o¶¶ø7oøøo7ooooø7¢1¶
ø177 o1 ooo ¢ø ¶ø7oooø¢oo1¢1ø7¶
¶¢¢7o7oo¢oøo ø¶ ¶¶ooo77o7ø¶1 o¶
1ø$oøo1øø¢¢7o ¶ø ø¶¢$$¢$¶77oø¶7
7¶17ø77¢7711¶7¶ ¢¢ø 71¢¶1
ø¶$¢øø71oøø¢¢¶ øø¶¶¶¶¶o
7¶oø¶¶¶¢77¶¶
$¶¶ø¢¶¶¶7
)~";
void Print_Symbol(string x){
if(x == "butterfly")
cout<<butterfly<<endl;
// Add else if for every new symbol
else
cout<<notfound<<endl;
}
void executeMe() {
Print_Symbol("butterfly");
std::cout << "executeMe()";
}
int main(int argc,char* argv[]){
std::map<std::string, std::function<void()>> functions;
functions["executeMe"] = executeMe;
if(argc < 2){
cout<<"To get a symbol enter: sym Symbol_Name"<<'n';
cout<<"Available Symbols: "<<'n';
cout<<"hello, hi,butterfly,.."<<'n';
}
for(int i=0;i<argc;i++){
string x = argv[i];
if (functions.find(x) != functions.end()) {
cout << " test " << x << endl;
functions[x]();
}
}
return 0;
}
相关文章:
- 调用CreateProcess()并获取字符串的返回值
- Visual c ++,使用字符串引用/指针调用 dll 函数
- 接收字符串并使用它来调用方法C++
- 如果结构中的字符串比使用的 p/调用签名长或短,会发生什么情况?
- 在字符串函数中抛出'char const*'实例后调用的终止
- 如何将字符串从 C++/CLI 方法返回到调用它的非托管C++
- 为什么在调用C#DLL时不需要提供字符串缓冲区
- 没有使用两种类型的字符串进行匹配的函数调用
- 添加字符串类型的类成员会导致调用基类函数而不是子函数
- C++ 通过字符串调用类中的成员
- 如何将字符串调用到引号中
- 使用QString或字符串调用结构对象
- C++按字符串调用函数,比较PHP的性能,如何在C++中优化代码
- 使用字符串调用方法时出错:类型为'size_t和
- 外部字符串调用导致32位的Segfault,以64位工作
- 使用变量参数映射函数并通过字符串 c++ 调用
- 使用包含函数名称的字符串调用函数
- C++-将输入函数作为字符串调用
- 如果使用错误的格式字符串调用 printf,会发生什么情况
- c++从字符串调用方法