亲宝软件园·资讯

展开

C#计算器 C#实现简易的计算器

敲代码两年半的练习生 人气:0
想了解C#实现简易的计算器的相关内容吗,敲代码两年半的练习生在本文为您仔细讲解C#计算器的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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 实现效果

加载全部内容

相关教程
猜你喜欢
用户评论