将单个字符串元素转换为c++中的十进制等价元素
converting individual string elements to their decimal equivalents in c++
我有一个字符串str ( "1 + 2 = 3" )
。我想获得字符串的各个数字的十进制值(而不是ASCII)。我试过atoi
和c_str()
。但它们都要求整个字符串只由数字组成。我正在用C++编写代码。
任何帮助都会很棒。
我的挑战是评估前缀表达式。我正在读取一个文件,其中每一行都包含一个前缀表达式。我用于标记和存储变量的代码片段如下所示。文件的每一行都包含由空格分隔的数字和运算符(+
、-
、*
)。
Ex-line = ( * + 2 3 4);
ifstream file;
string line;
file.open(argv[1]);
while(!file.eof())
{
getline(file,line);
if(line.length()==0)
continue;
else
{
vector<int> vec;
string delimiters = " ";
size_t current;
size_t next = -1;
do
{
current = next + 1;
next = line.find_first_of( delimiters, current );
if((line[next] <=57)&&(line[next] >=48))
vec.push_back(atoi((line.substr( current, next - current )).c_str()));
}while (next != string::npos);
cout << vec[0] << endl;
}
}
file.close();
在这种情况下,vec[0]
打印50
而不是2
。
您需要学习对字符串进行定界。您的分隔字符将是数学运算符(即:
C: 从分隔的源字符串创建字符串数组
http://www.gnu.org/software/libc/manual/html_node/Finding-Tokens-in-a-String.html
在第二个链接的情况下,您可以执行以下操作:
const char delimiters[] = "+-=";
有了这些知识,您可以创建一个字符串数组,并对每个字符串调用atoi()来获得等效的数字。然后,您可以使用每个分隔符的地址(数组索引)来确定存在哪个运算符。
对于像加法和减法这样的事情,这将是非常简单的。如果您想要运算和乘法的顺序、括号等,您的流程逻辑将更加复杂。
要获得更深入的示例,请查看最后一个链接。C中的一个简单的命令行计算器。这应该会让它非常清楚。
http://stevehanov.ca/blog/index.php?id=26
您不会落入if
,因为您的下一个位置将位于分隔符处。
string delimiters = " ";
...
next = line.find_first_of( delimiters, current );
if((line[next] <=57)&&(line[next] >=48))
...
由于您的delimiters
由" "
组成,那么line[next]
将是一个空格字符。
从问题的描述来看,您缺少了可以省去运算符的代码。没有代码可以尝试查找运算符。
您不必假定ASCII用于测试数字。例如,您可以使用is_digit()
,也可以与'9'
和'0'
进行比较。
打印矢量元素时,可能会不适当地访问矢量,因为数组中可能从未插入任何项。
不要使用fin.eof()
来控制循环。该函数只有在读取失败后才有用。
有很多方法可以从std::string
中获得int
,在这种情况下,我选择C++11标准中的std::stoi()
。
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
typedef std::vector<int> ints;
bool is_known_operator(std::string const& token)
{
static char const* tokens[] = {"*", "/", "+", "-"};
return std::find(std::begin(tokens), std::end(tokens), token) != std::end(tokens);
}
ints tokenise(std::string const& line)
{
ints vec;
std::string token;
std::istringstream iss(line);
while (iss >> token)
{
if (is_known_operator(token))
{
std::cout << "Handle operator [" << token << "]" << std::endl;
}
else
{
try
{
auto number = std::stoi(token);
vec.push_back(number);
}
catch (const std::invalid_argument&)
{
std::cerr << "Unexpected item in the bagging area ["
<< token << "]" << std::endl;
}
}
}
return vec;
}
int main(int, const char *argv[])
{
std::ifstream file(argv[1]);
std::string line;
ints vec;
while (std::getline(file, line))
{
vec = tokenise(line);
}
std::cout << "The following " << vec.size() << " numbers were read:n";
std::copy(vec.begin(), vec.end(), std::ostream_iterator<int>(std::cout, "n"));
}
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 使用strcpy将char数组的元素复制到另一个数组
- 使用不带参数的函数访问结构元素
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- C++如何通过用户输入删除列表元素
- lower_bound()返回最后一个元素
- 基于多个条件处理地图中的所有元素
- 调整大小后指向元素值的指针unordered_map有效?
- 使用std::transform将一个范围的元素添加到另一个范围中
- 使用函数"remove"删除重复元素
- 具有最大子序列大小的序列,每个元素都相同
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 如何将元素添加到数组的线程安全函数?
- 有没有办法将谓词中的元素偏移量传递给 std 算法?
- 我想访问std::unique_ptr中的一个特定元素
- 如何通过 getter 函数删除矢量的元素?
- visual在c++中将十进制数转换为二进制数
- 计算字符串/数组元素的十六进制,然后将其转换为十进制
- 将单个字符串元素转换为c++中的十进制等价元素