用c#實(shí)現(xiàn)簡易的計(jì)算器功能實(shí)例代碼
由于今天在網(wǎng)上搜了一下c#寫的計(jì)算器,發(fā)現(xiàn)大多都太繁瑣了,很多沒必要并且不容易理解的東西就專門寫了這個(gè)博客
1.首先新建一個(gè)windows窗體應(yīng)用的項(xiàng)目。執(zhí)行文件-新建-項(xiàng)目-windows窗體應(yīng)用
2.在工具箱中拖出一個(gè)textbox用于輸入和顯示,再拖出21個(gè)button按鈕用來當(dāng)計(jì)算器的按鍵,在textbox下面還有一個(gè)lable控件(我把它屬性改成了空格所以看不到了),改一下按鈕的text屬性
3.雙擊數(shù)字按鈕進(jìn)入代碼界面(數(shù)字只用一個(gè)事件即可,運(yùn)算符也是用一個(gè)事件,其他每個(gè)按鈕都需要雙擊添加事件)
4.代碼呢已經(jīng)準(zhǔn)備好了,只要雙擊按鈕進(jìn)入代碼界面,然后對應(yīng)著粘上就行了(注意所有數(shù)字都是用的一個(gè)事件,都有標(biāo)注,可以選擇按鈕,然后單擊屬性里的事件(閃電圖標(biāo))查看click的事件)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 計(jì)算器
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
//定義變量
char oper;
double num1;
double num2;
double result = 0;
double memory=0.0;
private void Button9_Click(object sender, EventArgs e)//數(shù)字按鈕的功能實(shí)現(xiàn)
{
Button a = (Button)sender;//判斷按下的是哪個(gè)按鈕
if (textBox1.Text == “0”)
{
textBox1.Text = a.Text;
}
else
textBox1.Text += a.Text;
}
private void Button16_Click(object sender, EventArgs e)//運(yùn)算符按鈕的功能實(shí)現(xiàn)
 {
  if (textBox1.Text != "")
  {
   num1 = double.Parse(textBox1.Text);
   oper = char.Parse(((Button)sender).Text);
   textBox1.Text = "";
  }
 }
 private void Button15_Click(object sender, EventArgs e)//C按鈕的功能實(shí)現(xiàn)
 {
  textBox1.Text = "";
  textBox1.Focus();
  num1 = 0;
  num2 = 0;
  oper = ' ';
 }
 private void Button14_Click(object sender, EventArgs e)//結(jié)果按鈕的功能實(shí)現(xiàn)
 {
  if (textBox1.Text != "")
  {
   num2 = double.Parse(textBox1.Text);
   switch (oper)
   {
    case '+': result = num1 + num2; break;
    case '-': result = num1 - num2; break;
    case '*': result = num1 * num2; break;
    case '÷': result = num1 / num2; break;
   }
   textBox1.Text = result.ToString();
  }
 }
 private void Button17_Click(object sender, EventArgs e)//小數(shù)點(diǎn)按鈕的功能實(shí)現(xiàn)
 {
  if (textBox1.Text != "")
  {
   textBox1.Text += ".";
  }
  else
  {
   textBox1.Text = "0.";
  }
 }
 private void Button18_Click(object sender, EventArgs e)//M+按鈕的功能實(shí)現(xiàn)
 {
  if(textBox1.Text!="")
  {
   label1.Text = "M";
   memory += double.Parse(textBox1.Text);
   textBox1.Text = " ";
  }
 }
 private void Button20_Click(object sender, EventArgs e)//MR按鈕的功能實(shí)現(xiàn)
 {
  textBox1.Text = memory.ToString();
 }
 private void Button21_Click(object sender, EventArgs e)//MC按鈕的功能實(shí)現(xiàn)
 {
  label1.Text = "";
  memory = 0;
 }
 private void Button19_Click(object sender, EventArgs e)//M-按鈕的功能實(shí)現(xiàn)
 {
  if (textBox1.Text != "")
  {
   label1.Text = "M";
   memory -= double.Parse(textBox1.Text);
   textBox1.Text = " ";
  }
 }
}
以上所述是小編給大家介紹的c#實(shí)現(xiàn)簡易的計(jì)算器功能詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對我們網(wǎng)站的支持!
欄 目:C#教程
下一篇:C# FileStream實(shí)現(xiàn)大文件復(fù)制
本文標(biāo)題:用c#實(shí)現(xiàn)簡易的計(jì)算器功能實(shí)例代碼
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4776.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新聞效果的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
 - 01-10C#實(shí)現(xiàn)多線程下載文件的方法
 - 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
 - 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
 - 01-10C#自定義簽名章實(shí)現(xiàn)方法
 - 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
 - 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法
 


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
 - 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
 - 01-10WinForm限制窗體不能移到屏幕外的方法
 - 01-10WinForm繪制圓角的方法
 - 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
 - 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動新
 - 01-10C#停止線程的方法
 - 01-10C#實(shí)現(xiàn)清空回收站的方法
 - 01-10C#通過重寫Panel改變邊框顏色與寬度的
 - 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
 
隨機(jī)閱讀
- 01-10C#中split用法實(shí)例總結(jié)
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
 - 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 01-10delphi制作wav文件的方法
 - 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
 - 04-02jquery與jsp,用jquery
 - 01-11ajax實(shí)現(xiàn)頁面的局部加載
 


