以下是我用c#寫的一個(gè)圖形化的計(jì)算器,這是關(guān)鍵代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
namespace Calculator
{
public partial class CalCulator : Form
{
private enum OptrNum //枚舉類型定義,方便比較運(yùn)算符的大小
{
LessThan, //小于
Equal, //等于
GreaterThan, //大于
Error //錯(cuò)誤
};
private string temp_textBoxView; //數(shù)據(jù)存儲(chǔ)區(qū)
private CalCulatorStack OptrStack; //運(yùn)算符棧
private CalCulatorStack NumStack; //運(yùn)算數(shù)棧
private ArrayList temp_List; //分離數(shù)據(jù)臨時(shí)變量存儲(chǔ)區(qū)
private string StrOptr = "+-*/()#"; //計(jì)算支持的運(yùn)算符
private int[,] OptrReation = new int[7, 7]{ //存儲(chǔ)操作數(shù)關(guān)系
{1,1,-1,-1,-1,1,1}, // +
{1,1,-1,-1,-1,1,1}, // -
{1,1,1,1,-1,1,1}, // *
{1,1,1,1,-1,1,1}, // /
{-1,-1,-1,-1,-1,0,2}, // (
{1,1,1,1,2,1,1}, // )
{-1,-1,-1,-1,-1,2,0}}; // #
public CalCulator()
{
this.StartPosition = FormStartPosition.CenterScreen;
OptrStack = new CalCulatorStack();
NumStack = new CalCulatorStack();
temp_List = new ArrayList();
InitializeComponent();
temp_textBoxView = string.Empty;
}
/// <summary>
/// ButtonOne點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonOneClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "1";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// ButtonTwo點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonTwoClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "2";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// ButtonThree點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonThreeClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "3";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// ButtonFour點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonFourClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "4";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// ButtonFive點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonFiveClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "5";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// ButtonSix點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSixClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "6";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// ButtonSeven點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonSevenClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "7";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// ButtonEight點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonEightClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "8";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// ButtonNine點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonNineClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "9";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// ButtonZero點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ButtonZeroClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "0";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// PriorBracketButton點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void PriorBracketButtonClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "(";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// NextBracketButton點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void NextBracketButtonClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += ")";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// 加號(hào)點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AddButtonClick(object sender,EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "+";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// 減號(hào)點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SubButtonClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "-";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// 乘號(hào)點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void MulButtonClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "*";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// 除號(hào)點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DivButtonClick(object sender, EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView += "/";
textBoxView.Text = temp_textBoxView;
}
/// <summary>
/// 清除點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ClrButtonClick(object sender,EventArgs e)
{
textBoxView.Text = string.Empty;
temp_textBoxView = string.Empty;
}
/// <summary>
/// 退格點(diǎn)擊事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DelButtonClick(object sender,EventArgs e)
{
if (temp_textBoxView == string.Empty)
return;
else
{
string temp;
temp = temp_textBoxView.Substring(0, temp_textBoxView.Length - 1);
temp_textBoxView = temp;
textBoxView.Text = temp_textBoxView;
}
}
/// <summary>
/// 求值事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void EuqButtonClick(object sender, EventArgs e)
{
if (temp_textBoxView == string.Empty)
return;
else
{
InitTempList();
OptrStack.Push(temp_List[0]);
temp_List.RemoveAt(0);
object temp;
temp = temp_List[0];
temp_List.RemoveAt(0);
while (Convert.ToChar(Convert.ToInt32(temp)) != '#' || Convert.ToChar(OptrStack.GetTop()) != '#')
{
if (IsOptr(Convert.ToChar(Convert.ToInt32(temp))) == false)
{
NumStack.Push(Convert.ToDouble(temp));
temp = temp_List[0];
temp_List.RemoveAt(0);
}
else
{
switch (Precede(Convert.ToChar(OptrStack.GetTop()), Convert.ToChar(temp)))
{
case OptrNum.LessThan:
OptrStack.Push(Convert.ToChar(temp));
temp = temp_List[0];
temp_List.RemoveAt(0);
break;
case OptrNum.Equal:
OptrStack.Pop();
temp = temp_List[0];
temp_List.RemoveAt(0);
break;
case OptrNum.GreaterThan:
char temp_optr;
double a, b,c;
temp_optr = Convert.ToChar(OptrStack.Pop());
b = Convert.ToDouble(NumStack.Pop());
a = Convert.ToDouble((NumStack.Pop()));
c = Operate(a, temp_optr, b);
NumStack.Push(c);
break;
case OptrNum.Error:
MessageBox.Show(this, "運(yùn)算錯(cuò)誤,請(qǐng)檢查輸入是否正確!");
break;
}
}
}
textBoxView.Text = Convert.ToString(NumStack.Pop());
temp_textBoxView = string.Empty;
temp_List.Clear();
OptrStack.Clear();
NumStack.Clear();
}
}
/// <summary>
/// 加載事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmLoad(object sender, EventArgs e)
{
buttonOne.Click += new EventHandler(ButtonOneClick);
buttonTwo.Click += new EventHandler(ButtonTwoClick);
buttonThree.Click += new EventHandler(ButtonThreeClick);
buttonFour.Click += new EventHandler(ButtonFourClick);
buttonFive.Click += new EventHandler(ButtonFiveClick);
buttonSix.Click += new EventHandler(ButtonSixClick);
buttonSeven.Click += new EventHandler(ButtonSevenClick);
buttonEight.Click += new EventHandler(ButtonEightClick);
buttonNine.Click += new EventHandler(ButtonNineClick);
buttonZero.Click += new EventHandler(ButtonZeroClick);
PriorBracketButton.Click += new EventHandler(PriorBracketButtonClick);
NextBracketButton.Click += new EventHandler(NextBracketButtonClick);
AddButton.Click += new EventHandler(AddButtonClick);
SubButton.Click += new EventHandler(SubButtonClick);
MulButton.Click += new EventHandler(MulButtonClick);
DivButton.Click += new EventHandler(DivButtonClick);
ClrButton.Click += new EventHandler(ClrButtonClick);
DelButton.Click += new EventHandler(DelButtonClick);
EuqButton.Click += new EventHandler(EuqButtonClick);
}
/// <summary>
/// 分離數(shù)據(jù)存儲(chǔ)區(qū)的運(yùn)算數(shù)與運(yùn)算符,
/// 并將其插入到temp_list等待下一步計(jì)算
/// </summary>
private void InitTempList()
{
temp_textBoxView += "#";
char[] Temp = temp_textBoxView.ToCharArray();
double Num = 0, COUNT = 10;
int i;
temp_List.Add('#');
for (i = 0; i < Temp.Length; i++)
{
if (IsOptr(Temp[i]) == true)
{
if (Num != 0)
{
temp_List.Add(Num);
Num = 0;
}
temp_List.Add(Temp[i]);
}
else
{
Num = ConvertToInt32(Temp[i]) + Num * COUNT;
}
}
}
/// <summary>
/// 判斷元素是否為操作符
/// </summary>
/// <param name="temp"></param>
/// <returns></returns>
private bool IsOptr(char temp)
{
if (temp == '+' || temp == '-' || temp == '*' || temp == '/' ||
temp == '(' || temp == ')'||temp=='#')
return true;
else
return false;
}
/// <summary>
/// 比較兩個(gè)運(yùn)算符的大小
/// </summary>
/// <param name="prior_Optr"></param>
/// <param name="next_Optr"></param>
/// <returns></returns>
private OptrNum Precede(char prior_Optr,char next_Optr)
{
OptrNum optr_num=OptrNum.Error;
int i = StrOptr.IndexOf(prior_Optr);
int j = StrOptr.IndexOf(next_Optr);
if (OptrReation[i, j] == 1)
optr_num = OptrNum.GreaterThan;
if (OptrReation[i, j] == -1)
optr_num = OptrNum.LessThan;
if (OptrReation[i, j] == 0)
optr_num = OptrNum.Equal;
if (OptrReation[i, j] == 2)
optr_num = OptrNum.Error;
return optr_num;
}
/// <summary>
/// 運(yùn)算函數(shù)
/// </summary>
/// <param name="a"></param>
/// <param name="Optr"></param>
/// <param name="b"></param>
private double Operate(double a,char Optr,double b)
{
if (Optr == '+')
return a + b;
if (Optr == '-')
return a - b;
if (Optr == '*')
return a * b;
if (Optr == '/')
return a / b;
else
return 0;
}
/// <summary>
/// 將字符轉(zhuǎn)換為數(shù)字
/// </summary>
/// <param name="temp"></param>
/// <returns></returns>
private int ConvertToInt32(char temp)
{
if (temp == '0')
return 0;
if (temp == '1')
return 1;
if (temp == '2')
return 2;
if (temp == '3')
return 3;
if (temp == '4')
return 4;
if (temp == '5')
return 5;
if (temp == '6')
return 6;
if (temp == '7')
return 7;
if (temp == '8')
return 8;
if (temp == '9')
return 9;
else
return 0;
}
}
}另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前題目:C#實(shí)現(xiàn)計(jì)算器-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://www.chinadenli.net/article12/ccsidc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站策劃、云服務(wù)器、小程序開發(fā)、軟件開發(fā)、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容