我想将一些旧的手写解析代码转换为Boost Spirit,并在此过程中学习(更多)精神.旧代码使用流和模板来解析某些数据类型和某些容器的定义.
一些典型的格式:
VECTOR[number_of_items,(item_1, item_2 .... item_n)]
PAIR(p1, p2)
RECT[(left,top)-(right,bottom)]
Point( x, y )
Size( x, y )
Run Code Online (Sandbox Code Playgroud)
解析函数是模板,其中项目的类型作为模板参数,并使用流作为输入,例如
template<class T> std::istream& operator>>(std::Stream& in, std::vector<T>& v);
template<class T1, class T2> std::istream& operator>>(std::istream& in, std::pair<T1, T2>& p);
template<class T1, class T2> std::istream& operator>>(std::istream& in, RectType<T>& r);
etc.
Run Code Online (Sandbox Code Playgroud)
向量的解析器(流提取器)调用解析器以获取模板类型.
使用这些可以解析整数矩形,双矩形以及字符串和整数对的向量的定义.
是否有可能使用Spirit编写模板解析器来调用模板类型的子解析器?
我试图在Qt中使用脚本,这里是一个非常简单的代码.
QCoreApplication a(argc, argv);
double x;
cout<<"Please enter a number: ";
cin>>x;
QFile file("cube.js");
if(!file.open(QIODevice::ReadOnly))
abort();
QTextStream in(&file);
in.setCodec("UTF-8");
QString script=in.readAll();
file.close();
QScriptEngine interpreter;
QScriptValue operand(&interpreter,x);
interpreter.globalObject().setProperty("x",operand);
QScriptValue result=interpreter.evaluate(script);
cout<<"The result is "<<result.data().toInt32()<<endl;
return a.exec();
Run Code Online (Sandbox Code Playgroud)
cube.js的内容只有一行:
return x*x*x;
Run Code Online (Sandbox Code Playgroud)
我运行这个程序,但它总是返回零.有人能告诉我它有什么问题吗?文件内容正确读取.
最好的祝福,
有没有办法在Qt中使用qreal获取小数值?
像这样:
qreal decimal;
average = 3/2;
Run Code Online (Sandbox Code Playgroud)
输出应该肯定是1.5,但我得到1结果.
谁能帮我??