使用C#创建一个计算器

本文介绍了一个使用C#编写的简易计算器程序。该程序通过简单的界面实现基本的数学运算功能,包括加减乘除等操作,并附带了完整的源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

         本文是当初涉猎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.youkuaiyun.com/download/lingyunxianhe/9963006

      


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值