加法计算器

一:上图,代码简单,思路挺好

二:代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace calculator
{
    public partial class Frm_main : Form
    {
        public Frm_main()
        {
            InitializeComponent();
        }

        private void btn_0_Click(object sender, EventArgs e)
        {
            clickNum("0");
           
        }

        private void btn_1_Click(object sender, EventArgs e)
        {
            clickNum("1");
            
        }
        private void btn_2_Click(object sender, EventArgs e)
        {
            clickNum("2");
             
        }
        private void btn_3_Click(object sender, EventArgs e)
        {
            clickNum("3");
            
        }

        private void btn_4_Click(object sender, EventArgs e)
        {
            clickNum("4");
           
        }

        private void btn_5_Click(object sender, EventArgs e)
        {
            clickNum("5");
            
        }

        private void btn_6_Click(object sender, EventArgs e)
        {
            clickNum("6");
            
        }

        private void btn_7_Click(object sender, EventArgs e)
        {
            clickNum("7");
           
        }

        private void btn_8_Click(object sender, EventArgs e)
        {
            clickNum("8");
         
        }

        private void btn_9_Click(object sender, EventArgs e)
        {
            clickNum("9");
           
        }
        private List<double> valueList = new List<double>();
        private bool addState = false;//加号点击状态
        private bool addOne = false;//防止加号多次打开
        private bool equalState = false;//等号点击状态

        /// <summary>
        /// 计算机点击数字
        /// </summary>
        /// <param name="num">点击的数字的值</param>
        private void clickNum(string num)
        {
            if (txt_value.Text.Length < 9)//长度要小于9
            {
                if (addState)//打开加号开关
                {
                    txt_value.Clear();//清空屏幕
                    addState = false;//关闭加号开关
                }
                    txt_value.Text += num;//将数字叠加显示到屏幕
                    addOne = false;//没有按下一次加号
                    
            }
        }
        /// <summary>
        /// 事件:按下加号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_add_Click(object sender, EventArgs e)
        {

                if (addOne == false)//没有按下一次加号
                {
                   
                    valueList.Add(double.Parse(txt_value.Text.Trim()));//将数字存到泛型数组里
                    addOne = true;//按下一次加号
                }

                addState = true;//打开加号开关
                equalState = true;
        }
        /// <summary>
        /// 事件:按下等号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_result_Click(object sender, EventArgs e)
        {
            if (addOne==false)
            {
                if (equalState == true)
                {
                    valueList.Add(double.Parse(txt_value.Text.Trim()));

                    equalState = false;
                    GetValue();
                    lbl_num.Text = GetString();
                    equalState = false;
                }
            }
        }

        /// <summary>
        /// 方法用于计算累加数值并输出
        /// </summary>
        void GetValue()
        {
            double P_dbl_temp = 0;//定义局部变量
            foreach (double d in valueList)//遍历集合
            {
                P_dbl_temp += d;//计算累加结果
            }
            txt_value.Text = P_dbl_temp.ToString();//显示累加结果
        }
        /// <summary>
        /// 方法用于得到数值的字符串表示
        /// </summary>
        /// <returns>返回字符串数值</returns>
        string GetString()
        {
            string P_str_temp = string.Empty;//定义局部变量
            for (int i = 0; i < valueList.Count; i++)//遍历集合
            {
                if (i != 0)//判断是否是第一个数值
                {
                    P_str_temp += //产生字符串
                        "+" + valueList[i].ToString();
                }
                else
                {
                    P_str_temp = //产生字符串
                        valueList[i].ToString();
                }
            }
            return P_str_temp;//返回字符串
        }

        private void btn_clean_Click(object sender, EventArgs e)
        {
            valueList.Clear();//清空集合中数值
            lbl_num.Text = null;//清空计算表达式
            txt_value.Clear();//清空累加结果
        }
       
    }
}

 

posted @ 2014-05-14 19:19  HongMaJu  阅读(506)  评论(0编辑  收藏  举报