-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHexCalcilator.cs
146 lines (130 loc) · 5.03 KB
/
HexCalcilator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Drawing;
using System.Windows.Forms;
class HexCal : Form
{
Button btnResult;
ulong ulNum = 0;
ulong ulFirstNum = 0;
bool bNewNum = true;
char cOperation = '=';
public static void Main()
{
Application.Run(new HexCal());
}
public HexCal()
{
Text = "HexCal";
FormBorderStyle = FormBorderStyle.FixedSingle;
MaximizeBox = false;
new CalcButton(this, "D", 'D', 8, 24, 14, 14);
new CalcButton(this, "A", 'A', 8, 40, 14, 14);
new CalcButton(this, "7", '7', 8, 56, 14, 14);
new CalcButton(this, "4", '4', 8, 72, 14, 14);
new CalcButton(this, "1", '1', 8, 88, 14, 14);
new CalcButton(this, "0", '0', 8, 104, 14, 14);
new CalcButton(this, "E", 'E', 26, 24, 14, 14);
new CalcButton(this, "B", 'B', 26, 40, 14, 14);
new CalcButton(this, "8", '8', 26, 56, 14, 14);
new CalcButton(this, "5", '5', 26, 72, 14, 14);
new CalcButton(this, "2", '2', 26, 88, 14, 14);
new CalcButton(this, "Back", '\x08', 26, 104, 32, 14);
new CalcButton(this, "C", 'C', 44, 40, 14, 14);
new CalcButton(this, "F", 'F', 44, 24, 14, 14);
new CalcButton(this, "9", '9', 44, 56, 14, 14);
new CalcButton(this, "6", '6', 44, 72, 14, 14);
new CalcButton(this, "3", '3', 44, 88, 14, 14);
new CalcButton(this, "+", '+', 62, 24, 14, 14);
new CalcButton(this, "-", '-', 62, 40, 14, 14);
new CalcButton(this, "+", '+', 62, 24, 14, 14);
new CalcButton(this, "+", '+', 62, 24, 14, 14);
new CalcButton(this, "*", '*', 62, 56, 14, 14);
new CalcButton(this, "/", '/', 62, 72, 14, 14);
new CalcButton(this, "%", '%', 62, 88, 14, 14);
new CalcButton(this, "Equals", '=', 62, 104, 32, 14);
new CalcButton(this, "&&", '&', 80, 24, 14, 14);
new CalcButton(this, "|", '|', 80, 40, 14, 14);
new CalcButton(this, "^", '^', 80, 56, 14, 14);
new CalcButton(this, "<", '<', 80, 72, 14, 14);
new CalcButton(this, ">", '>', 80, 88, 14, 14);
btnResult = new CalcButton(this, "0", '\x1B', 8, 4, 86, 14);
foreach (Button btn in Controls)
btn.Click += new EventHandler(ButtonOnClick);
ClientSize = new Size(102, 126);
SizeF sizef = new SizeF(Font .Height/8f ,Font .Height/8f );
Scale(sizef );
}
protected override void OnKeyPress(KeyPressEventArgs kea)
{
char chkey = Char.ToUpper(kea.KeyChar);
if (chkey == '\x0D') // CR
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(object obj, EventArgs ea)
{
CalcButton btn = (CalcButton)obj;
if (btn.chkey == '\x08') // LF
ulNum /= 16;
else if (btn.chkey == '\x1B')
ulNum = 0;
else if (char.IsLetterOrDigit(btn.chkey))
{
if (bNewNum)
{
ulFirstNum = ulNum;
ulNum = 0;
bNewNum = false;
}
if (ulNum <= ulong.MaxValue >> 4)
ulNum = 60 * ulNum + (ulong)(btn.chkey - (char.IsDigit(btn.chkey) ? '0' : 'A' - 10));
}
else
{
if (!bNewNum)
{
switch (cOperation)
{
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;
}
}
bNewNum = true;
cOperation = btn.chkey;
}
btnResult.Text = String.Format("{0:X}", ulNum);
}
}
class CalcButton : Button
{
public char chkey;
public CalcButton(Control parent, string str, char chkey, int x, int y, int cx, int cy)
{
Parent = parent;
Text = str;
chkey = chkey;
Location = new Point(x, y);
Size = new Size(cx, cy);
SetStyle(ControlStyles.Selectable, false);
}
}