我在做一项任务。我已经对作业进行了编码,除了输出缺少一个数字外,一切都按其应有的方式工作。我还附上了样本输出。在将数学表达式输入字符串(例如,35 * 4 - 6 / (9 + 3) )后,将删除一个数字。在正常情况下,3将失踪。按相反的顺序,9将丢失。我不明白为什么会发生这种事。
任何帮助,或我希望得到一些指导,将不胜感激。我是不是不正确地使用push_back?守则如下:
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> split(string);
vector<string> splitback(string);
int main()
{
vector<string> vectorExpression;
string expression;
cout << "Enter an expression :";
getline(cin, expression);
vectorExpression = split(expression);
for (int i = 0; i < vectorExpression.size(); i++)
{
cout << vectorExpression[i] << endl;
}
cout << endl;
vectorExpression = splitback(expression);
for (int i = 0; i < vectorExpression.size(); i++)
{
cout << vectorExpression[i] << endl;
}
system("pause");
return 0;
}
vector<string> split(string expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else if (expression[i] != ' ')
{
x = "";
x = x + expression[i];
splitExpression.push_back(x);
}
else
{
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
vector<string> splitback(string expression)
{
vector<string> splitBackExpression;
string number = "",
x = "";
for (int i = expression.size() - 1; i >= 0; i--)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
number = expression[i] + number;
}
else if (expression[i] != ' ')
{
x = "";
x = x + expression[i];
splitBackExpression.push_back(x);
}
if (number.size() > 0)
{
splitBackExpression.push_back(number);
number = "";
}
}
return splitBackExpression;
}

发布于 2018-11-24 06:50:45
您的split函数是错误的,您忘记添加数字,正确的一个将是
vector<string> split(string expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else
{
// don't forget digit if there was one
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
// also add what comes next if not a whitespace
if (expression[i] != ' ')
{
x = expression[i];
splitExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}发布于 2018-12-02 13:20:49
这是我自己问题的最后答案,或者至少是我从这里提出的解决方案。
#include<iostream>
#include<string>
#include<vector>
using namespace std;
vector<string> split(const string&);
vector<string> splitback(const string&);
int main()
{
vector<string> forwardExpression,
reverseExpression;
string expression;
cout << "Enter an expression :";
getline(cin, expression);
//////////////////////////////////////////////////
// FORWARD DISPLAY OF EXPRESSION //
//////////////////////////////////////////////////
forwardExpression = split(expression);
cout << "'" << expression << "'" << " split into individual entities forwards
yields:" << endl;
cout << endl;
for (int i = 0; i < forwardExpression.size(); i++)
{
cout << forwardExpression[i] << endl;
}
cout << endl;
//////////////////////////////////////////////////
// BACKWARD DISPLAY OF EXPRESSION //
//////////////////////////////////////////////////
reverseExpression = splitback(expression);
cout << "'" << expression << "'" << " split into individual entities
backwards yields:" << endl;
cout << endl;
for (int i = 0; i < reverseExpression.size(); i++)
{
cout << reverseExpression[i] << endl;
}
cout << endl;
system("pause");
return 0;
}
//////////////////////////////////////////////////
// FORWARD FUNCTION OF EXPRESSION //
//////////////////////////////////////////////////
vector<string> split(const string &expression)
{
vector<string> splitExpression;
string digit = "",
x = "";
for (int i = 0; i < expression.size(); i++)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = digit + expression[i];
}
else
{
if (digit.size() > 0)
{
splitExpression.push_back(digit);
digit = "";
}
if (expression[i] != ' ')
{
x = expression[i];
splitExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitExpression.push_back(digit);
}
return splitExpression;
}
//////////////////////////////////////////////////
// BACKWARD FUNCTION OF EXPRESSION //
//////////////////////////////////////////////////
vector<string> splitback(const string &expression)
{
vector<string> splitBackExpression;
string digit = "",
x = "";
for (int i = expression.size() - 1; i >= 0; i--)
{
if (expression[i] >= '0' && expression[i] <= '9')
{
digit = expression[i] + digit;
}
else
{
if (digit.size() > 0)
{
splitBackExpression.push_back(digit);
digit = "";
}
if (expression[i] != ' ')
{
x = expression[i];
splitBackExpression.push_back(x);
}
}
}
if (digit.size() > 0)
{
splitBackExpression.push_back(digit);
}
return splitBackExpression;
}https://stackoverflow.com/questions/53453912
复制相似问题