关闭

c#简易计算器设计

标签: 源代码c#计算器
322人阅读 评论(0) 收藏 举报
分类:

 

c#简易计算器设计,这是我学习时的练习作品,能够完成简单的运算,但设计还有很多不周的地方,仅供参考。

 

 



 源代码:

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 jisuanqi
{
    public partial class Form1 : Form
    {
        double numA, numB, result;//numA数A,numB数B,result结果
        bool flag;//判断是否为小小数
        string myOperator;//操作类型
        string historytxt;//记录原有的操作数
        /// <summary>
        /// NumberClick类
        /// </summary>
        public void NumberClick(int myNumber)
        {
            if (flag)
            {
                textBox1.Text = textBox1.Text + myNumber;
                historytxt = historytxt + myNumber;
            }
            else
            {
                if (historytxt  == "")
                {
                    //输入的是整数的第一个数字
                    textBox1.Text =textBox1 .Text + myNumber + "";
                    historytxt = myNumber + "";
                }
                else
                {
                    //输入的不是整数的第一个数字
                    textBox1.Text = textBox1.Text.Substring
                        (0, textBox1.Text.Length) + myNumber + "";
                    historytxt = historytxt.Substring(0, historytxt.Length) + myNumber + "";
                }
            }
        }

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            historytxt  = "";
            textBox1.Text = "";
            //指定文本框的对齐方式为右对齐
            textBox1.TextAlign = HorizontalAlignment.Right;
        }

        private void button0_Click(object sender, EventArgs e)
        {
            NumberClick(0);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NumberClick(1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            NumberClick(2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            NumberClick(3);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            NumberClick(4);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            NumberClick(5);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            NumberClick(6);
        }

        private void button7_Click(object sender, EventArgs e)
        {
            NumberClick(7);
        }

        private void button8_Click(object sender, EventArgs e)
        {
            NumberClick(8);
        }

        private void button9_Click(object sender, EventArgs e)
        {
            NumberClick(9);
        }

        private void bDot_Click(object sender, EventArgs e)
        {
            flag = true;//输入小数点
            textBox1.Text = textBox1.Text + ".";
            historytxt = historytxt + ".";
        }

        private void bCle_Click(object sender, EventArgs e)
        {
            numA = 0;
            numB = 0;
            textBox1.Text = "";
            historytxt = "";
            flag = false;
        }

        private void bPlus_Click(object sender, EventArgs e)
        {
            myOperator = "add";
            numA = double.Parse(historytxt);
            historytxt = "";
            textBox1.Text = textBox1.Text + "+";
            flag = false;
        }

        private void bSub_Click(object sender, EventArgs e)
        {
            myOperator = "Sub";
            numA = double.Parse(historytxt);
            textBox1.Text= textBox1.Text + "-";
            historytxt ="";
            flag = false;
        }

        private void bMul_Click(object sender, EventArgs e)
        {
            myOperator = "Mul";
            numA = double.Parse(historytxt);
            textBox1.Text = textBox1.Text + "*";
            historytxt ="";
            flag = false;
        }

        private void bDiv_Click(object sender, EventArgs e)
        {
            myOperator = "Div";
            numA = double.Parse(historytxt);
            textBox1.Text = textBox1.Text + "/";
            historytxt  = "";
            flag = false;
        }

        private void bEqu_Click(object sender, EventArgs e)
        {
            numB = double.Parse(historytxt);
            switch (myOperator)
            {
                case "add":
                    result = numA + numB;
                    textBox1.Text = textBox1.Text  +"="+ result.ToString();
                    break;
                case "Sub":
                    result = numA - numB;
                    textBox1.Text =textBox1.Text  +"="+ result.ToString();
                    break;
                case "Mul":
                    result = numA * numB ;
                    textBox1.Text =textBox1.Text  +"="+ result.ToString();
                    break;
                case "Div":
                    if (numB == 0)
                    {
                        textBox1.Text = "除数不能为零!";
                    }
                    else
                    {
                        result = numA / numB;
                        textBox1.Text =textBox1 .Text  +"="+ result.ToString();
                    }
                    break;
            }
           // numA = result;
            historytxt = result.ToString();
        }
    }
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:525次
    • 积分:17
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档