简单计算器 (C#)

历时一个晚上,终于搞定了第一个C#应用程序。虽然做的很简陋,但是毕竟第一次,心情还是蛮激动的。

做完之后又学习了一下如何做安装包,因为觉得做成安装包更酷一点。但是做完之后发现安装包需要一个

安装向导才能完成安装,我想可能安装包有的地方做的不够到位吧,等以后在回来改进。

由于制作比较简单,这里就只附上程序代码啦:

using System;
using System.Windows.Forms;

namespace calculate1
{
    public partial class Form1 : Form
    {
        private double _num1 = 0, _num2 = 0, _result = 0;
        private string _operator = "";
        private int _operatorCount = 0;
        private bool _firstNumberFlag = false;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_firstNumberFlag == true)
            {
                textBox1.Text = "2";
                _firstNumberFlag = false;
            }
            else
                textBox1.Text = textBox1.Text + "2";
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonNum0_Click(object sender, EventArgs e)
        {
            if (_firstNumberFlag == true)
            {
                textBox1.Text = "0";
                _firstNumberFlag = false;
            }
            else
                textBox1.Text = textBox1.Text + "0";
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void buttonNum3_Click(object sender, EventArgs e)
        {
            if (_firstNumberFlag == true)
            {
                textBox1.Text = "3";
                _firstNumberFlag = false;
            }
            else
                textBox1.Text = textBox1.Text + "3";
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonNum4_Click(object sender, EventArgs e)
        {
            if (_firstNumberFlag == true)
            {
                textBox1.Text = "4";
                _firstNumberFlag = false;
            }
            else
                textBox1.Text = textBox1.Text + "4";
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonNum5_Click(object sender, EventArgs e)
        {
            if (_firstNumberFlag == true)
            {
                textBox1.Text = "5";
                _firstNumberFlag = false;
            }
            else
                textBox1.Text = textBox1.Text + "5";
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonNum6_Click(object sender, EventArgs e)
        {
            if (_firstNumberFlag == true)
            {
                textBox1.Text = "6";
                _firstNumberFlag = false;
            }
            else
                textBox1.Text = textBox1.Text + "6";
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonNum7_Click(object sender, EventArgs e)
        {
            if (_firstNumberFlag == true)
            {
                textBox1.Text = "7";
                _firstNumberFlag = false;
            }
            else
                textBox1.Text = textBox1.Text + "7";
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonNum8_Click(object sender, EventArgs e)
        {
            if (_firstNumberFlag == true)
            {
                textBox1.Text = "8";
                _firstNumberFlag = false;
            }
            else
                textBox1.Text = textBox1.Text + "8";
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonNum9_Click(object sender, EventArgs e)
        {
            if (_firstNumberFlag == true)
            {
                textBox1.Text = "9";
                _firstNumberFlag = false;
            }
            else
                textBox1.Text = textBox1.Text + "9";
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonSubtract_Click(object sender, EventArgs e)
        {
            _firstNumberFlag = true;
            _operatorCount = _operatorCount + 1;
            if (_operatorCount == 1)
            {
                _num1 = Convert.ToDouble(textBox1.Text);
                _operator = "-";
                textBox1.Text = "";
            }
            else
            {
                _num2 = Convert.ToDouble(textBox1.Text);
                if (_operator == "+")
                    _num1 = _num1 + _num2;
                else if (_operator == "-")
                    _num1 = _num1 - _num2;
                else if (_operator == "*")
                    _num1 = _num1 * _num2;
                else
                    _num1 = _num1 / _num2;
                _operator = "-";
                textBox1.Text = _num1.ToString();
            }
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonMultiply_Click(object sender, EventArgs e)
        {
            _firstNumberFlag = true;
            _operatorCount = _operatorCount + 1;
            if (_operatorCount == 1)
            {
                _num1 = Convert.ToDouble(textBox1.Text);
                _operator = "*";
                textBox1.Text = "";
            }
            else
            {
                _num2 = Convert.ToDouble(textBox1.Text);
                if (_operator == "+")
                    _num1 = _num1 + _num2;
                else if (_operator == "-")
                    _num1 = _num1 - _num2;
                else if (_operator == "*")
                    _num1 = _num1 * _num2;
                else
                    _num1 = _num1 / _num2;
                _operator = "*";
                textBox1.Text = _num1.ToString();
            }
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonDivide_Click(object sender, EventArgs e)
        {
            _firstNumberFlag = true;
            _operatorCount = _operatorCount + 1;
            if (_operatorCount == 1)
            {
                _num1 = Convert.ToDouble(textBox1.Text);
                _operator = "/";
                textBox1.Text = "";
            }
            else
            {
                _num2 = Convert.ToDouble(textBox1.Text);
                if (_operator == "+")
                    _num1 = _num1 + _num2;
                else if (_operator == "-")
                    _num1 = _num1 - _num2;
                else if (_operator == "*")
                    _num1 = _num1 * _num2;
                else
                    _num1 = _num1 / _num2;
                _operator = "/";
                textBox1.Text = _num1.ToString();
            }
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonEqual_Click(object sender, EventArgs e)
        {
            _num2 = Convert.ToDouble(textBox1.Text);
            if (_operator == "+")
                _result = _num1 + _num2;
            else if (_operator == "-")
                _result = _num1 - _num2;
            else if (_operator == "*")
                _result = _num1 * _num2;
            else
                _result = _num1 / _num2;
            textBox1.Text = _result.ToString();
            _operatorCount = 0;
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonDot_Click(object sender, EventArgs e)
        {
            if(_firstNumberFlag==true)
            {
                textBox1.Text = ".";
                _firstNumberFlag = false;
            }
            else
            {
                if (textBox1.Text.IndexOf('.') == -1)
                    textBox1.Text = textBox1.Text + ".";
            }
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonBackSpace_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            _num1 = 0;_num2 = 0;_result = 0;
            _operator = "";
            _operatorCount = 0;
            _firstNumberFlag = false;
            textBox1.Text = "";
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonSign_Click(object sender, EventArgs e)
        {
            if(textBox1.Text.Length>=1)
            {
                string firstChar = textBox1.Text.Substring(0, 1);
                if (firstChar == "-")
                    textBox1.Text = "+" + textBox1.Text.Substring(1, textBox1.Text.Length - 1);
                else if (firstChar == "+")
                    textBox1.Text = "-" + textBox1.Text.Substring(1, textBox1.Text.Length - 1);
                else
                    textBox1.Text = "-" + textBox1.Text;
            }
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '0')
                buttonNum0_Click(sender, e);
            else if (e.KeyChar == '1')
                buttonNum1_Click(sender, e);
            else if (e.KeyChar == '2')
                button1_Click(sender, e);
            else if (e.KeyChar == '3')
                buttonNum3_Click(sender, e);
            else if (e.KeyChar == '4')
                buttonNum4_Click(sender, e);
            else if (e.KeyChar == '5')
                buttonNum5_Click(sender, e);
            else if (e.KeyChar == '6')
                buttonNum6_Click(sender, e);
            else if (e.KeyChar == '7')
                buttonNum7_Click(sender, e);
            else if (e.KeyChar == '8')
                buttonNum8_Click(sender, e);
            else if (e.KeyChar == '9')
                buttonNum9_Click(sender, e);
            else if (e.KeyChar == '+')
                buttonAdd_Click(sender, e);
            else if (e.KeyChar == '-')
                buttonSubtract_Click(sender, e);
            else if (e.KeyChar == '*')
                buttonMultiply_Click(sender, e);
            else if (e.KeyChar == '/')
                buttonDivide_Click(sender, e);
            else if (e.KeyChar == '=')
                buttonEqual_Click(sender, e);
            else if (e.KeyChar == 'c' || e.KeyChar == 'C')
                buttonClear_Click(sender, e);
            else if (e.KeyChar == '.')
                buttonDot_Click(sender, e);
            else if (e.KeyChar == 8)
                buttonBackSpace_Click(sender, e);
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void buttonAdd_Click(object sender, EventArgs e)

        {
            _firstNumberFlag =true;
            _operatorCount = _operatorCount + 1;
            if(_operatorCount==1)
            {
                _num1 = Convert.ToDouble(textBox1.Text);
                _operator = "+";
                textBox1.Text = "";
            }
            else
            {
                _num2 = Convert.ToDouble(textBox1.Text);
                if (_operator == "+")
                    _num1 = _num1 + _num2;
                else if (_operator == "-")
                    _num1 = _num1 - _num2;
                else if (_operator == "*")
                    _num1 = _num1 * _num2;
                else
                    _num1 = _num1 / _num2;
                _operator = "+";
                textBox1.Text = _num1.ToString();
            }
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }

        private void buttonNum1_Click(object sender, EventArgs e)
        {
            if (_firstNumberFlag == true)
            {
                textBox1.Text = "1";
                _firstNumberFlag = false;
            }
            else
                textBox1.Text = textBox1.Text + "1";
            textBox1.Focus();
            textBox1.Select(textBox1.Text.Length, 0);
        }
    }
}


一、实验题目:计算器实验 二、实验目的: 1)熟悉.NET的编程方法,掌握项目的创建与使用及安装; 2)熟悉C#编程语言,字符串使用、自定义类、自定义类库等; 3)熟悉Windows应用程序类编程、事件驱动编程; 三、实验内容: 1)参照Windows提供的计算器工具,设计一个与之类似的程序;另外可参考运行效果下载(计算器Demo程序); 2)基本要求:实现“普通运算”功能,支持连续运算,如连续输入“2+3×5”后点击“等号”按钮进行运算其结果为25. 3)附加要求: A,支持“科学运算”功能,即输入表达式时遵循运算符号的优先原则,连续输入“2+3×5”后点击“等号”按钮进行运算其结果为17; B,支持数字键盘(小键盘)输入功能(焦点不在文本框内时也支持键盘输入); 四、实验方法 经过用户的输入得到数学表达式后,可选择以下方法计算结果: 方法1)不定义"计算类",根据用户输入在程序中直接计算出结果。 方法2)在项目中设计一个计算类(CalculateClass),其中包含“普通计算”和“科学计算”功能,即输入数学运算表达式字符串,返回计算结果。直接在项目中调用该类,输出计算结果。 方法3)在解决方案中增加一个类库(ClassLibrary)项目(在bin\debug下生成dll文件),其中包含计算类(CalculateClass),该类中包含两个方法(普通计算和科学计算),可以根据表达式计算结果。然后在计算器项目中“引用”该类库中的类,输出计算结果; 点评:方法1代码重用性差;方法2代码重用性好,在项目内可方便使用;方法3代码重用性较好,能在不同的项目内重用。 五、实验说明 1)认真分析总结每个“按钮”点击之后的动作(对应的代码); 默认的控件是Button按钮,其实可以用其他支持Click事件的控件替代也行,比如图片控件PictureBox; 2)代码的共享:如数字按钮可以共享一个事件代码; // 参考代码 private void button2_Click(object sender, EventArgs e) { Button objbutton = (Button)sender; label1.Text = "你按了第" + (sender as Button).Tag.ToString() + "个按钮"; } 3)考虑操作性或实用性,如支持退格键、异常输入处理等; 4)为突出实验重点,可以不考虑数制转换、复合运算、括号运算等功能; 5)支持数字小键盘输入时,留意理解窗口的KeyPreview属性,即是否窗口接收键盘事件。另外实现时会涉及到“焦点隐藏”问题,以及按钮的键盘事件等。 敲键盘时发生的事件当然是键盘事件,关键是“窗体”还是“控件”接收键盘事件。如果窗体的KeyPreview设为true,则敲键盘时发生的事件被窗体接收;否则就是控件接收键盘事件。 所谓“焦点隐藏” 是指界面上没有焦点,例如系统提供的计算器。方法一:界面上的控件不获取焦点或者没有焦点(如标签);方法二:将焦点藏在界面上不可见的控件上。 支持数字键盘输入的情形:当焦点在按钮1上时,按数字键2,其使用效果应相当于点击了按钮2。 6)键盘事件KeyPress和KeyDown中e参数包括键盘码; private void KeyBoardForm_KeyPress(object sender, KeyPressEventArgs e) { //KeyPress 不能识别组合键 label1.Text = "你按的键是:" + e.KeyChar; } 7)“麻雀虽小,五脏俱全”,程序很容易出Bug,请认真调试; 8)要想达到界面有特点,不亚于在功能上下功夫。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值