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

C++ c++;在数组或字符串中插入字符,c++,arrays,string,character,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;*/ char语句[255]; 字符

我有这部分代码:

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;*/
char语句[255];
字符串结果=”//或者您也可以使用result[299]
cin.getline(声明,255);
/*
我不知道这里要插入的代码
*/

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

  • 当您看到分号或运算符时,请在其后面添加右括号
  • 每次插入右括号时,将
    count
    变量递增一
  • 否则,将字符复制到输出中
  • 到达
    =
    符号后,在其前面插入
    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;
}
string PopNextField(字符串和输入)
{
//跳过白板
while(input.length()>0)
{
if(!::isspace(input.front()))
打破
input=input.substr(1);
}
字符串结果=”;
//通读
while(input.length()>0)
{
if(::isspace(input.front()))
打破
//类型开关
如果(
result.length()!=0&&
(::isalnum(input.front())!=::isalnum(result.front()))
)
打破
结果+=input.front();
input=input.substr(1);
}
返回结果;
}
布尔字段等参运算符(字符串字段、常量向量和运算)
{
for(auto it=ops.begin();it!=ops.end();it++)
如果(*it==字段)
返回true;
返回false;
}
布尔字段结束(字符串字段)
{
返回字段==“;”;
}
向量字段(字符串和输入)
{
向量场;
while(input.length()>0)
{
字符串字段=PopNextField(输入);
如果(field.length()>0)
字段。推回(字段);
}
返回字段;
}
字符串添加参数(字符串输入、常量向量和opprec)
{
向量字段=解析字段(输入);
字符串结果=”;
//如果字段大小为1,则不要换行
如果(fields.size()==1)
{
返回字段。front();
}
for(自动it=fields.begin();it!=fields.end();it++)
{
字符串next=*it;
if(现场等参器(下一个,opprec))
{
结果+=“”+下一步;
}
else if(字段结束(下一个))
{
结果+=下一个;
}
其他的
{
结果=“(“+result+next+”);
}
}
返回结果;
}
int main()
{
向量opprec;
反向推回(“(”);
逆推后(“)”;
反向推回(“*”);
反向推回(“/”);
反向推回(“+”);
反向推回(“-”);
字符串输入=“x=x+y*z-a/b;”;
字符串结果=”;
字符串remainingInput=输入;
//拆分作业
while(remainingInput.length()>0)
{
auto-nextAssignmentIndex=remainingInput.find(“=”);
字符串nextInput=remainingInput.substr(0,nextAssignmentIndex);
结果+=添加参数(下一个输入,opprec);
if(nextAssignmentIndex!=字符串::npos)
{
结果+=“=”;
remainingInput=remainingInput.substr(nextAssignmentIndex+1);
}
其他的
{
打破
}
}

cout
std::string
有一个insert方法我如何根据给定的插入它?我真的不知道如何实现它。提示:搜索标记化和解析(递归下降解析器,解析器生成器)。它应该是(((x+y)*z)-a)/b还是最大值(((x+y)*z)-a)/b,为什么需要在x的周围加一对括号?可以是(((x+y)*z)-a)/b)。我该怎么做?我不能使用向量,尽管标准模板库很贴切地说是标准的。我看不出你为什么不能使用向量。如果你必须使用向量,那么就创建你自己的:@jm-另外,如果这是一个家庭作业问题,请相应地标记你的堆栈溢出问题;也就是说,添加
家庭作业
标记。@jm-创建两个索引(或指针,如果您愿意),一个指向目标缓冲区的开头,另一个指向源缓冲区的结尾。移动索引(指针)在一个相反方向的单循环中,将字符从源复制到目标。我无法理解指针的内容。你能告诉我怎么做吗?@jm-这是一个演示如何反转字符串的方法。谢谢。但是有一个问题。使用你的算法会给出(((x=x+y)-z)*a)/b);