|
using System;
using System.Globalization;
namespace MVCDemo
{
public delegate void RegisterChangeDelegate(object sender, EventArgs args);
public class RpnModel : ModelBase
{
// 4 register stack
protected string[] registerStack={"0.00", "0.00", "0.00", "0.00"};
protected bool newEntryState;
protected NumberFormatInfo formatProvider;
public bool NewEntryState
{
get {return newEntryState;}
set
{
newEntryState=value;
eventPool.FireEvent("EntryStateChanged");
}
}
public string X
{
get {return registerStack[0];}
set
{
registerStack[0]=value;
eventPool.FireEvent("X");
}
}
public string Y
{
get {return registerStack[1];}
set
{
registerStack[1]=value;
eventPool.FireEvent("Y");
}
}
public string R3
{
get {return registerStack[2];}
set
{
registerStack[2]=value;
eventPool.FireEvent("R3");
}
}
public string R4
{
get {return registerStack[3];}
set
{
registerStack[3]=value;
eventPool.FireEvent("R4");
}
}
public double DX
{
get {return Convert.ToDouble(X);}
set {X=value.ToString("N", formatProvider);}
}
public double DY
{
get {return Convert.ToDouble(Y);}
}
public RpnModel()
{
formatProvider=new NumberFormatInfo();
formatProvider.NumberDecimalDigits=2;
newEntryState=true;
eventPool.Publish("X", "Y", "R3", "R4", "EntryStateChanged");
}
public void RollUp()
{
string temp=R4;
R4=R3;
R3=Y;
Y=X;
X=temp;
}
public void RollDown()
{
string temp=X;
X=Y;
Y=R3;
R3=R4;
R4=temp;
}
public void Push()
{
R4=R3;
R3=Y;
Y=X;
}
public void PopY()
{
Y=R3;
R3=R4;
R4="0.00";
}
public void Exchange()
{
string temp=X;
X=Y;
Y=temp;
}
public void ForceUpdate()
{
eventPool.FireAll();
}
public void FormatX()
{
X=DX.ToString("N", formatProvider);
}
public void AddPoint()
{
AddCharToX(formatProvider.NumberDecimalSeparator[0]);
}
public void AddCharToX(char c)
{
if (newEntryState)
{
X=c.ToString();
}
else
{
X+=c;
}
}
public void RemoveCharFromX()
{
if (X.Length==1)
{
DX=0;
NewEntryState=true;
}
else
{
X=X.Substring(0, X.Length-1);
}
}
public void ChangeSign()
{
if (DX != 0.00)
{
if (X[0] != '-')
{
X="-"+X;
}
else
{
X=X.Substring(1);
}
}
}
public IMemento GetState()
{
IMemento memento=new RpnModelMemento();
memento.SetState(registerStack);
return memento;
}
public void SetState(IMemento memento)
{
((string[])memento.GetState()).CopyTo(registerStack, 0);
eventPool.FireAll();
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.