《简单计算器实现(c#)》


 新开博客,早期刚学c#的简单计算器的实现,大一的时候,刚刚接触编程语言,老师就让我们写个计算器,当时用c写了个控制台的,现在找不到了,只找到这个c#写的(写的有些潦草呵,望指正!),分享给那写刚开始学习语言的同胞(大神是不用的了,呵呵!)。

界面:


using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
usingSystem.Windows.Forms;
 
namespace 计?算?器¡Â
{
    public partial class Form1 : Form
    {
        double a, b,c;
        int flag=0;           //设置运算符
        int flag2 = 0;       //判断是否有内容第二个文本框
        int flag1 = 0;       //判断是否有内容第一个文本框
        public Form1()
        {
           InitializeComponent();
        }
 
        private voidbutton1_Click(object sender, EventArgs e)
        {
            if (flag2 == 0)
            {
               textBox2.AppendText("1");
            }
            else
            {
               textBox1.Clear();
               textBox2.Clear();
               textBox2.AppendText("1");
 
            }
           flag2 = 0;
        }
     
        private voidbutton2_Click(object sender, EventArgs e)
        {
            if (flag2 == 0)
            {
               textBox2.AppendText("2");
            }
            else
            {
                textBox1.Clear();
               textBox2.Clear();
               textBox2.AppendText("2");
 
            }
           flag2 = 0;
        }
 
        private voidbutton3_Click(object sender, EventArgs e)
        {
            if (flag2 == 0)
            {
               textBox2.AppendText("3");
            }
            else
            {
               textBox1.Clear();
               textBox2.Clear();
               textBox2.AppendText("3");
 
            }
           flag2 = 0;
        }
 
        private voidbutton4_Click(object sender, EventArgs e)
        {
            if (flag2 == 0)
            {
               textBox2.AppendText("4");
            }
            else
            {
               textBox1.Clear();
               textBox2.Clear();
               textBox2.AppendText("4");
 
            }
           flag2 = 0;
        }
 
        private voidbutton5_Click(object sender, EventArgs e)
        {
            if (flag2 == 0)
            {
               textBox2.AppendText("5");
            }
            else
            {
               textBox1.Clear();
               textBox2.Clear();
               textBox2.AppendText("5");
 
            }
           flag2 = 0;
        }
 
        private voidbutton6_Click(object sender, EventArgs e)
        {
            if (flag2 == 0)
            {
               textBox2.AppendText("6");
            }
            else
            {
               textBox1.Clear();
               textBox2.Clear();
               textBox2.AppendText("6");
 
            }
           flag2 = 0;
        }
 
        private voidbutton7_Click(object sender, EventArgs e)
        {
            if (flag2 == 0)
            {
               textBox2.AppendText("7");
            }
            else
            {
               textBox1.Clear();
               textBox2.Clear();
               textBox2.AppendText("7");
 
            }
           flag2 = 0;
        }
 
        private voidbutton8_Click(object sender, EventArgs e)
        {
            if (flag2 == 0)
            {
               textBox2.AppendText("8");
            }
            else
            {
               textBox1.Clear();
               textBox2.Clear();
               textBox2.AppendText("8");
 
            }
           flag2 = 0;
        }
 
        private voidbutton9_Click(object sender, EventArgs e)
        {
            if (flag2 == 0)
            {
               textBox2.AppendText("9");
            }
            else
            {
               textBox1.Clear();
               textBox2.Clear();
               textBox2.AppendText("9");
 
            }
           flag2 = 0;
        }
 
        private voidbutton0_Click(object sender, EventArgs e)
        {
            if (flag2 == 0)
            {
               textBox2.AppendText("0");
            }
            else
            {
               textBox1.Clear();
               textBox2.Clear();
               textBox2.AppendText("0");
 
            }
           flag2 = 0;
        }
 
        private void buttonDot_Click(object sender, EventArgs e)  //小数点
        {
            if (flag2 == 0)
            {
               textBox2.AppendText(".");
            }
            else
            {
               textBox1.Clear();
               textBox2.Clear();
               textBox2.AppendText(".");
 
            }
           flag2 = 0;            
        }
 
        private void buttonReset_Click(object sender, EventArgs e)  //归零
        {
           textBox1.Clear();
           textBox2.Clear();
           flag = 0;
           flag1 = 0;
           flag2 = 1;
        }
 
        private void button10_Click(object sender, EventArgs e)//加
        {
           flag = 1;
            if (flag1 == 0)
            {
 
                textBox1.Text = textBox2.Text;
               textBox2.Clear();
            }
            else
            {
               this.x();
               textBox1.Text = textBox2.Text;
               textBox2.Clear();
            }
           flag1 = 1;
           flag2 = 0;
        }
 
        private voidbutton11_Click(object sender, EventArgs e)  //减
        {
           flag = 2;
            if (flag1 == 0)
            {
 
               textBox1.Text = textBox2.Text;
               textBox2.Clear();
            }
            else
            {
               this.x();
               textBox1.Text = textBox2.Text;
               textBox2.Clear();
            }
           flag1 = 1;
           flag2 = 0;
        }
 
        private voidbutton12_Click(object sender, EventArgs e)  //乘
        {
           flag = 3;
            if (flag1 == 0)
            {
 
               textBox1.Text = textBox2.Text;
               textBox2.Clear();
            }
            else
            {
               this.x();
               textBox1.Text = textBox2.Text;
               textBox2.Clear();
            }
           flag1 = 1;
           flag2 = 0;
        }
 
        private voidbutton13_Click(object sender, EventArgs e)   //除
        {
           flag = 4;
            if (flag1 == 0)
            {
 
               textBox1.Text = textBox2.Text;
               textBox2.Clear();
            }
            else
            {
               this.x();
               textBox1.Text = textBox2.Text;
               textBox2.Clear();
            }
           flag1 = 1;
           flag2 = 0;
        }
 
        private voidbutton16_Click(object sender, EventArgs e)
        {
            this.x();
        }
        private void x()
        {
            try
            {
               a = Convert.ToDouble(textBox1.Text);
               b = Convert.ToDouble(textBox2.Text);
               switch (flag)
               {
                   case 1:
                        c = a + b;textBox2.Text = Convert.ToString(c); break;
                   case 2:
                        c = a - b;textBox2.Text = Convert.ToString(c); break;
                   case 3:
                        c = a * b;textBox2.Text = Convert.ToString(c); break;
                   case 4:
                        c = a / b;textBox2.Text = Convert.ToString(c); break;
                   case 5:
                        c = a % b;textBox2.Text= Convert.ToString(c); break;
               }
               flag1 = 0;
               flag2 = 1;
            }
            catch
            {
               MessageBox.Show("请?输º?入¨?正y确¨¡¤的Ì?数ºy!ê?");
            }
        }
 
        private voidbutton18_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
 
        private voidbutton21_Click(object sender, EventArgs e)
        {
            try
            {
               textBox2.Text = textBox2.Text.Substring(0, textBox2.Text.Length - 1);
            }
            catch
            {
            }
        }
 
        private voidbutton19_Click(object sender, EventArgs e)
        {
           flag = 5;
            if (flag1 == 0)
            {
 
               textBox1.Text = textBox2.Text;
               textBox2.Clear();
            }
            else
            {
               this.x();
               textBox1.Text = textBox2.Text;
               textBox2.Clear();
            }
           flag1 = 1;
           flag2 = 0;
        }
    }
}


 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值