Aurora75 2021-07-11 14:07 采纳率: 100%
浏览 152
已采纳

C++用类实现复数矩阵操作遇到的问题?

码:

#include<iostream>
using namespace std;

class Complex
{
public:
    Complex(double x = 0, double y = 0) :real(x), imag(y) {};
    friend istream& operator>>(istream& , Complex& c);
    friend ostream& operator<<(ostream& , const Complex& c);
    friend Complex operator + (const Complex& c1, const Complex& c2);
private:
    double real;
    double imag;
};
class Complex_matrix
{
public:
    Complex_matrix(int x=0,int y=0);
    Complex_matrix(Complex_matrix& p);
    friend istream& operator>>(istream&, Complex_matrix&);
    friend ostream& operator<<(ostream&, const Complex_matrix&);
    friend Complex_matrix operator + (const Complex_matrix& c1, const Complex_matrix& c2);
private:
    int m;
    int n;
    Complex*a;
};
istream& operator>>(istream& input1, Complex& c)
{
    input1>> c.real >> c.imag;
    return input1;
}
ostream& operator<<(ostream& output1, const Complex& c)
{
    output1 << "(" << c.real << "+" << c.imag << "i)" ;
    return output1;
}
Complex operator + (const Complex& c1, const Complex& c2)
{
    return Complex(c1.real + c2.real, c1.imag + c2.imag);
}


/*................................这是分割线......................................*/

Complex_matrix::Complex_matrix(int x, int y)
{
    if (x > 0 && y > 0)
    {
        a = new Complex[x * y];
        m = x;
        n = y;
    }
}
Complex_matrix::Complex_matrix(Complex_matrix& p)
{
    a = new Complex[p.m * p.n];
    m = p.m;
    n = p.n;
    for (int i = 0;i < m;i++)
    {
        for (int j = 0;j < n;j++)
            (a)[(i - 1) * n + j - 1] = (p.a)[(i - 1) * n + j - 1] + (p.a)[(i - 1) * n + j - 1];
    }

}
istream& operator>>(istream& input2, Complex_matrix& c)
{
    for (int i = 0;i < c.m;i++)
    {
        for (int j = 0;j < c.n;j++)
        {
            cout << "请输入第(" << i + 1 << "," << j + 1 << ")位的元素" << endl;
            cin >> (c.a)[(i - 1) * c.n + j - 1];
        }
    }
    return input2;

}
ostream& operator<<(ostream& output2, const Complex_matrix& c)
{
    for (int i = 0;i < c.m;i++)
    {
        for (int j = 0;j < c.n;j++)
            cout << (c.a)[(i - 1) * c.n + j - 1];
        cout << endl;
    }
    return output2;
}
Complex_matrix operator + (const Complex_matrix& c1, const Complex_matrix& c2)
{
    cout << c1;
    cout << c2;
        Complex_matrix result(3, 2);
        return result;

}
int main()
{

        Complex_matrix m1(2, 2);
        cin >> m1;
        cout << m1;
        Complex_matrix m2(2, 2);
        cin >> m2;
        cout << m2;
}

对m1和m2进行赋值操作的时候为什么会出现这样的情况,对第一个对象能够正常赋值,对第二个对象的右上角元素不能进行正常赋值,如图:

img

  • 写回答

3条回答 默认 最新

  • Soda Wang 2021-07-11 17:19
    关注

    你index错了,应该是

    (c.a)[i*c.n + j]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已采纳回答 7月11日
  • 创建了问题 7月11日

悬赏问题

  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用