If语句不识别加号
If Statement Not Recognizing Plus Sign
我试图创建一个程序来计算一个多项式表达式的简化版本。我想分开所有的变量,和常数(加号),但我的程序似乎不能识别一些加号在我的字符串:
string al("2x+2+6y+8^7");
vector<string> variableStorage;
for (auto &c : al)
{
static int count = 0;
static int lastcount = 0;
if(c == '+' || count == al.length()-1)
{
static int spot(0);
variableStorage.push_back(al.substr(lastcount, count));
lastcount = count+1;
++spot;
}
++count;
}
for(auto c : variableStorage)
cout << c << endl;
当我运行这个程序时,得到以下输出:
2x
2+6y
6y+8^7
8^7
但是我想要的输出是:
2x
2
6y
8^7
我试着检查我的数学是否有错误,但就我所见,它似乎很好。
在+
s处拆分字符串(tokenize)
#include <string>
#include <vector>
using namespace std;
// This code from another SO question about splitting strings in C++
// http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c
template < class ContainerT >
void tokenize(const std::string& str, ContainerT& tokens,
const std::string& delimiters = " ", bool trimEmpty = false)
{
std::string::size_type pos, lastPos = 0;
while(true)
{
pos = str.find_first_of(delimiters, lastPos);
if(pos == std::string::npos)
{
pos = str.length();
if(pos != lastPos || !trimEmpty)
tokens.push_back(ContainerT::value_type(str.data()+lastPos,
(ContainerT::value_type::size_type)pos-lastPos ));
break;
}
else
{
if(pos != lastPos || !trimEmpty)
tokens.push_back(ContainerT::value_type(str.data()+lastPos,
(ContainerT::value_type::size_type)pos-lastPos ));
}
lastPos = pos + 1;
}
};
int main( void )
{
string al("2x+2+6y+8^7");
vector<string> variableStorage;
tokenize( al, viariableStorage );
for(auto c : variableStorage)
cout << c << endl;
//Your items are in variableStorage at this point
return( 0 );
}
上面的代码没有测试,很晚了,我觉得很懒。
获取起始位置和长度。所以你应该调用al.substr(lastcount, count-lastcount)
相关文章:
- 提升 ASIO 无法识别计时器对象
- 从udp接收帧对于人脸识别来说太慢
- 我的简单if-else语句是如何无法访问的代码
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 模板类无法识别友元运算符
- 线程,如果else语句,都是错误的上下文切换后,会发生什么
- 为什么是0;C++中的有效语句
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- if 语句错误地识别括号中的空格
- 在if语句中未识别滚动骰子号
- C :评估if语句时未识别炭值
- 我可以't识别if语句中的endl字符
- 试图获得一个C++switch语句来识别空格、制表符等
- catch语句如何在没有反射的情况下识别异常类型
- 在if语句中识别为9的8个字符
- 如果语句不工作,输入无法识别(c++)
- 复制语句如何识别其构造函数
- If语句不识别加号