| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 541 人关注过本帖
标题:关于运算符的重载
只看楼主 加入收藏
陌晗蔓
Rank: 1
来 自:安徽
等 级:新手上路
帖 子:4
专家分:0
注 册:2014-3-16
结帖率:100%
收藏
 问题点数:0 回复次数:0 
关于运算符的重载
下列程序定义了一实部为real,虚部为imag的复数类complex,要求重载运算符“+”、“-”、“*”实现复数对象的加法、减法、乘法运算,并编写测试程序。
class Complex
{
private:
        double  real, imag;
public:
        complex(double r, double i)
        {
            real=r;
            imag=i;
        }
};
下面是我写的程序,但是有错误,请帮我修改一下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 实验二_2
{
    class Program
    {
        static void Main(string[] args)
        {
            Complex c1 = new Complex( );
            Complex c2 = new Complex( );
            Console.WriteLine("请输入一个复数的实部和虚部:");
            c1.Real =double.Parse ( Console.ReadLine());
            c1.Imag =double.Parse (Console.ReadLine());
            Console.WriteLine("请输入一个复数的实部和虚部:")
            c2.Real =double.Parse (Console.ReadLine());
            c2.Imag =double.Parse ( Console.ReadLine());
            Complex c3= double.Parse (operator+(c1,c2));
            Console.WriteLine( "两个复数的和为:{0}",c3.Write( ));
            Complex c4=double.Parse (operator-(c1,c2));
            Console.WriteLine( "两个复数的差为:{0}",c4.Write( ));
            Complex c5=double.Parse (operator*(c1,c2));
            Console.WriteLine( "两个复数的积为:{0}",c5.Write( ));
    }
    class Complex
    {
        private double real,imag;
        public double Real
        {
            get { return real;}
            set { real=value;}
        }
        public double Imag
        {
            get { return imag;}
            set { imag =value;}
        }
        public Complex (double r,double i)
        {
            real=r;
            imag=i;
        }
        public static Complex operator+(Complex c1,Complex c2)
        {
            return new Complex (c1.real+c2.real,c1.imag +c2.imag );
        }
        public static Complex operator-(Complex c1,Complex c2)
        {
            return new Complex (c1.real-c2.real ,c1.imag-c2.imag);
        }
        public static Complex operator*(Complex c1,Complex c2)
        {;
            return new Complex (c1.real*c2.real ,c1.imag*c2.imag);
        }
        public void Write()
        {
            Console.WriteLine ("{0}+{1}i",real,imag);
        }
   
        }
    }
}
搜索更多相关主题的帖子: Complex private complex public double 
2014-10-11 20:42
快速回复:关于运算符的重载
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.039946 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved