【问题标题】:c++ insert character in an array or stringc++ 在数组或字符串中插入字符
【发布时间】:2014-03-13 23:55:46
【问题描述】:

我有这部分代码:

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

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

【问题讨论】:

  • std::string 有一个插入方法en.cppreference.com/w/cpp/string/basic_string/insert
  • 我将如何根据给定的插入它?我真的不知道我将如何实现它。
  • 提示:搜索标记化和解析(递归下降解析器,解析器生成器)。
  • 不应该是 (((x+y)*z)-a)/b 还是最大应该是 ((((x+y)*z)-a)/b) , 为什么需要在 x 周围加上一对括号?
  • 可以是 ((((x+y)*z)-a)/b) 。我该怎么做?

标签: c++ arrays string character


【解决方案1】:

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

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

如果你遵循这个算法,中间输出会是这样的:

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

此数据进入单独的char 数组或std::string

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

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

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

【讨论】:

  • @jm- 创建两个索引(或指针,如果您愿意),一个指向目标缓冲区的开头,另一个指向源缓冲区的结尾。在一个循环中沿相反方向移动索引(指针),将字符从源复制到目标。
  • 我无法理解指针的事情。你能告诉我怎么做吗?
  • @jm- 这是一个link to an answer,它向您展示了如何将字符串反转到位。
  • 谢谢。不过有问题。使用您的算法将给出 ((((x =x + y)-z)*a)/b);
【解决方案2】:

有时我会忘乎所以。我不确定这段代码有什么帮助,但它会以你演示的方式包装。

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

【讨论】:

  • 我不能使用向量
  • 标准模板库,恰当地说,是标准的。我看不出你为什么不能。如果你不能 - 然后建立你自己的:cs.sfu.ca/CourseCentral/125/tjd/vector_example.html
  • @jm- 另外,如果这是一个家庭作业问题,请相应地标记您的 stackoverflow 问题;即添加homework 标签。
猜你喜欢
  • 1970-01-01
  • 2019-05-04
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
  • 2017-04-09
  • 1970-01-01
相关资源
最近更新 更多