将单个字符串元素转换为c++中的十进制等价元素

converting individual string elements to their decimal equivalents in c++

本文关键字:元素 十进制 单个 字符串 转换 c++      更新时间:2023-10-16

我有一个字符串str ( "1 + 2 = 3" )。我想获得字符串的各个数字的十进制值(而不是ASCII)。我试过atoic_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"));
}