如何读取带有数字间隔的文本文件? C ++

问题描述 投票:-1回答:1

我真的需要你的帮助来解决这个问题:

我有一个file2文本文件,其中包含如下所示的数字范围:

12345678[5-8][0-9]
3684567150
329465207[023456789]
132478026[13]
778941351[02-689]
84364575[89][0-9]
88229401XX
981024833X
8912380XXX

所以这个数字范围像这样分解:

12345678[5-8][0-9]:   1234567850-1234567889
3684567150:           3684567150
329465207[023456789]: 3294652070 and 3294652072-3294652079
132478026[13]:        1324780261 and 1324780263
778941351[02-689]:    7789413510, 7789413512-7789413516, 7789413518 and 7789413519
84364575[89][0-9]:    8436457580-8436457599
88229401XX:           8822940100-8822940199
981024833X:           9810248330-9810248339
8912380XXX:           8912380000-8912380999

X可以从0 to 9获取值。所有这些数字都是10位数。但是范围可能有时变化很大,范围可以写成:[02-9]写为:[023456789],反之亦然。

我只需要这部分关于如何将其作为数字范围读取或者可能定义如下情况:

 XXXXXXXXX[X-X]
 XXXXXXXX[X-X][X-X]
 XXXXXXXXX[XX-X]
 XXXXXXXXX[XXXXXXXX]

但是我不知道如何做到这一点,我希望你能帮助我。现在我只是将文件中的数据保存到字符串向量中:

ifstream file2("file2.txt");

    while (getline(file2,line) && line.size()>=0)
        vfile2.push_back(line);

请不要单独离开我不知道怎么开始,我为此做了更多的代码,但它与这部分无关,因为如果你需要证据的话,这不仅仅是整个程序应该做的事情告诉我

谢谢!

更新:

我有一个file2文本文件,其中包含如下所示的数字范围:

88229401XX
981024833X
8912380XXX

所以这个数字范围像这样分解:

88229401XX:           8822940100-8822940199
981024833X:           9810248330-9810248339
8912380XXX:           8912380000-8912380999

再次感谢您的帮助和时间,我正在努力解决这个问题,如果我弄明白的话(我可能对如何有所了解),它会立即发布。

c++ numbers intervals
1个回答
0
投票

这是一个解析问题。有一些工具可以帮助您解析,但必须学习它们。有时最好潜入并编写代码。如果你对它有条不紊的话,那就不那么难了。诀窍是使代码的结构与您正在解析的语法结构相匹配。

以下代码假定您要将整数存储在向量中。它只是在最后打印出该向量的内容。

以下代码根本不对输入进行错误检查。在一个真正的程序,这将是一个严重的问题。我会留下你添加错误检查。

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdint>

typedef uint64_t int_type;

void parse(const std::string& line, std::vector<int_type>& numbers)
{
    numbers = { 0 };
    size_t i = 0;
    while (i < line.size())
    {
        char ch = line[i++];
        if (ch == '[')
        {
            // start a group
            std::string group;
            while (line[i] != ']')
            {
                char lo = line[i++];
                if (line[i] == '-')
                {
                    ++i;
                    char hi = line[i++];
                    while (lo <= hi)
                    {
                        group += lo;
                        ++lo;
                    }
                }
                else
                {
                    group += lo;
                }
            }
            ++i;
            // add the new numbers implied by the group
            std::vector<int_type> new_numbers;
            for (auto num : numbers)
            {
                for (auto digit : group)
                    new_numbers.push_back(10 * num + (digit - '0'));
            }
            numbers.swap(new_numbers);
        }
        else
        {
            std::transform(numbers.begin(), numbers.end(), numbers.begin(),
                [=](auto n) { return 10 * n + (ch - '0');  });
        }
    }
}

int main()
{
    std::string data = "12345678[5-8][0-9]\n"
        "3684567150\n"
        "329465207[023456789]\n"
        "132478026[13]\n"
        "778941351[02-689]\n"
        "84364575[89][0-9]\n";
    std::istringstream input(data);
    std::string line;
    while (getline(input, line) && line.size() >= 0)
    {
        std::vector<int_type> numbers;
        parse(line, numbers);
        for (auto num : numbers)
        {
            std::cout << num << '\n';
        }
        std::cout << '\n';
    }
}

只是简单测试过,但似乎有效。

© www.soinside.com 2019 - 2024. All rights reserved.