如何从字符串中提取浮点数
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
由于这个文件包含三种不同类型的数据,所以我很难只计算那些代表价格的浮点数。我需要以某种方式将它们输入到一个变量中,然后进行计算。
由于该文件包含三种不同类型的
你应该定义三种不同的类型。为每种类型定义输入运算符。然后为数据行定义一个类型,您就可以读取这三种不同的类型。
DataLine
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;
}
};
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;
}
};
找到合适的分隔符来定位您感兴趣的子字符串,然后使用字符串流将其转换为浮点/双精度。wstring操作有等效的std对象。
相关文章:
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- C++浮点数据类型和字符串数据类型无法子到模板函数中
- 使用提升将数据从 PyObject 复制到浮点数 *
- 使用浮点数和双精度数的非常小数字的数学
- 使用英特尔内联函数将打包的 8 位整数乘以浮点数向量
- 如何在 c++ 中将小数点后两位数的浮点数分配给另一个浮点数
- 返回浮点数的小数位数
- 找到一种使用 C++ 中的条件提取浮点数的方法
- 如何使用 sscanf 提取浮点数?
- 如何在 c++ 中使用 FFT 从 32 位浮点数组中提取频率
- Z3 SMT求解器 - 如何在FPA中提取浮点数的值
- 从C 中的字符串中提取浮点数的最简单方法
- 如何从字符串中提取浮点数
- 从 CString 中提取浮点数
- 如何从C++文件中的特定行中提取数字(浮点数)
- std::istream 提取运算符(双精度或浮点数)在 VS 2012 中非常慢
- 从浮点数中提取数字
- 精确地提取浮点数的小数部分
- 对某些标准流类插入和提取浮点数是否保留其值