c#编写简易计算器

题目:编写一个简易计算器

程序最后的结果如图所示:

步骤:

首先创建一个windows form窗体程序,Compute

拖动一个textbox控件和若干button控件,在窗体上布置成如上布局

1.然后,打开代码界面,首先编写一个当点击数字按钮时进行的程序,将点击的数组显示在textbox中;

2.然后在每一个数组按钮的click事件中(即双击数字按钮即可编写相应按钮的click事件),调用如上方法;

3.在+-*/按钮的click事件中(即双击数字按钮即可编写相应按钮的click事件),,编写代码,获取第一个操作数,同时将操作符也显示在textbox中;

=按钮的click事件中编写计算结果的函数;获取第二个操作数用到了stringindexof方法,获得op的位置,从而截取字符串获得第二个操作数;用到了switch case语句

总的代码如下:

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 ClassForm
{
    public partial class Compute : Form
    {
        public Compute()
        {
            InitializeComponent();
        }
        public double op1;//第一个操作数
        public double op2;//第二个操作数
        public double result;//计算结果
        char op;//运算符
        //当点击数值按钮输入数值时调用此方法,将输入的数字显示在文本框中
        public void OpButton(int num)
        {
            if (textBox1.Text == "0")
            {
                textBox1.Text = num + "";
            }
            else
            {
                textBox1.Text += num;
            }
        }

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

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

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

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

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

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

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

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

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

        private void button10_Click(object sender, EventArgs e)
        {
            OpButton(0);
        }

        private void button11_Click(object sender, EventArgs e)
        {
            op1 = 0;
            op2 = 0;
            result = 0;
            textBox1.Text = "0";
            op = ' ';
        }

        private void button13_Click(object sender, EventArgs e)
        {
            textBox1.Text += ".";
        }

        private void button14_Click(object sender, EventArgs e)
        {
            op = '+';
            op1 = Convert.ToDouble(textBox1.Text);
            textBox1.Text+="+";
        }

        private void button15_Click(object sender, EventArgs e)
        {
            op = '-';
            op1 = Convert.ToDouble(textBox1.Text);
            textBox1.Text+="-";
        }

        private void button16_Click(object sender, EventArgs e)
        {
            op = '*';
            op1 = Convert.ToDouble(textBox1.Text);
            textBox1.Text +="*";
        }

        private void button17_Click(object sender, EventArgs e)
        {
            op = '/';
            op1 = Convert.ToDouble(textBox1.Text);
            textBox1.Text+="/";
        }

        private void button18_Click(object sender, EventArgs e)
        {
            int i=textBox1.Text.IndexOf(op);//获取操作符在texbox中的位置
            op2 = Convert.ToDouble(textBox1.Text.Substring(i+1,textBox1.Text.Length-i-1));
            //第二个操作数是从操作符后面那个开始的
           //长度是总的长度减去第一个数的长度(注意,
           //具体的位置是从0开始的,所以i的值加1才为
           //第二个数的前面的字符串的总长度)
            switch(op)
            {
                case '+':
                    result = op1 + op2;
                    textBox1.Text = result.ToString();
                    break;
                case '-':
                    result = op1 - op2;
                    textBox1.Text = result.ToString();
                    break;
                case  '*':
                    result = op1 * op2;
                    textBox1.Text = result.ToString();
                    break;
                case '/':
                    result = op1 / op2;
                    textBox1.Text = result.ToString();
                    break;
                default:
                    break;

            }
        }
       //窗体的载入事件代码
        private void Compute_Load(object sender, EventArgs e)
        {
            textBox1.Text = "0";
            textBox1.TextAlign = HorizontalAlignment.Right;
        }
    }
}

 

最后,在program.cs写成run(compute)即可;

 

总的工程在我的资源中可下载。

你可能感兴趣的:(c#)