从外部输入文件将字符串数学公式作为双精度读入 cpp
Read string math-formula into cpp as double from external input-file
我对 C++ 编码比较陌生,并尝试编写一个程序来数值求解微分方程。我使用 codeBlocks 作为编译器并在 windows 下工作。数值求解器已经运行良好。
我的程序包含一些非常长的公式,这些公式由 mathematica 创建并转换为 cpp 语言。然后将公式存储在 .txt-file.I 中已经可以将公式作为字符串读取,但不能用它来计算东西,因为程序必须将公式解释为双精度型数学而不是字符串.这里的问题是,我的公式不仅包含数字,还包含作为变量的字母(它们的值在程序中设置)和其他数学符号。这就是为什么我认为我不能只使用 "atof" (http://www.cplusplus.com/reference/cstdlib/atof/?kw=atof) 或其他转换函数。 (如果我在这一点上错了,我很乐意学习如何使用该函数来解决这个问题!)
这是我的小程序中的一些示例代码:
//Program to solve ODEs
#include <iostream>
#include <math.h>
#include <cmath>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <time.h> //to measure the time
#include <stdio.h>
#include <conio.h>
using namespace std;
int main(void)
{
double k1=0;
ifstream file("Formelvu1.txt");//file with the fromula
string line;
stringstream longform;
while(getline(file, line)){ //read the formula and store them
longform << line; //store the string in "longform"
cout << longform;
}
return 0;
for(double t=0; t<10; t++){
k1 = (longform) * t; //simple operation with the formula
}
return 0;
}
此代码无效,因为长格式不是双...
longform 是一个字符串,类似于:ab+pow(t,3)-sin(tb)/x.
我已经找到了几个与此主题相关的问题,但其中 none 对我来说很容易理解或我想做的正确事情:
How can I convert string to double in C++?
据我了解,这个人正在尝试最接近我想做的事情:
Evaluate math formula from String using ScriptEngine
但是我没有完全理解代码。
如果它对我的问题有用:这部分是做什么的?
try{
return (Double)engine.eval(tmp);
}
catch(Exception fexp)
{
}
我还听说过可以解释 xpressions 行 muparser 的解析器:
http://muparser.beltoforion.de/mup_eval.html
但我不知道这是否会超出我的需要...
感谢大家 answer/response 并帮助解决这个问题。
谢谢!
你要仔细研究基本C/C++语言和标准库中包含什么,不包含什么。
但是,如果您从 Mathematica 获得了 C++ 兼容表达式,那么您可以让 C++ 编译器完成它的工作,如
double myfunc(double a, double b, double c, double t, double x) {
return
#include "Formelvu1.txt"
;
}
我对 C++ 编码比较陌生,并尝试编写一个程序来数值求解微分方程。我使用 codeBlocks 作为编译器并在 windows 下工作。数值求解器已经运行良好。 我的程序包含一些非常长的公式,这些公式由 mathematica 创建并转换为 cpp 语言。然后将公式存储在 .txt-file.I 中已经可以将公式作为字符串读取,但不能用它来计算东西,因为程序必须将公式解释为双精度型数学而不是字符串.这里的问题是,我的公式不仅包含数字,还包含作为变量的字母(它们的值在程序中设置)和其他数学符号。这就是为什么我认为我不能只使用 "atof" (http://www.cplusplus.com/reference/cstdlib/atof/?kw=atof) 或其他转换函数。 (如果我在这一点上错了,我很乐意学习如何使用该函数来解决这个问题!)
这是我的小程序中的一些示例代码:
//Program to solve ODEs
#include <iostream>
#include <math.h>
#include <cmath>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <time.h> //to measure the time
#include <stdio.h>
#include <conio.h>
using namespace std;
int main(void)
{
double k1=0;
ifstream file("Formelvu1.txt");//file with the fromula
string line;
stringstream longform;
while(getline(file, line)){ //read the formula and store them
longform << line; //store the string in "longform"
cout << longform;
}
return 0;
for(double t=0; t<10; t++){
k1 = (longform) * t; //simple operation with the formula
}
return 0;
}
此代码无效,因为长格式不是双... longform 是一个字符串,类似于:ab+pow(t,3)-sin(tb)/x.
我已经找到了几个与此主题相关的问题,但其中 none 对我来说很容易理解或我想做的正确事情:
How can I convert string to double in C++?
据我了解,这个人正在尝试最接近我想做的事情:
Evaluate math formula from String using ScriptEngine
但是我没有完全理解代码。 如果它对我的问题有用:这部分是做什么的?
try{
return (Double)engine.eval(tmp);
}
catch(Exception fexp)
{
}
我还听说过可以解释 xpressions 行 muparser 的解析器: http://muparser.beltoforion.de/mup_eval.html 但我不知道这是否会超出我的需要...
感谢大家 answer/response 并帮助解决这个问题。 谢谢!
你要仔细研究基本C/C++语言和标准库中包含什么,不包含什么。
但是,如果您从 Mathematica 获得了 C++ 兼容表达式,那么您可以让 C++ 编译器完成它的工作,如
double myfunc(double a, double b, double c, double t, double x) {
return
#include "Formelvu1.txt"
;
}