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

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





以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
VS2013創(chuàng)建Windows服務(wù)與調(diào)試服務(wù)的圖文方法
這篇文章主要介紹了VS2013創(chuàng)建Windows服務(wù)與調(diào)試服務(wù)的圖文方法,需要的朋友可以參考下2017-02-02
C#實(shí)現(xiàn)將程序鎖定到Win7任務(wù)欄的方法
這篇文章主要介紹了C#實(shí)現(xiàn)將程序鎖定到Win7任務(wù)欄的方法,涉及C#調(diào)用Shell類的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
c#進(jìn)度條 progressBar 使用方法的小例子
1、創(chuàng)建進(jìn)度條窗口2、在其他窗口中調(diào)用方法ShowProcess(int percent,string message)2013-04-04
C#操作mysql數(shù)據(jù)庫的代碼實(shí)例
這篇文章為大家提供了一個(gè)C#操作mysql數(shù)據(jù)庫的實(shí)例,大家可以參考使用2013-11-11
C#實(shí)現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform(附DEMO)
本文主要介紹了C#實(shí)現(xiàn)簡易灰度圖和酷炫HeatMap熱力圖winform(附DEMO),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12

