winform计算器

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 FirstForm
{
    public partial class JiSuanQi : Form
    {
        public JiSuanQi()
        {
            InitializeComponent();
        }

        private double sum=0;
        private string biaodashi;
        private string Preyunsuanfu;//存放之前的运算符
        private bool isok=true;//用来判断是否刚刚点过运算符

        private void btnling_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            if (isok)//是否点击过运算符
            {
                txtshuru.Text = btn.Text;
                //isok = false;
            }
            else
            {
                txtshuru.Text += btn.Text;
            }

        }

        private void btnjia_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string yunsuanfu = btn.Text;
            if (Preyunsuanfu == null)
            {
                biaodashi = txtshuru.Text;//“+”可以去掉
                sum = double.Parse(txtshuru.Text);
            }
            else
            {
                if (!isok)
                {
                    if (Preyunsuanfu == "+")
                    {
                        sum += double.Parse(txtshuru.Text);
                    }
                    if (Preyunsuanfu == "-")
                    {
                        sum -= double.Parse(txtshuru.Text);
                    }
                    if (Preyunsuanfu == "*")
                    {
                        sum *= double.Parse(txtshuru.Text);
                    }
                    if (Preyunsuanfu == "/")
                    {
                        sum /= double.Parse(txtshuru.Text);
                    }
                    if (Preyunsuanfu == "%")
                    {
                        sum %= double.Parse(txtshuru.Text);
                    }

                    biaodashi = biaodashi+ Preyunsuanfu + txtshuru.Text;
                    txtshuru.Text = sum.ToString();
                } 
            }
            txtjisuanshi.Text = biaodashi + yunsuanfu;
            Preyunsuanfu = yunsuanfu;
            isok = true;
            
        }

        private void btnClear_Click(object sender, EventArgs e)
        {

        }

        private void btndeng_Click(object sender, EventArgs e)
        {

        }

    }
}

 

posted @ 2015-07-15 17:24  Yusarin  阅读(313)  评论(0编辑  收藏  举报