| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2299 人关注过本帖
标题:运算符重载这块,我好像做错了,该怎么改??
只看楼主 加入收藏
仓乃梦
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:20
专家分:0
注 册:2016-10-3
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
运算符重载这块,我好像做错了,该怎么改??
设计人民币类RMB
(1)私有字段三个分别为:元、角、分;并封装三个字段的属性;
(2)公有方法如下:
   1)public RMB(uint yuan, uint jiao, uint fen)
          public RMB(uint fen)
   2)void StandRMB()  //将人民币标准化
   3)  public static RMB operator +(RMB r1, RMB r2) //人民币加人民币运算符重载
   4) public static RMB operator -(RMB r1, RMB r2) //人民币减人民币运算符重载
         public static RMB operator ++(RMB r)
        public static RMB operator --(RMB r)
 5) public static bool operator ==(RMB r1, RMB r2)
      public static bool operator !=(RMB r1, RMB r2)
      public override bool Equals(object obj)
   
  6)public override string ToString()//以元角分的形式输出人民币
(3)设计测试类,完成上述类的测试。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace rmb
{
    class Program
    {
        static void Main(string[] args)
        {
            RMB rm1 = new RMB() { Yuan = 100, Jiao = 5, Fen = 7 };
            RMB rm2 = new RMB() { Yuan = 200, Jiao = 6, Fen = 5 };
            RMB rm3 = new RMB();
            RMB rm4 = new RMB();
            rm3 = rm1 + rm2;
            rm4 = rm1 - rm2;
            Console.WriteLine("the add is {0}", rm3);
            Console.WriteLine("jian hou is {0}", rm4);
            Console.Read();
        }
    }

    class RMB
    {
        private uint yuan;

        public uint Yuan
        {
            get { return yuan; }
            set { yuan = value; }
        }
        private uint jiao;

        public uint Jiao
        {
            get { return jiao; }
            set { jiao = value; }
        }
        private uint fen;

        public uint Fen
        {
            get { return fen; }
            set { fen = value; }
        }
        public RMB(uint yuan, uint jiao, uint fen)
        {
            this.yuan = yuan;
            this.jiao = jiao;
            this.fen = fen;
        }
        public RMB(uint fen)
        {
            uint f;
            f = fen;
            f = yuan * 100 + jiao * 10 + fen;
            fen = f % 10;
            jiao = f % 10 - (f / 100 * 10);
            yuan = f / 100;
        }
        public RMB()
        {
        
        }
      public  void  StandRMB()
        {
            unit f = yuan * 100 + jiao * 10 + fen;
           fen =f%10;
           jiao =f%10-(f/100*10);
           yuan=f/100;
        }
       public static RMB operator +(RMB r1, RMB r2)
       {
           RMB temp = new RMB();
           temp.yuan = r1.yuan + r2.yuan;
           temp.jiao = r1.jiao + r2.jiao;
           temp.fen = r1.fen + r2.fen;
           return temp;
       }
       public static RMB operator -(RMB r1, RMB r2)
       {
           RMB temp = new RMB();
           temp.yuan = r1.yuan - r2.yuan;
           temp.jiao = r1.jiao - r2.jiao;
           temp.fen = r1.fen - r2.fen;
           return temp;
       }
        public static RMB operator ++(RMB r)
        {
            uint f;
            f = r.yuan * 100 + r.jiao * 10 + r.Fen;
            f++;
            return new RMB(f);
           
        }
        public static RMB operator --(RMB r)
        {
            uint f;
            f = r.yuan * 100 + r.jiao * 10 + r.fen;
            f--;
            return new RMB(f);
        }
        public static bool operator ==(RMB r1, RMB r2)
        {
            return (r1.yuan == r2.yuan) && (r1.jiao == r2.jiao) == (r1.fen == r2.fen);
            
        }
        public static bool operator !=(RMB r1, RMB r2)
        {
            return (r1.yuan != r2.yuan) || (r1.jiao!= r2.jiao) || (r1.fen != r2.fen);      
        }
        public override string ToString()
        {
            StandRMB();
            return string .Format ("{0}yuan{1}jiao{2}fen",yuan,jiao ,fen );
        }

    }
}
搜索更多相关主题的帖子: 人民币 public 
2016-10-18 22:43
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:20 
程序代码:
/*

 设计人民币类RMB
(1)私有字段三个分别为:元、角、分;并封装三个字段的属性;
(2)公有方法如下:
   1)public RMB(uint yuan, uint jiao, uint fen) 
          public RMB(uint fen)
   2)void StandRMB()  //将人民币标准化
   3)  public static RMB operator +(RMB r1, RMB r2) //人民币加人民币运算符重载
   4) public static RMB operator -(RMB r1, RMB r2) //人民币减人民币运算符重载
         public static RMB operator ++(RMB r)
        public static RMB operator --(RMB r)

 5) public static bool operator ==(RMB r1, RMB r2)
      public static bool operator !=(RMB r1, RMB r2)
      public override bool Equals(object obj)
    
  6)public override string ToString()//以元角分的形式输出人民币
(3)设计测试类,完成上述类的测试。*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace rmb
{
    class Program
    {
        static void Main(string[] args)
        {
            //以下4行构造全部都是调用的无参构造函数,只是前两行另调用了类属性的Set方法进行赋值
            //RMB rm1 = new RMB() { Yuan = 100, Jiao = 5, Fen = 7 };
            //RMB rm2 = new RMB() { Yuan = 200, Jiao = 6, Fen = 5 };
            //RMB rm3 = new RMB();
            //RMB rm4 = new RMB();


            /**
            *   RMB rm1 = new RMB() { Yuan = 100, Jiao = 5, Fen = 7 };这种写法与下面的写法是相同的,只是一种简写,并非调用有参构造函数进行类的实例化
            *   
            *   RMB rm1 = new RMB();
            *   rm1.Yuan = 100;
            *   rm1.Jiao = 5;
            *   rm1.Fen = 7;
            **/

            //rm3 = rm1 + rm2;
            //rm4 = rm1 - rm2;
            //Console.WriteLine("the add is {0}", rm3);
            //Console.WriteLine("jian hou is {0}", rm4);

            RMB rm1 = new RMB(100, 5, 7);
            Console.WriteLine("rm1为:" + rm1.ToString());

            RMB rm2 = new RMB(20065);
            Console.WriteLine("rm2为:" + rm2.ToString());

            RMB rm3 = rm1 + rm2;
            Console.WriteLine("rm1 + rm2为:" + rm3.ToString());

            RMB rm4 = rm1 - rm2;
            Console.WriteLine("rm1 - rm2为:" + rm4.ToString());

            rm1++;
            Console.WriteLine("rm1++为:" + rm1.ToString());

            rm2--;
            Console.WriteLine("rm2--为:" + rm2.ToString());

            RMB rm7 = new RMB(0);
            Console.WriteLine("rm7为:" + rm7.ToString());
            rm7--;
            Console.WriteLine("rm7--为:" + rm7.ToString());


            RMB rm5 = new RMB(100, 5, 7);
            RMB rm6 = new RMB(10057);
            if (rm5 == rm6)     //测试重载==号
            {
                Console.WriteLine("rm5==rm6");
            }
            else
            {
                Console.WriteLine("rm5与rm6不相等");
            }

            rm5++;
            if (rm5 != rm6) //测试重载!=号
            {
                Console.WriteLine("rm5!=rm6");
            }
            else
            {
                Console.WriteLine("rm5与rm6相等");
            }
            Console.Read();
        }
    }

    class RMB
    {
        private uint yuan;

        public uint Yuan
        {
            get { return yuan; }
            set { yuan = value; }
        }
        private uint jiao;

        public uint Jiao
        {
            get { return jiao; }
            set { jiao = value; }
        }
        private uint fen;

        public uint Fen
        {
            get { return fen; }
            set { fen = value; }
        }

        /// <summary>
        /// 这里增加了一个的bool型变量,用于表示正负,true表示正,false表示负
        /// </summary>
        private bool flag = true;

        public bool Flag 
        { 
            get { return flag; } 
        }

        /// <summary>
        /// 根据元角分构造
        /// </summary>
        /// <param name="yuan"></param>
        /// <param name="jiao"></param>
        /// <param name="fen"></param>
        public RMB(uint yuan, uint jiao, uint fen)
        {
            this.yuan = yuan;
            this.jiao = jiao;
            this.fen = fen;
            //因为可能传入的参数不是标准的,例入传入100元,35角,89分,所以还是再调用一次标准化函数
            this.StandRMB();
        }

        /// <summary>
        /// 根据分构造
        /// </summary>
        /// <param name="fen"></param>
        public RMB(uint fen)
        {
            //uint f;
            //f = fen;
            //f = yuan * 100 + jiao * 10 + fen;
            //fen = f % 10;
            //jiao = f % 10 - (f / 100 * 10);
            //yuan = f / 100;
            //这个构造函数可以肯定传入的参数不是标准的,所以代码不用再写一遍,直接调用标准化函数
            this.fen = fen;
            this.StandRMB();
        }

        //可以不需要无参构造函数,保留也是可以的(因为提供了属性的Get和Set方法),只是题目上并未作此要求
        public RMB()
        {

        }

        /// <summary>
        /// 标准化RMB
        /// </summary>
        public void StandRMB()
        {
            //unit f = yuan * 100 + jiao * 10 + fen; unit是什么鬼
            uint f = yuan * 100 + jiao * 10 + fen;  //得到总共多少分
            yuan = f / 100; //先算元
            jiao = (f - yuan * 100) / 10;   //再算角
            fen = f % 10;   //最后算分 
        }

        /// <summary>
        /// 运算符重载(加法)
        /// </summary>
        /// <param name="r1"></param>
        /// <param name="r2"></param>
        /// <returns></returns>
        public static RMB operator +(RMB r1, RMB r2)
        {
            RMB temp = new RMB();
            temp.yuan = r1.yuan + r2.yuan;
            temp.jiao = r1.jiao + r2.jiao;
            temp.fen = r1.fen + r2.fen;
            temp.StandRMB();    //需要调用标准化函数,因为在元角分的相加过程中,可能会超出标准,例如5角+7角,或是9分+6分
            return temp;
        }

        /// <summary>
        /// 运算符重载(减法)
        /// </summary>
        /// <param name="r1"></param>
        /// <param name="r2"></param>
        /// <returns></returns>
        public static RMB operator -(RMB r1, RMB r2)
        {
            RMB temp = new RMB();
            //temp.yuan = r1.yuan - r2.yuan;
            //temp.jiao = r1.jiao - r2.jiao;
            //temp.fen = r1.fen - r2.fen;

            //减法是不能直接减的,因为会出现负数
            //先全部转换为分
            uint fen1 = r1.yuan * 100 + r1.jiao * 10 + r1.fen;
            uint fen2 = r2.yuan * 100 + r2.jiao * 10 + r2.fen;
            //再进行相减
            if (fen1 < fen2)
            {
                temp.flag = false;   //表示负数
                temp.fen = fen2 - fen1;  //如果是r1比r2小,就用r2 - r1
            }
            else
            {
                temp.fen = fen1 - fen2; //否则就r1 - r2;
            }
            temp.StandRMB();    //还是需要调用标准化函数;
            return temp;
        }

        public static RMB operator ++(RMB r)
        {
            uint f;
            //f = r.yuan * 100 + r.jiao * 10 + r.Fen;   不要有的用字段,有的用属性
            f = r.yuan * 100 + r.jiao * 10 + r.fen;
            f++;
            return new RMB(f);
        }

        public static RMB operator --(RMB r)
        {
            uint f;
            f = r.yuan * 100 + r.jiao * 10 + r.fen;
            //f--;
            //return new RMB(f);
            bool b = true;
            if (f == 0)  //判断自减以后是否为负,这里本因直接判断是否小于0,但由于整个类都是用的无符号整数,所以不会小于0,就判断是否等于0,实际上这题应该不需要考虑负数的
            {
                //由于f为uint,所以不会小于0,但我们知道0减1等于负1,所以用正1和负号表示
                f++;
                b = false;
            }
            else
            {
                f--;
            }
            RMB temp = new RMB(f);
            temp.flag = b;
            return temp;
        }

        public static bool operator ==(RMB r1, RMB r2)
        {
            //return (r1.yuan == r2.yuan) && (r1.jiao == r2.jiao) == (r1.fen == r2.fen);
            //最好还是转换为分进行比较,0元10角 与 1元0角 实际上是相等的
            uint fen1 = r1.yuan * 100 + r1.jiao * 10 + r1.fen;
            uint fen2 = r2.yuan * 100 + r2.jiao * 10 + r2.fen;
            return fen1 == fen2;
        }

        public static bool operator !=(RMB r1, RMB r2)
        {
            //return (r1.yuan != r2.yuan) || (r1.jiao != r2.jiao) || (r1.fen != r2.fen);
            uint fen1 = r1.yuan * 100 + r1.jiao * 10 + r1.fen;
            uint fen2 = r2.yuan * 100 + r2.jiao * 10 + r2.fen;
            return fen1 != fen2;
        }

        public override string ToString()
        {
            StandRMB();
            return string.Format((this.flag == false? "":"") +  "{0}元{1}角{2}分", yuan, jiao, fen);
        }

    }
}


[此贴子已经被作者于2016-10-19 22:19编辑过]

2016-10-19 22:15
仓乃梦
Rank: 1
来 自:江苏
等 级:新手上路
帖 子:20
专家分:0
注 册:2016-10-3
收藏
得分:0 
回复 2楼 yhlvht
版主的解释很完整,对我启发很大,语言刚入门,很多东西用的还不熟悉,也有很多不懂的,不过版主就我编的代码给予老师一样的批改还有注释,超赞的
我会再接再厉的,谢谢版主

立志成为版主一样的人。。。
2016-10-20 14:23
快速回复:运算符重载这块,我好像做错了,该怎么改??
数据加载中...
 
   



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

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