Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual c++ C++;如何使用TinyExpr解析字符串中的数学问题?_Visual C++ - Fatal编程技术网

Visual c++ C++;如何使用TinyExpr解析字符串中的数学问题?

Visual c++ C++;如何使用TinyExpr解析字符串中的数学问题?,visual-c++,Visual C++,首先,我需要检测来自播放器的消息是否包含数学问题 好像我收到了他的两条信息 char* message1 = "Please solve this math problem"; char* message2 = "How much is 1 + 2 ?"; 这些信息不是静态的,玩家可以说出不同的信息 我需要处理message1和message2 我需要检测数学问题,并将其像message2一样剪切,我需要它为“1+2” 我认为它失败了,结果总是0,因为m

首先,我需要检测来自播放器的消息是否包含数学问题 好像我收到了他的两条信息

char* message1 = "Please solve this math problem";
char* message2 = "How much is 1 + 2 ?";
这些信息不是静态的,玩家可以说出不同的信息

我需要处理message1和message2 我需要检测数学问题,并将其像message2一样剪切,我需要它为“1+2”


我认为它失败了,结果总是0,因为msg是“Player:1+1”,我需要从中删除数学问题,我不知道如何做…a

找到该字符串中第一个数字的位置,并从该点获取子字符串:

std::size_t found = msg.find_first_of("0123456789");
if(found != std::string::npos)
{
  std::string sub = msg.substr(found);
  double answer = te_interp(sub, 0);
}
如果同样失败,尝试修剪无效的尾部

您可能希望扩展上面的
find\u first\u
,以包括open paren
、一元减号
-
)或一些支持的函数名(我不熟悉TinyExpr)

std::string msg = Message;

if (func.contains_math_operators(msg.c_str()) && func.contains_number(msg.c_str()))
{
    double answer = te_interp(Message, 0);
}
std::size_t found = msg.find_first_of("0123456789");
if(found != std::string::npos)
{
  std::string sub = msg.substr(found);
  double answer = te_interp(sub, 0);
}