の形式の文字列で計算していますx operator y
。この文字列を数値用の 2 つの double と演算子用の char に分割するにはどうすればよいですか?
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
文字列を解析するか、トークン化する必要があります-文字列を個々のデータにスプライシングします。
- 文字列を解析する
- データを探す
- データの抽出
- x & y を double に変換
- 演算子によると、x と y で ... を実行します。(たとえば、switch ステートメントを使用)
私が提案する 2 つの方法で、問題を簡単に解決できます。単純に「+」文字を見つけて、その文字の LEFT を x にし、その文字の RIGHT を y にすることができます。次に、抽出された文字列を double に変換します。注: これは、文字列に 'x operator y' が 1 つしかない場合にのみ許容されます。そうしないと、必要以上に取得する可能性があります。
そして、二分木を使った別の方法もあります。基本的に、バイナリ ツリーを使用して電卓を作成できますが、これはかなり高度なものであり、現時点ではお勧めしません。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 文字を検索する代わりに、1 ~ 3 文字を検索します。私が助けてくれることを願っています:)
于 2012-12-02T14:02:28.590 に答える