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

时间:2014-03-13 23:55:47

标签: c++ arrays string character

我有这部分代码:

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

如何将这些括号插入原始输入?谢谢。顺便说一句,我真的需要将它存储在一个新的数组或一个字符串中。我只是不知道如何插入这些括号。

2 个答案:

答案 0 :(得分:2)

您可以从后面开始写入不同的数组,而不是将新项目插入到原始输入中。

  • 当您看到分号或运算符时,请在其后添加右括号
  • 每次插入右括号时,请将count变量加一个
  • 否则,将字符复制到输出
  • 到达=符号后,在其前插入count个括号
  • 通过反转字符串
  • 生成最终输出

如果您遵循此算法,中间输出将如下所示:

;)b/)a-)z*)y+)x(((((=x

此数据分为char个数组或std::string

当你反转它时,输出就变成你想要的了:

x=((((x)+y)*z)-a)/b);

如果您愿意,可以将反转数据写回原始缓冲区。

答案 1 :(得分:0)

有时我会被带走。我不确定这段代码是如何有用的,但它会以你演示的方式包装。

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