如何从字符串中提取浮点数?
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 [=11=].50
Juice 02j .5
Chocolate 03c .00
Pen 04p 0.20
Backpack 05b .00
Bag 06b .25
Ball 07b .50
Toy 08t .22
Wi-Fi Router 09wr .00
Generic HDMI cable 010hc .00
由于此文件包含三种不同类型的数据,我真的很难弄清楚如何只计算那些代表价格的浮点数。
我需要以某种方式将它们输入到变量中而不是执行计算。
find the appropriate delimiter to locate the substr you're interested in, then use a stringstream 转换为 float/double。 wstring 操作有等效的 std 对象。
Since this file contains three different types
你应该定义三种不同的类型。为每种类型定义输入运算符。然后为这行数据定义一个类型,你就可以读入三种不同的类型。
数据线
struct DataLine
{
std::string name; // Wi-Fi Router
DataBlob blob; // 010hc
Price price; // .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;
}
};
ItemNameReader
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;
}
};
DataBlob
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;
}
};
我需要读取一行包含字母、整数和浮点数的文本,然后计算这些浮点数。到目前为止我所做的低于
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 [=11=].50
Juice 02j .5
Chocolate 03c .00
Pen 04p 0.20
Backpack 05b .00
Bag 06b .25
Ball 07b .50
Toy 08t .22
Wi-Fi Router 09wr .00
Generic HDMI cable 010hc .00
由于此文件包含三种不同类型的数据,我真的很难弄清楚如何只计算那些代表价格的浮点数。 我需要以某种方式将它们输入到变量中而不是执行计算。
find the appropriate delimiter to locate the substr you're interested in, then use a stringstream 转换为 float/double。 wstring 操作有等效的 std 对象。
Since this file contains three different types
你应该定义三种不同的类型。为每种类型定义输入运算符。然后为这行数据定义一个类型,你就可以读入三种不同的类型。
数据线
struct DataLine
{
std::string name; // Wi-Fi Router
DataBlob blob; // 010hc
Price price; // .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;
}
};
ItemNameReader
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;
}
};
DataBlob
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;
}
};