dhwl1234 2022-03-12 22:06 采纳率: 0%
浏览 321
已结题

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

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

  • 写回答

2条回答 默认 最新

  • 广大菜鸟 2022-03-13 01:24
    关注
    获得5.40元问题酬金

    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

    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 3月20日
  • 创建了问题 3月12日

悬赏问题

  • ¥15 虚拟机检测,可以是封装好的DLL,可付费
  • ¥15 kafka无法正常启动(只启动了一瞬间会然后挂了)
  • ¥15 开发一个类似百度网盘的软件,在主页 文件列表点击进入文件夹,在文件夹里面还有文件夹,代码该怎么写?
  • ¥30 使用matlab将观测点聚合成多条目标轨迹
  • ¥15 Workbench中材料库无法更新,如何解决?
  • ¥20 如何推断此服务器配置
  • ¥15 关于github的项目怎么在pycharm上面运行
  • ¥15 内存地址视频流转RTMP
  • ¥100 有偿,谁有移远的EC200S固件和最新的Qflsh工具。
  • ¥15 有没有整苹果智能分拣线上图像数据