16.C#编程学习—— 简易计算器
源码
usingSystem;
usingSystem.Drawing;
usingSystem.Windows.Forms;
classHexCalc : Form
{
Button btnResult;
ulong ulNum = 0;
ulong ulFirstNum = 0;
bool bNewNumber = true;
char chOperation = '=';
publicstaticvoid Main()
{
Application.Run(newHexCalc());
}
public HexCalc()
{
Text = "Hex Calc";
//Icon = new Icon(GetType(), "HexCalc.HexCalc.ico");
FormBorderStyle = FormBorderStyle.FixedSingle;
MaximizeBox = false;
newCalcButton(this, "D", 'D', 8, 24, 14, 14);
newCalcButton(this, "A", 'A', 8, 40, 14, 14);
newCalcButton(this, "7", '7', 8, 56, 14, 14);
newCalcButton(this, "4", '4', 8, 72, 14, 14);
newCalcButton(this, "1", '1', 8, 88, 14, 14);
newCalcButton(this, "0", '0', 8, 104, 14, 14);
newCalcButton(this, "E", 'E', 26, 24, 14, 14);
newCalcButton(this, "B", 'B', 26, 40, 14, 14);
newCalcButton(this, "8", '8', 26, 56, 14, 14);
newCalcButton(this, "5", '5', 26, 72, 14, 14);
newCalcButton(this, "2", '2', 26, 88, 14, 14);
newCalcButton(this, "Back", '\x08', 26, 104, 32, 14);
newCalcButton(this, "C", 'C', 44, 40, 14, 14);
newCalcButton(this, "F", 'F', 44, 24, 14, 14);
newCalcButton(this, "9", '9', 44, 56, 14, 14);
newCalcButton(this, "6", '6', 44, 72, 14, 14);
newCalcButton(this, "3", '3', 44, 88, 14, 14);
newCalcButton(this, "+", '+', 62, 24, 14, 14);
newCalcButton(this, "-", '-', 62, 40, 14, 14);
newCalcButton(this, "*", '*', 62, 56, 14, 14);
newCalcButton(this, "/", '/', 62, 72, 14, 14);
newCalcButton(this, "%", '%', 62, 88, 14, 14);
newCalcButton(this, "Equals", '=', 62, 104, 32, 14);
newCalcButton(this, "&&", '&',80, 24, 14, 14);
newCalcButton(this, "|", '|', 80, 40, 14, 14);
newCalcButton(this, "^", '^', 80, 56, 14, 14);
newCalcButton(this, "<", '<', 80, 72, 14, 14);
newCalcButton(this, ">", '>', 80, 88, 14, 14);
btnResult =
newCalcButton(this, "0", '\x1B', 8, 4, 86, 14);
foreach (Button btn in Controls)
btn.Click += newEventHandler(ButtonOnClick);
ClientSize = newSize(102, 126);
Scale(Font.Height / 8f);
}
protectedoverridevoid OnKeyPress(KeyPressEventArgs kpea)
{
char chKey = Char.ToUpper(kpea.KeyChar);
if (chKey == '\x0D')
chKey = '=';
for (int i = 0; i <Controls.Count; i++)
{
CalcButton btn = (CalcButton)Controls[i];
if (chKey == btn.chKey)
{
InvokeOnClick(btn, EventArgs.Empty);
break;
}
}
}
void ButtonOnClick(objectobj, EventArgsea)
{
CalcButton btn = (CalcButton)obj;
if (btn.chKey == '\x08')
ulNum /= 16;
elseif (btn.chKey == '\x1B')
ulNum = 0;
elseif (Char.IsLetterOrDigit(btn.chKey)) // Hex digit
{
if (bNewNumber)
{
ulFirstNum = ulNum;
ulNum = 0;
bNewNumber = false;
}
if (ulNum <= ulong.MaxValue>> 4)
ulNum = 16 * ulNum +
(ulong)(btn.chKey -
(Char.IsDigit(btn.chKey) ? '0' : 'A' - 10));
}
else // Operation
{
if (!bNewNumber)
{
switch (chOperation)
{
case'=': ulNum = ulNum; break;
case'+': ulNum = ulFirstNum +ulNum; break;
case'-': ulNum = ulFirstNum -ulNum; break;
case'*': ulNum = ulFirstNum *ulNum; break;
case'&':ulNum = ulFirstNum & ulNum; break;
case'|': ulNum = ulFirstNum |ulNum; break;
case'^': ulNum = ulFirstNum ^ulNum; break;
case'<': ulNum = ulFirstNum<< (int)ulNum; break;
case'>': ulNum = ulFirstNum>> (int)ulNum; break;
case'/': ulNum = ulNum != 0 ?
ulFirstNum / ulNum : ulong.MaxValue;
break;
case'%': ulNum = ulNum != 0 ?
ulFirstNum % ulNum : ulong.MaxValue;
break;
default: ulNum = 0; break;
}
}
bNewNumber = true;
chOperation = btn.chKey;
}
btnResult.Text = String.Format("{0:X}", ulNum);
}
}
classCalcButton : Button
{
publicchar chKey;
public CalcButton(Controlparent, string str, char chkey,
int x, int y, int cx, int cy)
{
Parent = parent;
Text = str;
chKey = chkey;
Location = newPoint(x, y);
Size = newSize(cx, cy);
SetStyle(ControlStyles.Selectable, false);
}
}