在数组或字符串中插入字符
c++ insert character in an array or string
我有这部分代码:
char statement[255];
string result = ""; //or you can use result[299] too
cin.getline(statement,255);
/*
I don't know the code to be inserted here
*/
cout<<statement<<endl;
/*or cout<<result<<endl;*/
现在,我要做的是:
如果用户输入x = x + y * z - a / b ;
,我希望输出为x = ((((( x ) + y ) * z ) - a ) / b) ;
如何将这些括号插入到原始输入中?谢谢。顺便说一下,我真的需要将它存储在一个新的数组或字符串中。我只是不知道怎么插入括号
与其在原始输入中插入新项,不如从后面开始写入另一个数组。
- 当您看到分号或操作符时,在其后面添加右括号
- 每次插入右括号时,将
count
变量增加1 - 否则,将字符复制到输出
- 到达
=
符号后,在其前面插入count
左括号 - 通过反转字符串 产生最终输出
如果您遵循此算法,中间输出将如下所示:
;)b/)a-)z*)y+)x(((((=x
这些数据进入一个单独的char
数组或std::string
。
当你反转它时,输出变成你想要的:
x=((((x)+y)*z)-a)/b);
如果您愿意,可以将反转的数据写回原始缓冲区。
有时候我会忘乎所以。我不确定这段代码是否有帮助,但它将按照您演示的方式进行包装。
string PopNextField(string& input)
{
// skip whitesapce
while (input.length() > 0)
{
if (!::isspace(input.front()))
break;
input = input.substr(1);
}
string result = "";
// read to end
while (input.length() > 0)
{
if (::isspace(input.front()))
break;
// type switch
if (
result.length() != 0 &&
(::isalnum(input.front()) != ::isalnum(result.front()))
)
break;
result += input.front();
input = input.substr(1);
}
return result;
}
bool FieldIsOperator(string field, const vector<string>& ops)
{
for (auto it = ops.begin(); it != ops.end(); it++)
if (*it == field)
return true;
return false;
}
bool FieldIsEnd(string field)
{
return field == ";";
}
vector<string> ParseFields(string& input)
{
vector<string> fields;
while (input.length() > 0)
{
string field = PopNextField(input);
if (field.length() > 0)
fields.push_back(field);
}
return fields;
}
string AddParens(string input, const vector<string>& opprec)
{
vector<string> fields = ParseFields(input);
string result = "";
// if field size is one, don't wrap
if (fields.size() == 1)
{
return fields.front();
}
for (auto it = fields.begin(); it != fields.end(); it++)
{
string next = *it;
if (FieldIsOperator(next, opprec))
{
result += " " + next;
}
else if (FieldIsEnd(next))
{
result += next;
}
else
{
result = "(" + result + next + ")";
}
}
return result;
}
int main()
{
vector<string> opprec;
opprec.push_back("(");
opprec.push_back(")");
opprec.push_back("*");
opprec.push_back("/");
opprec.push_back("+");
opprec.push_back("-");
string input = "x = x + y * z - a / b ;";
string result = "";
string remainingInput = input;
// split assignments
while (remainingInput.length() > 0)
{
auto nextAssignmentIndex = remainingInput.find("=");
string nextInput = remainingInput.substr(0, nextAssignmentIndex);
result += AddParens(nextInput, opprec);
if (nextAssignmentIndex != string::npos)
{
result += "=";
remainingInput = remainingInput.substr(nextAssignmentIndex + 1);
}
else
{
break;
}
}
cout << "Input: " << input << endl;
cout << "Result: " << result << endl;
cin.get();
return 0;
}
相关文章:
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 在将字符插入 2d 字符串时如何不显示空字符?
- 如何将整数向量值插入字符向量
- 如何从代码本身向 wxwidgets 中的文本控件插入字符?
- C++ 在字符的开头插入字符
- C ++程序在字符数组中插入单词
- C++如果旋转是特定字符,请插入符号
- 如何在文本文件中间插入字符
- 通过字符串::插入插入字符时出错
- 如何在矢量"[i]"中插入*字符<string>?[C++]
- 在字符串之间插入字符
- 在字符串的每个元素(不使用弦乐)的每个元素之后插入字符
- 在 sqlite 中插入字符* 作为向量<byte>
- 如何在集合中插入字符
- 插入字符在C 中介绍一个字符串
- 检测编辑控件焦点并插入字符
- 文件处理,在复制内容时插入ÿ字符
- 在字符数组中的特定位置插入字符
- 在c++中的字符串中插入字符
- 在数组或字符串中插入字符