创建4个复数对象构建复数数组。两个复数求和并打印至控制台。构建2*2的复数矩阵。
时间: 2024-05-16 17:13:04 浏览: 5
下面是使用 C# 语言实现的代码示例:
```csharp
using System;
class ComplexNumber
{
public double Real { get; set; }
public double Imaginary { get; set; }
public ComplexNumber(double real, double imaginary)
{
Real = real;
Imaginary = imaginary;
}
public static ComplexNumber operator +(ComplexNumber c1, ComplexNumber c2)
{
return new ComplexNumber(c1.Real + c2.Real, c1.Imaginary + c2.Imaginary);
}
public override string ToString()
{
return $"{Real} + {Imaginary}i";
}
}
class Program
{
static void Main()
{
ComplexNumber[] array = new ComplexNumber[4];
array[0] = new ComplexNumber(1, 2);
array[1] = new ComplexNumber(3, 4);
array[2] = new ComplexNumber(5, 6);
array[3] = new ComplexNumber(7, 8);
ComplexNumber sum = array[0] + array[1];
Console.WriteLine(sum);
ComplexNumber[,] matrix = new ComplexNumber[2, 2];
matrix[0, 0] = new ComplexNumber(1, 2);
matrix[0, 1] = new ComplexNumber(3, 4);
matrix[1, 0] = new ComplexNumber(5, 6);
matrix[1, 1] = new ComplexNumber(7, 8);
Console.WriteLine($"[{matrix[0, 0]}, {matrix[0, 1]}]");
Console.WriteLine($"[{matrix[1, 0]}, {matrix[1, 1]}]");
}
}
```
输出:
```
4 + 6i
[1 + 2i, 3 + 4i]
[5 + 6i, 7 + 8i]
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)