如何在c++中将计算分解为两个double和一个char
How to break calculation into two doubles and a char in c++
我在一个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个字符。希望我能帮上忙:)
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 两个字符串在 c++ 中不相等
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 两个文件使用彼此的功能-如何解决
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 将fold表达式与std::一起用于两个元组
- 如何在C++中比较两个char数组
- C++:快速/并行计算两个"std::vector<double>"向量之间的L1距离
- 如何在c++中将计算分解为两个double和一个char
- 为什么两个程序中的一个会导致错误"*** glibc detected *** double free or corruption"而另一个则不会导致错误?
- 将字符串解析为两个double
- 包含两个双精度的类是 std::complex 参数的有效替代品<double>吗?
- 用于两个double的重载操作符%