利用c/c++创建复数二维数组,也就是复数矩阵,同时把matlab生成的复数矩阵保存为txt格式的数据读入c/c++的复数矩阵里。

利用c/c++创建复数二维数组,也就是复数矩阵,同时把matlab生成的复数矩阵保存为txt格式的数据读入c/c++的复数矩阵里。
希望能够说的详细一点点,实在看不懂网络上的各种教程

1、先写matlab导出的

A=[1,3;2,4;0,2]-[5,8;6,9;1,3]*i;
dlmwrite('C:\\Users\\Lenovo\\Desktop\\1.txt', A);

效果:

img

2、c++

其实它有定义,但是我不太了解,直接用自定义的

#include<iostream>
#include<fstream>    
#include <string>
#include<vector>
using namespace std;

//自定义复数类
class CPLX {
public:
    double r, i;
    CPLX(double rr = 0, double ii = 0) :r(rr), i(ii) {

    }
    friend istream& operator>>(istream& is, CPLX& C) {
        char tmp = '\0',top='\0';
        top = is.peek();
        double a, b;
        if (top == '+' || top == '-' || (int('0') <= (int)top && (int)top <= int('9'))) {
            is >> a;
            top = is.peek();
            if (top == 'I' || top == 'i') {//bi
                C.i = a; C.r = 0;
                is >> tmp;
                return is;
            }
            else if (top == '+' || top == '-') {
                is >> b;
                top = is.peek();
                if (top == 'I' || top == 'i') {//a+/-bi
                    C.r = a, C.i = b;
                    is >> tmp; 
                    return is;
                }
            }
            else {//a
                C.r = a; C.i = 0; 
                return is;
            }
        }
        //剩下的全是不符合格式的,抛异常处理
        throw  invalid_argument("输入的复数格式不正确,应该为[a+bi,a-bi,a,bi]");
        is.fail();
        return is;
    }
    friend ostream& operator<<(ostream& os, const CPLX& c) {
        if (c.i == 0.0)
            os << std::noshowpos<< c.r;
        else if (c.r == 0.0) {
            if (c.i == 1)
                os << 'i';
            else  if (c.i == -1)
                os << '-' << 'i';
            else
                 os << c.i << 'i';
        }           
        else
            os << std::noshowpos << c.r << std::showpos << c.i<< 'i';
        return os;
    }
};

int main()
{
    CPLX tmpCP;
    vector<vector<CPLX>>source;
    const char* path = "C:\\Users\\Lenovo\\Desktop\\1.txt";
    ifstream myfile(path);
    if (!myfile.is_open())
    {
        cout << "未成功打开文件" << endl;
    }
    char tmpStr = '\0';
    int i = 0;
    source.push_back(vector<CPLX>());
    while (myfile >> tmpCP) {
        source[i].push_back(tmpCP);
        tmpStr = myfile.peek();
        if (tmpStr != ',' && tmpStr != EOF) {//不在同一行
            i += 1;
            source.push_back(vector<CPLX>());
            myfile.get(tmpStr);
            if (myfile.peek() == EOF)//判断是否读取到文件最后
                break;
        }
        else if(tmpStr == EOF)//判断是否读取到文件最后
            break;
        else {
            myfile >> tmpStr;
        }
       
    }
    if (source[i].size() == 0)
        source.pop_back();
    myfile.close();
    int w = 0, h = 0, H = 0;
    for (auto iter = source.begin(); iter != source.end(); ++iter) {//遍历行
        h = 0;
        for (int i = 0; i < (*iter).size(); ++i) {//遍历列
            cout << (*iter)[i] << " ";
            h += 1;
        }
        if (H != 0 && h != H)
            throw  invalid_argument("要串联的数组的维度不一致。");
        else
            H = h;
        w += 1;
        cout << endl;
    }
    cout << "shape=(" << std::noshowpos<<w << "," << H << ")" << endl;
}


    

img

#include <iostream>
#include <complex>
#include <vector>
#include <cassert>
#include <algorithm>

template <typename T>
class ComplexMatrix
{
public:
    typedef T value_type;
    typedef std::complex<T> complex_type;
    typedef std::size_t size_type;

    ComplexMatrix() {}

    ComplexMatrix(size_type rows, size_type columns) : _rows(rows), _columns(columns), _data(rows * columns) {}

    ComplexMatrix(const ComplexMatrix &other) : _rows(other._rows), _columns(other._columns), _data(other._data) {}

    ComplexMatrix(ComplexMatrix &&other) : _rows(other._rows), _columns(other._columns), _data(std::move(other._data))
    {
        other._rows = 0;
        other._columns = 0;
    }

    ~ComplexMatrix() {}

    ComplexMatrix &operator=(const ComplexMatrix &other)
    {
        if (this != &other)
        {
            _rows = other._rows;
            _columns = other._columns;
            _data = other._data;
        }
        return *this;
    }

    ComplexMatrix &operator=(ComplexMatrix &&other)
    {
        _rows = other._rows;
        _columns = other._columns;
        _data = std::move(other._data);
        other._rows = 0;
        other._columns = 0;
    }

    bool operator==(const ComplexMatrix &other) const
    {
        return _rows == other._rows && _columns == other._columns && _data = other._data;
    }

    bool operator!=(const ComplexMatrix &other) const
    {
        return !(*this == other);
    }

    size_type rows() const { return _rows; }
    size_type columns() const { return _columns; }

    const complex_type &operator()(size_type i, size_type j) const
    {
        assert(i < _rows && j < _columns);
        return _data[i * _columns + j];
    }

    complex_type &operator()(size_type i, size_type j)
    {
        assert(i < _rows && j < _columns);
        return _data[i * _columns + j];
    }

    void clear()
    {
        _rows = 0;
        _columns = 0;
        _data.clear();
    }

private:
    static std::istream &read(std::istream &is, complex_type &c);
    static std::ostream &write(std::ostream &os, const complex_type &c);

    template <typename Type>
    friend std::istream &operator>>(std::istream &is, ComplexMatrix<Type> &matrix);
    template <typename Type>
    friend std::ostream &operator<<(std::ostream &os, const ComplexMatrix<Type> &matrix);

    size_type _rows = 0;
    size_type _columns = 0;
    std::vector<complex_type> _data;
};

template <typename T>
std::istream &ComplexMatrix<T>::read(std::istream &is, complex_type &c)
{
    T real, imag;
    char ch;
    // Try to read a complex number with both the real part and the imaginary
    // part.
    if ((is >> real >> imag >> ch) && (std::tolower(ch) == 'i'))
    {
        c = complex_type(real, imag);
    }
    else
    {
        is.clear();
        is.seekg(0);
        // Try to read a complex number with only the imaginary part.
        if ((is >> imag >> ch) && (std::tolower(ch) == 'i'))
        {
            c = complex_type(0.0, imag);
        }
        else
        {
            is.clear();
            is.seekg(0);
            // Try to read a complex number with only the real part.
            if (is >> real >> std::ws)
            {
                c = complex_type(real, 0.0);
            }
            else
            {
                // Invalid complex number.
                is.fail();
            }
        }
    }
    return is;
}

template <typename T>
std::ostream &ComplexMatrix<T>::write(std::ostream &os, const complex_type &c)
{
    if (c.imag() == 0.0)
        os << c.real();
    else if (c.real() == 0.0)
        os << c.imag() << 'i';
    else
        os << std::noshowpos << c.real() << std::showpos << c.imag() << 'i';
    return os;
}

template <typename T>
std::istream &operator>>(std::istream &is, ComplexMatrix<T> &matrix)
{
    typedef typename ComplexMatrix<T>::complex_type complex_type;
    std::string line;
    matrix.clear();
    while (getline(is, line))
    {
        int n = 0;
        std::size_t s = 0, e = 0;
        while (s != std::string::npos)
        {
            e = line.find_first_of(',', s);
            auto str = line.substr(s, e - s);
            if (e != std::string::npos)
                e++;
            std::remove_if(str.begin(), str.end(), [](auto c)
                           { return std::isspace(c); });
            std::istringstream ss(str);
            complex_type c;
            if (ComplexMatrix<T>::read(ss, c))
            {
                matrix._data.push_back(c);
                n++;
            }
            else
            {
                is.fail();
                break;
            }
            s = e;
        }
        matrix._columns = n;
        matrix._rows++;
    }
    return is;
}

template <typename T>
std::ostream &operator<<(std::ostream &os, const ComplexMatrix<T> &matrix)
{
    for (size_t i = 0; i < matrix._rows; i++)
    {
        for (size_t j = 0; j < matrix._columns; j++)
        {
            ComplexMatrix<T>::write(os, matrix(i, j));
            os << ((j < matrix._columns - 1) ? ',' : '\n');
        }
    }
    return os;
}

int main()
{
    ComplexMatrix<double> matrix;
    std::cin >> matrix;
    std::cout << "Rows: " << matrix.rows() << ", Columns: " << matrix.columns() << '\n';
    std::cout << matrix;
    return 0;
}
$  g++ -Wall main.cpp
$ ./a.out
1, 2, 3 
-2i, 0, 2.1-3.1i
2 + 3 i, 4 - 5 i, -2 - 2 i
Rows: 3, Columns: 3
1,2,3
-2i,0,2.1-3.1i
2+3i,4-5i,-2-2i