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

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]
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • CSDN专家-link 2021-07-11 17:33

    你的下标处理有问题,循环是从i=0,j=0开始的,那么最终a的下标计算应该是i*n+j,而不是(i-1)*n+j-1,否则都是越界访问

    #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 * n +j] = (p.a)[i * n + j] + (p.a)[i * n + j];
        }
    }
    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 * c.n + j];
            }
        }
        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 * c.n + j];
            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;
    }
    
    
    评论
    解决 无用
    打赏 举报
  • Aurora75 2021-07-11 14:50

    补充一下,任何维度的矩阵都会在第二个对象的右上角元素上出现同样的问题,到底是为什么啊

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题