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

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日

悬赏问题

  • ¥15 数学建模——参会安排怎么做
  • ¥15 电脑键盘实现触摸功能
  • ¥25 matlab无法将表达式转换为双数组怎么解决?
  • ¥15 单片机汇编语言相关程序
  • ¥20 家用射频美容仪技术规格
  • ¥15 大家帮我看看为什么错了
  • ¥15 unity互动琴弦抖动效果
  • ¥15 做了个的二极管反向饱和电流测量电路,但是测试达不到效果
  • ¥15 nginx无证书访问https失败
  • ¥15 树莓派启动AP热点传入数据