C#實現(xiàn)簡易的計算器
本文實例為大家分享了C#實現(xiàn)簡易的計算器的具體代碼,供大家參考,具體內(nèi)容如下
1 題目描述
(1)Form1窗體設計界面如下:

(2)運算類型的下列列表中包括:加法、減法、乘法、除法、取模共5種操作;初始狀態(tài)下,選擇“加法”運算,當用戶更改運算類型時,下面式子中的加號“+”應自動更改為相應的運算符;
(3)當用戶在前兩個文本框中輸入時,最后得到結果的文本框始終是空白狀態(tài),注意該文本框是只讀的,用戶不能更改其值;只有當用戶單擊確定按鈕時,結果文本框中才會顯示正確的計算結果;
(4)使用過程中,用戶修改運算類型時,三個文本框的內(nèi)容自動清空;
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 實現(xiàn)效果





以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Unity UGUI的RectMask2D遮罩組件的介紹使用
這篇文章主要為大家介紹了Unity UGUI的RectMask2D遮罩組件的介紹使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07
C#使用IComparer自定義List類實現(xiàn)排序的方法
這篇文章主要介紹了C#使用IComparer自定義List類實現(xiàn)排序的方法,涉及C#使用IComparer接口定義List類進行排序的相關技巧,需要的朋友可以參考下2015-08-08

