在数组或字符串中插入字符

c++ insert character in an array or string

本文关键字:插入 字符 字符串 数组      更新时间:2023-10-16

我有这部分代码:

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;
}