#include <iostream>
#include <stdio.h>
using namespace std;
class Complex
{
public:
Complex() { a[1][1][0] = 0; n = 0; m = 0; }
friend Complex operator +(Complex& c1, Complex& c2);
friend Complex operator -(Complex& c1, Complex& c2);
friend Complex operator *(Complex& c1, Complex& c2);
friend Complex operator !(Complex& c1);
friend ostream& operator <<(ostream&, Complex&);
friend istream& operator >>(istream&, Complex&);
private:
double a[10][10][2]; //定义矩阵,矩阵中a[i][j][0]表示复数的实部,a[i][j][1]表示复数的虚部
int n, m; //标识矩阵的大小,由用户输入
};
Complex operator +(Complex& c1, Complex& c2) //矩阵加法的重载实现
{
Complex c3;
if (c1.n != c2.n || c1.m != c2.m) //判断是否能够进行矩阵加法
{
printf("这两个矩阵不能相加\n");
return c3;
}
c3.n = c1.n;
c3.m = c1.m;
for (int i = 1; i <= c1.n; i++)
for (int j = 1; j <= c1.m; j++)
{
c3.a[i][j][0] = c1.a[i][j][0] + c2.a[i][j][0];
c3.a[i][j][1] = c1.a[i][j][1] + c2.a[i][j][1];
}
return c3;
}
Complex operator -(Complex& c1, Complex& c2) //矩阵减法的重载实现
{
Complex c3;
if (c1.n != c2.n || c1.m != c2.m) //判断是否能够进行矩阵减法
{
printf("这两个矩阵不能相减\n");
return c3;
}
c3.n = c1.n;
c3.m = c1.m;
for (int i = 1; i <= c1.n; i++)
for (int j = 1; j <= c1.m; j++)
{
c3.a[i][j][0] = c1.a[i][j][0] - c2.a[i][j][0];
c3.a[i][j][1] = c1.a[i][j][1] - c2.a[i][j][1];
}
return c3;
}
Complex operator *(Complex& c1, Complex& c2) //矩阵乘法的实现
{
Complex c3;
if (c1.m != c2.n) //判断能否进行矩阵乘法
{
printf("这两个矩阵不能相乘\n");
return c3;
}
c3.n = c1.n;
c3.m = c2.m;
double s1 = 0, s2 = 0;
for (int i = 1; i <= c1.n; i++)
{
for (int m = 1; m <= c2.m; m++)
{
for (int j = 1; j <= c1.m; j++)
{
s1 += c1.a[i][j][0] * c2.a[j][m][0] - c1.a[i][j][1] * c2.a[j][m][1];
s2 += c1.a[i][j][0] * c2.a[j][m][1] + c1.a[i][j][1] * c2.a[j][m][0];
}
c3.a[i][m][0] = s1;
c3.a[i][m][1] = s2;
s1 = 0;
s2 = 0;
}
}
return c3;
}
Complex operator !(Complex& c1) //矩阵转置的实现
{
Complex c2;
c2.m = c1.n;
c2.n = c1.m;
for (int i = 1; i <= c1.n; i++)
for (int j = 1; j <= c1.m; j++)
{
c2.a[j][i][0] = c1.a[i][j][0];
c2.a[j][i][1] = -c1.a[i][j][1];
}
return c2;
}
ostream & operator << (ostream & output, Complex &c) //矩阵输出
{
for (int i = 1; i <= c.n; i++)
{
for (int j = 1; j <= c.m; j++)
if (c.a[i][j][1] >= 0)
output << c.a[i][j][0] << '+' << c.a[i][j][1] << "i ";
else output << c.a[i][j][0] << c.a[i][j][1] << "i ";
output << endl;
}
return output;
}
istream & operator >> (istream & input, Complex &c) //矩阵输入
{
printf("请输入矩阵的大小:\n");
input >> c.n >> c.m; //先输入该矩阵的大小
printf("请输入矩阵:\n");
for (int i = 1; i <= c.n; i++)
{
for (int j = 1; j <= c.m; j++)
input >> c.a[i][j][0] >> c.a[i][j][1];
}
return input;
}
int main()
{
Complex c1, c2;
cin >> c1;
cin >> c2;
printf("矩阵1为:\n");
cout << c1;
printf("矩阵2为:\n");
cout << c2;
Complex c3;
printf("矩阵相加结果为:\n");
c3 = c1 + c2;
cout << c3;
printf("矩阵相减结果为:\n");
c3 = c1 - c2;
cout << c3;
printf("矩阵相乘结果为:\n");
c3 = c1 * c2;
cout << c3;
printf("矩阵1的共轭转置矩阵为:\n");
c3 = !c1;
cout << c3;
printf("矩阵2的共轭转置矩阵为:\n");
c3 = !c2;
cout << c3;
}
实数域
#include <iostream>
#include <stdio.h>
using namespace std;
class Complex
{
public:
Complex() { a[1][1][0] = 0; n = 0; m = 0; }
friend Complex operator +(Complex& c1, Complex& c2);
friend Complex operator -(Complex& c1, Complex& c2);
friend Complex operator *(Complex& c1, Complex& c2);
friend Complex operator !(Complex& c1);
friend ostream& operator <<(ostream&, Complex&);
friend istream& operator >>(istream&, Complex&);
private:
double a[10][10][2]; //定义矩阵,矩阵中a[i][j][0]表示复数的实部,a[i][j][1]表示复数的虚部
int n, m; //标识矩阵的大小,由用户输入
};
Complex operator +(Complex& c1, Complex& c2) //矩阵加法的重载实现
{
Complex c3;
if (c1.n != c2.n || c1.m != c2.m) //判断是否能够进行矩阵加法
{
printf("这两个矩阵不能相加\n");
return c3;
}
c3.n = c1.n;
c3.m = c1.m;
for (int i = 1; i <= c1.n; i++)
for (int j = 1; j <= c1.m; j++)
{
c3.a[i][j][0] = c1.a[i][j][0] + c2.a[i][j][0];
// c3.a[i][j][1] = c1.a[i][j][1] + c2.a[i][j][1];
}
return c3;
}
Complex operator -(Complex& c1, Complex& c2) //矩阵减法的重载实现
{
Complex c3;
if (c1.n != c2.n || c1.m != c2.m) //判断是否能够进行矩阵减法
{
printf("这两个矩阵不能相减\n");
return c3;
}
c3.n = c1.n;
c3.m = c1.m;
for (int i = 1; i <= c1.n; i++)
for (int j = 1; j <= c1.m; j++)
{
c3.a[i][j][0] = c1.a[i][j][0] - c2.a[i][j][0];
// c3.a[i][j][1] = c1.a[i][j][1] - c2.a[i][j][1];
}
return c3;
}
Complex operator *(Complex& c1, Complex& c2) //矩阵乘法的实现
{
Complex c3;
if (c1.m != c2.n) //判断能否进行矩阵乘法
{
printf("这两个矩阵不能相乘\n");
return c3;
}
c3.n = c1.n;
c3.m = c2.m;
double s1 = 0, s2 = 0;
for (int i = 1; i <= c1.n; i++)
{
for (int m = 1; m <= c2.m; m++)
{
for (int j = 1; j <= c1.m; j++)
{
s1 += c1.a[i][j][0] * c2.a[j][m][0];
// s2 += c1.a[i][j][0] * c2.a[j][m][1] + c1.a[i][j][1] * c2.a[j][m][0];
}
c3.a[i][m][0] = s1;
// c3.a[i][m][1] = s2;
s1 = 0;
// s2 = 0;
}
}
return c3;
}
Complex operator !(Complex& c1) //矩阵转置的实现
{
Complex c2;
c2.m = c1.n;
c2.n = c1.m;
for (int i = 1; i <= c1.n; i++)
for (int j = 1; j <= c1.m; j++)
{
c2.a[j][i][0] = c1.a[i][j][0];
// c2.a[j][i][1] = -c1.a[i][j][1];
}
return c2;
}
ostream & operator << (ostream & output, Complex &c) //矩阵输出
{
for (int i = 1; i <= c.n; i++)
{
for (int j = 1; j <= c.m; j++)
if (c.a[i][j][1] >= 0)
output << c.a[i][j][0] << ' ';
else output << c.a[i][j][0] << ' ';
output << endl;
}
return output;
}
istream & operator >> (istream & input, Complex &c) //矩阵输入
{
printf("请输入矩阵的大小:\n");
input >> c.n >> c.m; //先输入该矩阵的大小
printf("请输入矩阵:\n");
for (int i = 1; i <= c.n; i++)
{
for (int j = 1; j <= c.m; j++)
input >> c.a[i][j][0];
}
return input;
}
int main()
{
Complex c1, c2;
cin >> c1;
cin >> c2;
printf("矩阵1为:\n");
cout << c1;
printf("矩阵2为:\n");
cout << c2;
Complex c3;
printf("矩阵相加结果为:\n");
c3 = c1 + c2;
cout << c3;
printf("矩阵相减结果为:\n");
c3 = c1 - c2;
cout << c3;
printf("矩阵相乘结果为:\n");
c3 = c1 * c2;
cout << c3;
printf("矩阵1的转置矩阵为:\n");
c3 = !c1;
cout << c3;
printf("矩阵2的转置矩阵为:\n");
c3 = !c2;
cout << c3;
}
Comments NOTHING