如何将方程式放入矩阵中

How to put equations into a matrix?

本文关键字:方程式      更新时间:2023-10-16

我正在编写一个程序来求解多个方程,但这与此无关。我遇到的问题是解方程式。

例如,我有一个名为data.txt的文件,其内容如下:

2x - 5y + 3z = 10
5x + y - 2z = 4

我已经尝试了很长一段时间来解释它,但没有成功,因为我认为C++会有类似str.split()的东西

2 -5 3 10
5 1 -2 4

我该怎么做?

vector>数据

逐行读取data.txt:字符串行

使用分隔符"\t+-="拆分行:矢量标记

将令牌转换为数字格式:vector v

将v推送到数据:data.push_back(v)

更新:

vector<string> split(const string &s, const string &d)
{
    vector<string> t;
    string::size_type i = s.find_first_not_of(d);
    string::size_type j = s.find_first_of(d,i);
    while (string::npos != i || string::npos != j) {
        t.push_back(s.substr(i,j-i));
        i = s.find_first_not_of(d,j);
        j = s.find_first_of(d,i);
    }
    return t;
}
int main()
{
    vector<vector<double> > x;
    ifstream ifs("data.txt");
    string ls;
    while (getline(ifs,ls))
    {
        vector<string> ts = split(ls," t+-=");
        vector<dobule> v;
        for (auto& s : ts)
            v.push_back( atof(s.c_str()) );
        x.push_back(v);
    }
    return 0;
}