結果顯示及相關備注:
代碼:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace Calculator
{
public partial class CalcForm : Form
{
private string strOutput = "";
private double lNumFormer = 0;//前一個操作數(shù)
private double lNumTemp = 0;//臨時變量
private double lResult = 0;//結果
private char cOperator;//操作符
private bool bDotClicked = false;//用于標記小數(shù)點是否被按下
//private uint decimalIndex = 0;//用于標記當前小數(shù)點后精度位數(shù)欢际,如3.14的decimalIndex==2
double lastDecimalNum = 1;//最近一次點擊的小數(shù)精度,即小數(shù)最后一位精度糊识,如0.01
public CalcForm()
{
InitializeComponent();
//數(shù)字按鈕0~9
EventHandler eh = new EventHandler(Numbers_Click);
button_num_0.Click += eh;
button_num_1.Click += eh;
button_num_2.Click += eh;
button_num_3.Click += eh;
button_num_4.Click += eh;
button_num_5.Click += eh;
button_num_6.Click += eh;
button_num_7.Click += eh;
button_num_8.Click += eh;
button_num_9.Click += eh;
//運算符+惯豆、-迫靖、*、/
EventHandler eh2 = new EventHandler(Operators_Click);
button_add.Click += eh2;
button_sub.Click += eh2;
button_mul.Click += eh2;
button_div.Click += eh2;
}
private void CalcForm_Load(object sender, EventArgs e)
{
}
//****數(shù)字按鈕0-9
private void button_num_0_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Numbers_Click()函數(shù)
}
private void button_num_1_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Numbers_Click()函數(shù)
}
private void button_num_2_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Numbers_Click()函數(shù)
}
private void button_num_3_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Numbers_Click()函數(shù)
}
private void button_num_4_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Numbers_Click()函數(shù)
}
private void button_num_5_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Numbers_Click()函數(shù)
}
private void button_num_6_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Numbers_Click()函數(shù)
}
private void button_num_7_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Numbers_Click()函數(shù)
}
private void button_num_8_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Numbers_Click()函數(shù)
}
private void button_num_9_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Numbers_Click()函數(shù)
}
//****操作符+、-背桐、*、/
private void button_add_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Operators_Click()函數(shù)
}
private void button_sub_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Operators_Click()函數(shù)
}
private void button_mul_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Operators_Click()函數(shù)
}
private void button_div_Click(object sender, EventArgs e)
{
//功能實現(xiàn)代碼位于Operators_Click()函數(shù)
}
//****等于號=
private void button_enter_Click(object sender, EventArgs e)
{
try
{
switch (cOperator)
{
case '+':
//計算并檢測數(shù)據(jù)是否越界
checked { lResult = lNumFormer + lNumTemp; }
break;
case '-':
checked { lResult = lNumFormer - lNumTemp; }
break;
case '*':
checked { lResult = lNumFormer * lNumTemp; };
break;
case '/':
checked { lResult = lNumFormer / lNumTemp; };
break;
}
}
catch
{
MessageBox.Show("運算結果溢出");
}
txtOutput.Text = lResult.ToString();
//清空變量
lNumFormer = 0;//前一個操作數(shù)
lNumTemp = 0;//臨時變量
lResult = 0;//結果
cOperator = '\0';//操作符
strOutput = "";
bDotClicked = false;
lastDecimalNum = 1;
}
//****清空鍵CE
private void button_ce_Click(object sender, EventArgs e)
{
//清空變量
lNumFormer = 0;//前一個操作數(shù)
lNumTemp = 0;//臨時變量
lResult = 0;//結果
cOperator = '\0';//操作符
strOutput = "";
bDotClicked = false;
lastDecimalNum = 1;
//清空頁面顯示
txtOutput.Text = "";
}
//****小數(shù)點.
private void button_dot_Click(object sender, EventArgs e)
{
if (!bDotClicked)
{
strOutput += ".";
txtOutput.Text = strOutput;
bDotClicked = true;
}
}
//集中處理按鈕點擊事件
//****數(shù)字按鈕0-9
private void Numbers_Click(object sender, EventArgs e)
{
string strClickNum = ((Button)sender).Text;
try
{
if (bDotClicked)
{
//得出點擊的小數(shù)數(shù)值
//decimalIndex++;
lastDecimalNum *= 0.1;
checked { lNumTemp = lNumTemp + long.Parse(strClickNum) * lastDecimalNum; }
}
else
{
checked { lNumTemp = lNumTemp * 10 + long.Parse(strClickNum); }
}
}
catch
{
//貌似double型并不會輕易溢出
MessageBox.Show("數(shù)據(jù)溢出");
}
txtOutput.Text = lNumTemp.ToString();
strOutput += strClickNum;
//strOutput = lNumTemp.ToString();
txtOutput.Text = strOutput;
}
//集中處理按鈕點擊事件
//****操作符+蝉揍、-链峭、*、/
private void Operators_Click(object sender, EventArgs e)
{
string strClickOp = ((Button)sender).Text;
cOperator = strClickOp[0];//strClickOp長度為1
lNumFormer = lNumTemp;
lNumTemp = 0;
strOutput += cOperator.ToString();
txtOutput.Text = strOutput;
bDotClicked = false;
lastDecimalNum = 1;
}
//鍵盤按鍵
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// if it is a hotkey, return true; otherwise, return false
switch (keyData)
{
case Keys.NumPad0:
button_num_0.Focus();
button_num_0.PerformClick();
return true;
case Keys.NumPad1:
button_num_1.Focus();
button_num_1.PerformClick();
return true;
case Keys.NumPad2:
button_num_2.Focus();
button_num_2.PerformClick();
return true;
case Keys.NumPad3:
button_num_3.Focus();
button_num_3.PerformClick();
return true;
case Keys.NumPad4:
button_num_4.Focus();
button_num_4.PerformClick();
return true;
case Keys.NumPad5:
button_num_5.Focus();
button_num_5.PerformClick();
return true;
case Keys.NumPad6:
button_num_6.Focus();
button_num_6.PerformClick();
return true;
case Keys.NumPad7:
button_num_7.Focus();
button_num_7.PerformClick();
return true;
case Keys.NumPad8:
button_num_8.Focus();
button_num_8.PerformClick();
return true;
case Keys.NumPad9:
button_num_9.Focus();
button_num_9.PerformClick();
return true;
case Keys.Add:
button_add.Focus();
button_add.PerformClick();
return true;
case Keys.Subtract:
button_sub.Focus();
button_sub.PerformClick();
return true;
case Keys.Multiply:
button_mul.Focus();
button_mul.PerformClick();
return true;
case Keys.Divide:
button_div.Focus();
button_div.PerformClick();
return true;
case Keys.Enter:
button_enter.Focus();
button_enter.PerformClick();
return true;
case Keys.Escape:
button_ce.Focus();
button_ce.PerformClick();
return true;
case Keys.Decimal:
button_dot.Focus();
button_dot.PerformClick();
return true;
default:
break;
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void txtOutput_TextChanged(object sender, EventArgs e)
{
}
}
}