【问题标题】:Trying to find a number in a string, change it, put it back and find the next number试图在一个字符串中找到一个数字,改变它,把它放回去并找到下一个数字
【发布时间】:2017-10-12 01:45:45
【问题描述】:

如果这个问题已经得到回答,我很抱歉。如果是,请指导我找到解决方案。

我目前正在尝试浏览一个文本文件,遍历它以找到一个数值,将值通过管道传输到子/父进程中,然后我将更新后的值返回到文本中。尽管很简单,但我正在尝试查找数字并将它们放入数组/字符串中,这样我就可以将其输入管道,然后将其输入文本。我已经为此工作了5天,所以我终于向你们寻求帮助。再次道歉,如果这已经存在。

我目前正在浏览文本并检查每个字符是否为整数。我可以把它变成一个字符串,但它只是一个大长字符串。我不确定如何实现它。

这是我目前所拥有的。

while(!readText.eof())
    {
      while(getline(readText, item))
      {
      readText >> item;
      for(int i=0; i < item.length(); ++i){
        if(isdigit(item[i])) //checking if it is a digit
          newitem += item[i]; //puts into string
        if(!isdigit(item[i])) // if it's not a digit feed it out (but maybe I 
                              //should put into new variable?
           cout << item[i];
        else if(isspace(item[i]))//keeps the spaces as cout, but again, maybe 
                                //put it into a variable?
            cout << " ";
        }
      }
 }

我的挑战是试图弄清楚如何分离这些值并将它们放回原处。我会随着索引增长,但数字会增长,所以位置不会那么准确。


更新的上下文

感谢大家的帮助。一个示例文件将是

In the b6765lue sky there are 59 clouds. The gra7ss is green 766 and it 837 has 7 a cow in i87t.

我需要通过流程/功能更改数字。所以就像你提到的数字的大小自然会变得更大。测试文件可以是 100 行。

In the b6789lue sky there are 83 clouds. The gra31ss is green 790 and it 861 has 31 a cow in i111t.

值已更改为 5 个进程,增量值为 5。

【问题讨论】:

  • while(!readText.eof()) -- Please read why this is considered wrong
  • 您能否包含一个示例文件的小样本,以及您的预期输出是什么?
  • 鉴于您发布的输入行,我仍然不明白最终输出是什么,或者应该从中得出什么数字。你有数字和字母混合,好的,那你应该怎么做?你有没有字母的数字,你有什么用?
  • 对不起!我会更新的。
  • 非常感谢@PaulMcKenzie 的提示。我真的很感激!

标签: c++


【解决方案1】:

为什么不保存一个值和一个新值的起始位置,以便在到达该位置时打印?所以,在我看来是这样的:

struct replace_integer
{
  int old_int_length;
  int new_int_value;
  int int_to_replace_text_position;
};

然后,当您打印下来时,只需跳过旧的 int 并打印新的。它会起作用吗?

要记住起始位置,您应该修改循环:

int item_length = item.length();
for(int i=0; i < item_length ; ++i)
{
    if(isdigit(item[i]))
    {
      int_to_replace_text_position = i;
      while(isdigit(item[i++]) && i < item_length )
         newitem += item[old_int_length_counter]; //puts into string
      old_int_length = i - int_to_replace_text_position; //check it, i may have a 1 position mistake, too sleepy, so I can lose "-1"
    }

    if(!isdigit(item[i])) // if it's not a digit feed it out (but maybe I 
                          //should put into new variable?
       cout << item[i];
    else if(isspace(item[i]))//keeps the spaces as cout, but again, maybe 
                            //put it into a variable?
        cout << " ";
}

【讨论】:

  • 非常感谢您的建议!我还有一个问题,如果我有多个值,我会创建一个 for 循环来输入它们并进行迭代吗?
  • 刚看了你的昵称,真的很搞笑很酷)对不起。 “喂他们”是什么意思?你能不使用数组而是使用smth的向量,并使用迭代器进行迭代吗?
  • 谢谢!我正在使用这样的管道 (stackoverflow.com/questions/21664844/…),因此该值正在通过一个子进程并正在通过 n 个进程进行更改。
  • 愚蠢的问题,我将如何使用矢量?我对我的 c++ 仍然很生疏,因为我是一个红宝石女孩,后来改成了 c++ 和 java。
  • 好吧,你读过这个:en.cppreference.com/w/cpp/container/vector 吗?只需从我上面写的结构中创建一个类。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 1970-01-01
  • 2018-08-05
  • 2012-02-02
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
相关资源
最近更新 更多