C#简易的四则计算器。

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

namespace Object
{
    public partial class Form2 : Form
    {
        //接收的第一个数据
        string s1 = "0";
        //接受的第二个数据
        string s2 = "0";
        //保存前一次的操作符
        char o1 = ' ';
        //多次运算的第二个操作符
        char o2 = ' ';
        //数字集字符串
        string sNumber="0,1,2,3,4,5,6,7,8,9";
        //用于把s1与s2操作的结果集赋予s1
        double result;
        //判断是否重新输入
        bool jude = true;

        /// <summary>
        /// 窗体初始化
        /// </summary>
        public Form2()
        {
            InitializeComponent();
            txt.Text = "0";      
        }

        /// <summary>
        /// 按钮方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Click(object sender, EventArgs e)
        {
            caculate(char.Parse(((Button)sender).Text));
        }
        
        /// <summary>
        /// 数字与操作符的操作方法
        /// </summary>
        /// <param name="a"></param>
        private void caculate(char a)
        {
            if (sNumber.Contains(a.ToString()))
            {
                
                    if (o1 == ' ')
                    {
                        if (jude == false) { s1 = ""; jude = true; }
                        if (s1 == "0") { s1 = ""; }
                        s1 += a;
                        txt.Text = s1;
                    }
                    else
                    {
                        if (s2 == "0") { s2 = ""; }
                        s2 += a;
                        txt.Text = s2;
                    }
            }
            else if (a=='=')
            {
                GetSign(o1);
                o1 = ' ';
                jude = false;
            }
            else if (a == '.')
            {
               
                if(!txt.Text.Contains("."))
                {
                    jude = true;
                    if (o1 == ' ')
                    {
                            txt.Text = s1 += '.';      
                    }
                    else
                    {
                            txt.Text = s2 += '.';    
                    }
                }
            }
            else
            {
                jude = true;
                if (o1 != ' ')
                {
                    o2 = a;
                    GetSign(o1);
                    o1 = o2;
                }
                else
                {
                    o1 = a;
                }
            }
        }

        /// <summary>
        /// 运算方法
        /// </summary>
        /// <param name="t"></param>
        private void GetSign(char t)
        {
            if (s2 != "0")
            {
                switch (t)
                {
                    case '+': result = double.Parse(s1) + double.Parse(s2); break;
                    case '-': result = double.Parse(s1) - double.Parse(s2); break;
                    case '*': result = double.Parse(s1) * double.Parse(s2); break;
                    case '/': if (double.Parse(s2) != 0) { result = double.Parse(s1) / double.Parse(s2); } else { txt.Text = "被除数不能为零"; rtuZero(); return; } break;
                    case '%': if (!s1.Contains(".") && !s2.Contains(".")) { result = double.Parse(s1) % double.Parse(s2); } else { txt.Text = "求余符号两边必须为整数"; rtuZero(); return; } break;
                }
                txt.Text = result.ToString();
                s1 = result.ToString();
                s2 = "0";
            }
            else 
            { }
        }
        
        /// <summary>
        /// 归零方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnC_Click(object sender, EventArgs e)
        {
            s1 = "0";
            s2 = "0";
            o1 = ' ';
            o2 = ' ';
            txt.Text = "0";
            txt.ReadOnly = false;
        }
        
        /// <summary>
        /// 退格符方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Backbtn_Click(object sender, EventArgs e)
        {
            if (txt.Text.Length >= 2)
            {
                txt.Text = txt.Text.Remove(txt.Text.Length - 1, 1);
                if (o1 == ' ')
                {
                    if (s1.Length!=1)
                    {
                        s1 = s1.Remove(s1.Length - 1, 1);
                    }
                }
                else
                {
                    if (s2.Length != 1)
                    {
                        s2 = s2.Remove(s1.Length - 1, 1);
                    }
                }
            }
        }
        
        /// <summary>
        /// 调整错误情况下的变量,以便下一次的运算
        /// </summary>
        private void rtuZero()
        {
            txt.ReadOnly = true;
            o2 = ' ';
        }
        
    }
}

 很简单的一段代码。。正在实习中,无聊做的。供初学者参考。。。。

posted @ 2012-05-14 14:13  Json_Yh  阅读(400)  评论(0编辑  收藏  举报