我在一个x operator y
格式的字符串中进行了计算。如何将这个字符串分解为数字的两个双字符和运算符的一个字符?
这对你有用吗?
string line = "1 operator 2";
stringstream ss(line);
double n1, n2;
string op;
ss >> n1;
ss >> op;
ss >> n2;
您需要解析字符串,或者将其标记化-将字符串拼接成单独的数据片段:
- 分析字符串
- 查找数据
- 提取数据
- 转换为x&y到双打
- 根据运营商的说法。。。带有x和y(例如使用switch语句)
我提出的两种方法可以很容易地解决您的问题。你可以简单地找到"+"字符,然后得到该字符的左边是x,再到该字符的右边是y。然后将提取的字符串转换为双精度字符串。注意:只有当字符串中只有一个"x运算符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;
}
这段代码不使用空格,也不使用空格。可以对其进行扩展,而不是查找单个字符作为运算符,而是查找1-3个字符。希望我能帮上忙:)