AQUAさんとこ発で盛り上がっているのでのっかってみました。
http://blogs.wankuma.com/aqua/archive/2007/06/25/82152.aspx
http://blogs.wankuma.com/aqua/archive/2007/06/27/82456.aspx
画面はべたにこんな感じ。
計算は余り使われなさそうなilでいってみました。短いのでソース全部張ります。
namespace WankumaCalculator
{
public partial class Form1 : Form
{
#region MyRegion
enum CalcType
{
None,
Add,
Sub,
Multiply,
Division,
}
private CalcType calcType = CalcType.None;
private DynamicMethod calcUnit;
private ILGenerator il;
private string inputString = "";
private void InitDynamicMethod()
{
calcUnit = new DynamicMethod("Calculate", typeof(int), null, typeof(Form1));
calcUnit.InitLocals = true;
this.il = calcUnit.GetILGenerator();
}
#endregion
public Form1()
{
InitializeComponent();
resultArea.Text = "0";
inputString = "";
calcType = CalcType.None;
InitDynamicMethod();
}
private void buttonNums_Click(object sender, EventArgs e)
{
resultArea.Text = int.Parse(inputString += (sender as Button).Text).ToString();
}
private void buttonClear_Click(object sender, EventArgs e)
{
resultArea.Text = inputString = "0";
calcType = CalcType.None;
InitDynamicMethod();
}
private void buttoCalculate_Click(object sender, EventArgs e)
{
il.Emit(OpCodes.Ldc_I4, int.Parse(inputString));
if (calcType != CalcType.None)
{
switch (calcType)
{
case CalcType.Add:
il.Emit(OpCodes.Add);
break;
case CalcType.Sub:
il.Emit(OpCodes.Sub);
break;
case CalcType.Multiply:
il.Emit(OpCodes.Mul);
break;
case CalcType.Division:
il.Emit(OpCodes.Div_Un);
break;
}
il.Emit(OpCodes.Ret);
resultArea.Text = calcUnit.Invoke(null, null).ToString();
InitDynamicMethod();
il.Emit(OpCodes.Ldc_I4, int.Parse(resultArea.Text));
}
switch ((sender as Button).Text)
{
case "+":
inputString = "";
calcType = CalcType.Add;
break;
case "ー":
inputString = "";
calcType = CalcType.Sub;
break;
case "×":
inputString = "";
calcType = CalcType.Multiply;
break;
case "÷":
inputString = "";
calcType = CalcType.Division;
break;
case "=":
break;
}
}
}
}