0

我正在开发一个解决多个方程的程序,但这不相关。我遇到的问题是解释方程式。

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

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

我一直在尝试解释这一点,但没有成功,因为我认为 C++ 会有类似 str.split() 的东西。我想到的是一个包含这些内容的数组:

2 -5 3 10
5 1 -2 4

请问我该怎么做?

4

1 回答 1

-1

矢量 > 数据

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

使用分隔符“\t+-=”分割线:向量标记

将标记转换为数字格式:向量 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;
}
于 2012-09-13T11:40:20.927 回答