0

我在一个格式为x operator y. 如何将此字符串分解为数字的两个双精度数和运算符的一个字符?

4

2 回答 2

3

这对你有用吗?

string line = "1 operator 2";
stringstream ss(line);
double n1, n2;
string op;
ss >> n1;
ss >> op;
ss >> n2;
于 2012-12-02T12:09:21.343 回答
1

您需要解析字符串或对其进行标记 - 将字符串拼接成单独的数据片段:

  1. 解析字符串
  2. 查找数据
  3. 提取数据
  4. 转换为 x & y 为双精度
  5. 根据操作员做...用x和y。(例如使用 switch 语句)

我提出的两种方法可以轻松解决您的问题。你可以简单地找到'+'字符,然后得到那个字符的左边是x,然后得到那个字符的右边是y。然后将这些提取的字符串转换为双精度。注意:这只有在字符串中只有一个“x operator y”时才可以接受。否则你可能会抢到比你想要的更多的东西。

然后还有另一种使用二叉树的方法。本质上你可以用二叉树制作一个计算器,虽然它很先进,但我现在不推荐给你。http://en.wikipedia.org/wiki/Binary_tree

使用第一个建议的解决方案的评论示例:

#include <iostream> //used for std::cout
#include <string>   //used for std::string
#include <sstream>  //used to convert a std::string to a double
#include <cctype>   //used for checking if a character is a digit(0-9.)

int main() 
{
    //declare the source string to parse
    std::string source = "2+6";

    //output variables
    //operator is a keyword, so just use op.
    char op=' ';     
    double x=0, y=0;

    //parse source. iterate through each character starting at 0 (first character)
    for(int i = 0; i < source.size(); i++) {

        //check to see if its a character - ie if its not a number or . its an operator!
        //(can be any *character* really - however must be only *1 character* long) and it must also not be a space.
        if(!isdigit(source[i] || ' ')) {

            //create the strings to put the copied data in
            std::string xs, ys; //x and y strings

            //get the left and right of the operator
            //you could use a for loop, your choice.

            //copy before the operator.
            xs = source.substr(0, i);

            //get the operator
            op = source[i]; // by using the [i] - will just get a character from a string at [..]

            //skip the operator by adding 1 - get the right hand side
            ys = source.substr(i+1, source.size()-1);

            //create the string stream used for converting the data to a double (its like std::cout and std::cin - uses the << and >>)
            //use the stringstream xxs(..) <- to initialise the stringstream with our string above. 
            std::stringstream xss(xs); //xss = xs string
            std::stringstream yss(ys); //'   ' '  '

            //now the string stream does the work for us. just feed the xss & yss string streams(which are our x & y strings) into the doubles using the >> operator, converting the data types.
            xss >> x; //string TO double
            yss >> y;

            //don't want to search for any more characters now. finish up.
            break;
        }
    }

    std::cout << "x  = " << x  << std::endl;
    std::cout << "op = " << op << std::endl;
    std::cout << "y  = " << y  << std::endl;

    std::system("pause");
    return 0;

}

如果您只想查看裸代码,则无需注释:

#include <iostream>
#include <string>
#include <sstream> 
#include <cctype>  

int main()
{

    std::string source = "2+6";
    char op=' ';     
    double x=0, y=0;

    for(int i = 0; i < source.size(); i++) {
        if(!isdigit(source[i]) || ' ') {

            std::string xs, ys;

            xs = source.substr(0, i);
            op = source[i];
            ys = source.substr(i+1, source.size()-1);

            std::stringstream xss(xs);
            std::stringstream yss(ys); 

            xss >> x;
            yss >> y;

            break;
        }
    }

    std::cout << "x  = " << x  << std::endl;
    std::cout << "op = " << op << std::endl;
    std::cout << "y  = " << y  << std::endl;

    std::system("pause");


    return 0;
}

This code works with no spaces, or spaces, and can be expanded upon- instead of finding a single character as the operator, find 1-3 characters. Hope I helped :)

于 2012-12-02T14:02:28.590 回答