这篇文章主要为大家详细介绍了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#实现简易的计算器


基础教程推荐
猜你喜欢
- c# – USING块在网站与Windows窗体中的行为不同 2023-09-20
- C# 解析XML和反序列化的示例 2023-04-14
- C#中的Linq to JSON操作详解 2023-06-08
- 实例详解C#实现http不同方法的请求 2022-12-26
- C#调用摄像头实现拍照功能的示例代码 2023-03-09
- C#通过标签软件Bartender的ZPL命令打印条码 2023-05-16
- Unity 如何获取鼠标停留位置下的物体 2023-04-10
- C#中 Json 序列化去掉null值的方法 2022-11-18
- C#获取指定目录下某种格式文件集并备份到指定文件夹 2023-05-30
- Unity shader实现高斯模糊效果 2023-01-16