当前位置:编程学习 > C#/ASP.NET >>

c#新手上路请教 计算器问题 跪拜指点。。

我是刚学c# 没几天 想写个 简单计算器 
 就是 +  -  *  /  4个键 

首先按完数字 
 string c;  //定义一个c来接收 textbox1上面的数值 

 private void button1_Click(object sender, EventArgs e) //键1
        {
            textBox1.Text = c + button1.Text;
            c += button1.Text;
        }

        private void button2_Click(object sender, EventArgs e) //键2
        {
            textBox1.Text = c + button2.Text;
            c += button2.Text;
        }

简单的写了只是简单 1 和2  2个键
我按完之后存储到了 c里面了  
接下来按完+号  要存储我的+号后面的字符了 怎么来完成呢?不懂请大侠帮忙 --------------------编程问答-------------------- +后面的还需要存储么? 

public void  btn_plus()
{
     textbox1.text=""; 
}
private void  resule()
{
      double num;
     num=convert.todouble(textbox1.text);
    string resule=convert.tostring(num+c);
   textbox1.text=result;
}
这样不就行了?? --------------------编程问答--------------------  /// <summary>
    /// 操作
    /// </summary>
    public enum Opration
    {
        /// <summary>
        /// 加
        /// </summary>
        Plus,
        /// <summary>
        /// 减
        /// </summary>
        Min,
        /// <summary>
        /// 乘
        /// </summary>
        Mul,
        /// <summary>
        /// 除
        /// </summary>
        Div
    }

    /// <summary>
    /// 当前状态
    /// </summary>
    public enum CalState
    {
        /// <summary>
        /// 计算中
        /// </summary>
        Caling,
        /// <summary>
        /// 非计算中
        /// </summary>
        None
    }

    public partial class Form1 : Form
    {
        #region 构造函数
        public Form1()
        {
            InitializeComponent();
        }
        #endregion

        #region 字段

        private double value1;
        private double value2;
        private Opration opration;
        private CalState calState;
        private bool isClear;

        #endregion

        #region 定义方法

        /// <summary>
        /// 执行计算
        /// </summary>
        private void Cal()
        {
            double val = 0;
            switch (opration)
            { 
                case  Opration.Plus:
                    val = value1 + value2;
                    break;
                case  Opration.Min:
                    val = value1 - value2;
                    break;
                case  Opration.Mul:
                    val = value1 * value2;
                    break;
                case  Opration.Div:
                    val = value1 / value2;
                    break;
            }
            this.txtNumber.Text = val.ToString();
            //将运算后的结果赋值给第一个数以便连续操作
            value1 = val;
        }

        #endregion

        private void btnOne_Click(object sender, EventArgs e)
        {
            if (isClear)
            {
                this.txtNumber.Text = "";
                isClear = false;
            }
            Button b = (Button)sender;
            string valStr = this.txtNumber.Text + b.Text;
            this.txtNumber.Text = valStr;
            double val = 0;
            if (!double.TryParse(valStr, out val))
                return;
            if (calState == CalState.Caling)
                value2 = val;
            else
                value1 = val;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //状态为计算中
            if (calState == CalState.Caling)
            {
                Cal();
            }
            calState = CalState.Caling;
            isClear = true;
            string op=((Button)sender).Text;
            switch (op)
            { 
                case "+":
                    opration = Opration.Plus;
                    break;
                case "-":
                    opration = Opration.Min;
                    break;
                case "*":
                    opration = Opration.Mul;
                    break;
                case "/":
                    opration = Opration.Div;
                    break;
                case "=":
                    calState = CalState.None;
                    break;
            }
            
        }

    } --------------------编程问答-------------------- 帮顶一下了!!!我也是计算器 --------------------编程问答-------------------- 逆波兰式算法 可以了解一下!

http://blog.csdn.net/cheng110110/article/details/6529777

补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,