基本信息
源码名称:运算符重载
源码大小:0.15M
文件格式:.zip
开发语言:C#
更新时间:2021-04-05
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 2 元 
   源码介绍

通过运算符重载实现矩阵运算

static Matrix operator *(Matrix a, Matrix b)
        {
            Matrix t = new Matrix();
            t.set00Matrix();
            if (a.n == b.m)
            {
                for (int i = 0; i < a.m; i )
                {
                    for (int j = 0; j < b.n; j )
                    {
                        for (int k = 0; k < a.n; k )
                        {
                            t.matrix[i, j] = t.matrix[i, j] a.matrix[i, k] * b.matrix[k, j];
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("您输入的两个矩阵不满足相乘条件,无法相乘!!!");
                Export.flag = 0;
            }
            return t;
        }
        public static Matrix operator *(Matrix a, double b)
        {
            Matrix t = new Matrix();
            for (int i = 0; i < a.m; i )
                for (int j = 0; j < a.n; j )
                {
                    t.matrix[i, j] = a.matrix[i, j] * b;
                }
            return t;
        }
        public static Matrix operator *(double b, Matrix a)
        {
            Matrix t = new Matrix();
            for (int i = 0; i < a.m; i )
                for (int j = 0; j < a.n; j )
                {
                    t.matrix[i, j] = a.matrix[i, j] * b;
                }
            return t;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Complex A = new Complex();
            Complex B = new Complex();
            Complex C = new Complex();
            Console.WriteLine("请输入第一个复数 A 实部和虚部:");
            A.real = double.Parse(Console.ReadLine());
            A.imag = double.Parse(Console.ReadLine());
            Console.WriteLine("请输入第二个复数 B 实部和虚部:");
            B.real = double.Parse(Console.ReadLine());
            B.imag = double.Parse(Console.ReadLine());
            Console.Write("A B=:");
            C = A B;
            Console.WriteLine(C.print());
            Console.Write("A-B=:");
            C = A - B;
            Console.WriteLine(C.print());
            Console.Write("A*B=:");
            C = A * B;
            Console.WriteLine(C.print());
            Console.Write("A/B=:");
            C = A / B;
            Console.WriteLine(C.print());
            Matrix M = new Matrix();
            Matrix N = new Matrix();
            Matrix Q = new Matrix();
            Console.WriteLine("请输入第一个矩阵M的行数m和列数n:");
            M.m = int.Parse(Console.ReadLine());
            M.n = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入第一个矩阵M的元素:");
            M.setMatrix();
            Console.WriteLine("请输入第二个矩阵N的行数m和列数n:");
            N.m = int.Parse(Console.ReadLine());
            N.n = int.Parse(Console.ReadLine());
            Console.WriteLine("请输入第二个矩阵N的元素:");
            N.setMatrix();
            Q = M N;
            Q.m = M.m;
            Q.n = M.n;
            if (Export.flag != 0)
            {
                Console.WriteLine("M N=:");
                Q.output();
            }
            Export.flag = 1;
            Q = M - N;
            Q.m = M.m;
            Q.n = M.n;
            if (Export.flag != 0)
            {
                Console.WriteLine("M-N=:");
                Q.output();
            }
            Export.flag = 1;
            Q = M * N;
            Q.m = M.m;
            Q.n = N.n;
            if (Export.flag != 0)
            {
                Console.WriteLine("M*N=:");
                Q.output();
            }
            Console.WriteLine("请输入与矩阵相乘的数:k=");
            double k = double.Parse(Console.ReadLine());
            Q = k * M;
            Q.m = M.m;
            Q.n = N.n;
            Console.WriteLine("k*M=:");
            Q.output();
            Console.Read();
        }
    }
}