C#实现简易的计算器

这篇文章主要为大家详细介绍了C#实现简易的计算器,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#实现简易的计算器的具体代码,供大家参考,具体内容如下

1 题目描述

(1)Form1窗体设计界面如下:

(2)运算类型的下列列表中包括:加法、减法、乘法、除法、取模共5种操作;初始状态下,选择“加法”运算,当用户更改运算类型时,下面式子中的加号“+”应自动更改为相应的运算符;

(3)当用户在前两个文本框中输入时,最后得到结果的文本框始终是空白状态,注意该文本框是只读的,用户不能更改其值;只有当用户单击确定按钮时,结果文本框中才会显示正确的计算结果;

(4)使用过程中,用户修改运算类型时,三个文本框的内容自动清空;

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 Csharp7_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                label3.Text = "+";
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked == true)
            {
                label3.Text = "-";
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked == true)
            {
                label3.Text = "*";
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked == true)
            {
                label3.Text = "÷";
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
            }
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton5.Checked == true)
            {
                label3.Text = "%";
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked == true)
            {
                textBox3.Text = Convert.ToString((int.Parse(textBox1.Text)) + (int.Parse(textBox2.Text)));
            }
            if (radioButton2.Checked == true)
            {
                textBox3.Text = Convert.ToString((int.Parse(textBox1.Text)) - (int.Parse(textBox2.Text)));
            }
            if (radioButton3.Checked == true)
            {
                textBox3.Text = Convert.ToString((int.Parse(textBox1.Text)) * (int.Parse(textBox2.Text)));
            }
            if (radioButton4.Checked == true)
            {
                textBox3.Text = Convert.ToString((double.Parse(textBox1.Text)) / (double.Parse(textBox2.Text)));
            }
            if (radioButton5.Checked == true)
            {
                textBox3.Text = Convert.ToString((int.Parse(textBox1.Text)) % (int.Parse(textBox2.Text)));
            }
        }
    }
}

3 实现效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持得得之家。

本文标题为:C#实现简易的计算器

基础教程推荐