16.C#编程学习—— 简易计算器

本文介绍了一个使用C#编写的简易十六进制计算器的实现细节。该计算器支持基本算术运算及位操作,并通过按钮和键盘输入进行交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);

    }

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值