运算符重载这块,我好像做错了,该怎么改??
设计人民币类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 );
}
}
}