这篇文章主要介绍了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#如何实现简易的计算器”这篇文章对大家有帮助,同时也希望大家多多支持编程网,关注编程网行业资讯频道,更多相关知识等着你来学习!