我是靠谱客的博主 大力大神,最近开发中收集的这篇文章主要介绍VS2010(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 Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text += btn.Text;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + " " + btn.Text + " ";

        }

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

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

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

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

        private void button14_Click(object sender, EventArgs e)
        {
            Single r;
            string t = textBox1.Text;
            int space = t.IndexOf(' ');
            string s1=t.Substring (0,space);
            char op=Convert.ToChar (t.Substring (space+1,1));
            string s2=t.Substring (space+3);
            Single arg1=Convert.ToSingle(s1);
            Single arg2=Convert.ToSingle(s2);
            switch(op)
            {
                case '+':
                    r=arg1+arg2;
                    break;
                    case '-':
                    r=arg1-arg2;
                    break;
                    case '*':
                    r=arg1*arg2;
                    break;
                    case '/':
                    if(arg2==0)
                    {throw new ApplicationException();}
                    
                    else
                    {
                        r=arg1/arg2;
                    break;}
                    break;
                default:throw new ApplicationException();


            }
            textBox1.Text=r.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text += btn.Text;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text += btn.Text;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text += btn.Text;
        }

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

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

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

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

        private void button11_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text += btn.Text;
        }
    }
}
界面如图所示:


关键代码:

<strong>      </strong> private void btnAdd_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + " " + btn.Text + " ";

        }

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

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

        private void btnDiv_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            textBox1.Text = textBox1.Text + " " + btn.Text + " ";
        }
以上程序再处理显示时都事先加了“  ”(空格),方面下面的数据分割提取计算。这种思想方法值得借鉴。

int space = t.IndexOf(' ');
            string s1=t.Substring (0,space);
            char op=Convert.ToChar (t.Substring (space+1,1));
            string s2=t.Substring (space+3);
确定不同数字用空格隔开,用Substring()方法获得,本程序的重要思想方法。


最后

以上就是大力大神为你收集整理的VS2010(c#)-实现数字计算器的全部内容,希望文章能够帮你解决VS2010(c#)-实现数字计算器所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部