使用C#创建一个计算器

         本文是当初涉猎C#时写的一个小程序,现在已经没有使用这个语言了。不过想说的是:C#确实我见过的最优雅的语言(也许我见过的语言还少,可能还有比她更优雅的语言吧) 。我曾经做过一点Excel和AutoCAD的简单二次开发,使用C#比C++简洁、高效多了。现在就说下这个计算器的设计吧,我还是直接贴代码吧,简洁。

        1、首先创建一个大窗体,然后放置各个空间和文本框。创建的界面如下:

        

       2、编写控件触发函数

      

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 Calculator
{
    public partial class Form1 : Form
    {
        string Operation;

        public Form1()
        {
            InitializeComponent();
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button7_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text+button7.Text;

        }

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

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

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

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

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

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

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

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            string str;
            str= button1.Text;
            textBox1.Text = textBox1.Text + str;

            Operation = str;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string str;
            str = button2.Text;
            textBox1.Text = textBox1.Text + str;

            Operation = str;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            string str;
            str = button3.Text;
            textBox1.Text = textBox1.Text + str;

            Operation = str;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string str;
            str = button4.Text;
            textBox1.Text = textBox1.Text + str;

            Operation = str;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string str;
            str = button5.Text;
            string tempStr = textBox1.Text;
            textBox1.Text = textBox1.Text + str;
            string signal;
            signal = Operation;
        
            string numStr1,numStr2;
            string[] StrArray = tempStr.Split(char.Parse(signal));
            numStr1 = StrArray[0];
            numStr2 = StrArray[1];
            double num1, num2,result;

            num1 = double.Parse(numStr1);
            num2 = double.Parse(numStr2);
            switch(signal)
            {
                case "+":
               result=num1+num2;
               textBox1.Text = textBox1.Text + result.ToString();
                break;
                case "-":
               result=num1-num2;
               textBox1.Text = textBox1.Text + result.ToString();
                break;
                case "*":
               result=num1*num2;
               textBox1.Text = textBox1.Text + result.ToString();
                break;
                case "/":
               result=num1/num2;
               textBox1.Text = textBox1.Text + result.ToString();
                break;
            }
          }

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

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
       上面的完整工程我放在这个地方了,网址:http://download.csdn.net/download/lingyunxianhe/9963006

      


猜你喜欢

转载自blog.csdn.net/lingyunxianhe/article/details/77834391
今日推荐