|
// Copyright (c) 2004 Marc Clifton. All Rights Reserved
using System;
using System.Globalization;
using System.Web.UI.WebControls;
using MyXaml;
namespace declarativeWeb
{
public class SimpleCalc
{
private bool cleared;
private string lastOp;
private string lastValue;
private NumberFormatInfo formatProvider;
[MyXamlAutoInitialize] private TextBox display=null;
public SimpleCalc()
{
cleared=true;
lastOp=String.Empty;
lastValue=String.Empty;
formatProvider=new NumberFormatInfo();
formatProvider.NumberDecimalDigits=2;
}
public void OnClear(object sender, EventArgs e)
{
display.Text="0.00";
cleared=true;
lastOp=String.Empty;
lastValue=String.Empty;
}
public void OnClearEntry(object sender, EventArgs e)
{
display.Text="0.00";
cleared=true;
}
public void OnDigit(object sender, EventArgs e)
{
Button btn=(Button)sender;
if (cleared)
{
display.Text=btn.Text;
cleared=false;
}
else
{
display.Text=display.Text+btn.Text;
}
}
public string ProcessLastOp(string val)
{
double d=0;
switch(lastOp)
{
case "+":
{
d=Convert.ToDouble(lastValue) + Convert.ToDouble(val);
break;
}
case "-":
{
d=Convert.ToDouble(lastValue) - Convert.ToDouble(val);
break;
}
case "*":
{
d=Convert.ToDouble(lastValue) * Convert.ToDouble(val);
break;
}
case "/":
{
if (Convert.ToDouble(val) != 0.0)
{
d=Convert.ToDouble(lastValue) / Convert.ToDouble(val);
}
break;
}
}
lastValue=d.ToString("N", formatProvider);
return lastValue;
}
public void OnAdd(object sender, EventArgs e)
{
if (lastValue==String.Empty)
{
lastValue=display.Text;
}
else
{
lastValue=ProcessLastOp(display.Text);
display.Text=lastValue;
}
lastOp="+";
cleared=true;
}
public void OnSubtract(object sender, EventArgs e)
{
if (lastValue==String.Empty)
{
lastValue=display.Text;
}
else
{
lastValue=ProcessLastOp(display.Text);
display.Text=lastValue;
}
lastOp="-";
cleared=true;
}
public void OnMultiply(object sender, EventArgs e)
{
if (lastValue==String.Empty)
{
lastValue=display.Text;
}
else
{
lastValue=ProcessLastOp(display.Text);
display.Text=lastValue;
}
lastOp="*";
cleared=true;
}
public void OnDivide(object sender, EventArgs e)
{
if (lastValue==String.Empty)
{
lastValue=display.Text;
}
else
{
lastValue=ProcessLastOp(display.Text);
display.Text=lastValue;
}
lastOp="/";
cleared=true;
}
public void OnEqual(object sender, EventArgs e)
{
if (lastValue != String.Empty)
{
display.Text=ProcessLastOp(display.Text);
lastValue=String.Empty;
lastOp=String.Empty;
cleared=true;
}
}
}
}
|
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.
My main goal as a developer is to improve the way software is designed, and how it interacts with the user. I like designing software best, but I also like coding and documentation. I especially like to work with user interfaces and graphics.
I have extensive knowledge of the .NET Framework, and like to delve into its internals. I specialize in working with VG.net and MyXaml. I also like to work with ASP.NET, AJAX, and DHTML.