码迷,mamicode.com
首页 > Windows程序 > 详细

用C#实现复数运算

时间:2014-12-09 12:20:21      阅读:463      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   os   sp   on   2014   log   cti   

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sample_01_CA
{
    public class Complex
    {
        //定义复试的实部和虚部
        private int integer;
        private int fraction;
        //构造函数
        public Complex(int integer,int fraction)
        {
            this.integer = integer;
            this.fraction = fraction;
        }
        //重载运算符
        public static Complex operator +(Complex left,Complex right)
        {
            return new Complex(left.integer + right.integer, left.fraction + right.fraction);
        }
        //重写从Object继承的ToString()方法
        public override string ToString()
        {
            return integer.ToString()+"."+fraction.ToString();
        }
        static void Main(string[] args)
        {
            Complex left = new Complex(2008, 10);
            Complex right = new Complex(100, 1);
            Console.WriteLine(left.ToString() + " + " + right.ToString() + " = " + (left + right).ToString());
            Console.Read();
        }
    };
}

用C#实现复数运算

标签:blog   io   ar   os   sp   on   2014   log   cti   

原文地址:http://blog.csdn.net/qsyzb/article/details/41821731

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!