设计复数类Complex进行计算

// Copyright (c) 2014软件技术2班      
// All rights reserved.       
// 作    者:B39        
// 完成日期:2014年 12 月 8 日       
// 版 本 号:v1.0          
// 问题描述:设计复数类Complex进行计算  
//输入描述:设计复数类Complex,计算两个复数之和、差,同时以abi 的字符串形式显示。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    public struct Complex
    {
        public int x;//X为实数
        public int y;//Y为虚数
        public Complex(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        public static Complex operator +(Complex a, Complex b)
        {
            return new Complex(a.x + b.x, a.y + b.y);
        }
        public override string ToString()
        {
            return (String.Format("{0}+{1}i",x,y));
        }

    }
    public struct Complex1
    {
        public int x;//X为实数
        public int y;//Y为虚数
        public Complex1(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
        public static Complex1 operator -(Complex1 a, Complex1 b)
        {
            return new Complex1(a.x - b.x, a.y - b.y);
        }
        public override string ToString()
        {
            return (String.Format("{0}{1}i", x, y));
        }

    }

    class 复数
    {
        static void Main(string[] args)
        {
            Complex num1 = new Complex(1, 2);
            Complex num2 = new Complex(3, 4);
            Complex1 num3 = new Complex1(1, 2);
            Complex1 num4 = new Complex1(3, 4);
            Complex complexAdd = num1 + num2;
            Complex1 complexAdd1 =num3-num4;
           
           
            Console.WriteLine("第一个复数:{0}", num1);
            Console.WriteLine("第二个复数:{0}", num2);
            Console.WriteLine("两个复数之和:{0}", complexAdd);
            Console.WriteLine("两个复数之差:{0}", complexAdd1);
            Console.ReadKey();
        }
    }
}
输出:

总结:
     通过编写这个程序,我学会了复数类Complex的计算方法。


你可能感兴趣的:(设计复数类Complex进行计算)