簡體   English   中英

如何從字符串中提取浮點數?

[英]How to extract floating point numbers from a string?

我需要閱讀一行包含字母、整數和浮點數的文本,然后計算這些浮點數。 到目前為止我所做的如下

           while (getline(readSearch, line))
    {
        while (line.find(letters[0])!=string::npos ||   line.find(letters[1])!=string::npos || line.find(letters[2])!=string::npos || line.find(letters[3])!=string::npos || line.find(letters[4])!=string::npos)
        {
            cout << line << "\n";
            break;
        }

這是我正在閱讀的文件

   Chips 01c $0.50
   Juice  02j  $1.5
   Chocolate 03c $1.00
   Pen 04p 0.20
   Backpack 05b $30.00
   Bag 06b $35.25
   Ball 07b $10.50
   Toy 08t $15.22
   Wi-Fi Router 09wr $40.00
   Generic HDMI cable 010hc $4.00

由於這個文件包含三種不同類型的數據,我真的很難弄清楚如何只計算那些代表價格的浮點數。 我需要以某種方式將它們輸入變量而不是執行計算。

找到合適的分隔符來定位您感興趣的substr ,然后使用stringstream轉換為 float/double。 wstring 操作有等效的 std 對象。

由於此文件包含三種不同類型

他們應該定義三種不同的類型。 為每種類型定義輸入運算符。 然后為數據行定義一個類型,您可以讀取三種不同的類型。

數據線

struct DataLine
{
    std::string name;   // Wi-Fi Router
    DataBlob    blob;   // 010hc 
    Price       price;  // $40.00

    void swap(DataLine& other) noexcept
    {
        using std::swap;
        swap(name,   other.name);
        swap(blob,   other.blob);
        swap(price,  other.price);
    }

    friend std::istream& operator>>(std::istream& s, DataLine& data)
    {
        std::string line;
        if (std::getline(s, line))
        {
            // The name takes up all but the last two space separated words.
            // So find that point first.
            std::size_t  first  = line.find_last_of(" \t");
            std::size_t  second = (first == 0)
                                      ? std::string::npos 
                                      : line.find_last_of(" \t", first-1);
            std::stringstream lineStream(line);
            DataLine  tmp;
            if (lineStream >> ItemNameReader(tmp.name, second - 1) >> tmp.blob >> tmp.price) {
               // All three items were read correctly
               // So update the object with the new state.
               data.swap(tmp);
            }
            else {
               // One of the read operations failed.
               // So set the state of the stream to bad
               // The user can then handle this situation.
               s. setstate(std::ios::badbit);
            }
        }
        return s;
    }
};

項目名稱閱讀器

struct ItemNameReader
{
    std::string&  name;
    std::size_t   size;
    ItemNameReader(std::string& name, std::size_t size)
        : name(name)
        , size(size)
    {}
    friend std::istream& operator>>(std::istream& s, ItemNameReader const& data)
    {
        data.name.resize(data.size);
        s.read(data.name.data(), data.size);
        return s;
    }   
};

數據塊

class DataBlob
{
    std::string   blob;
    friend std::istream& operator>>(std::istream& s, DataBlob& data)
    {
        return s >> data.blob;
    }
};

價錢

class Price
{
    char  units;
    float value;  // Though you may want to consider monetary
                  // units as integer values to avoid any rounding
                  // issues associated with floating point. 
    friend std::istream& operator>>(std::istream& s, Price& data)
    {
        return s >> data.units >> data.value;
    }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM