C#實(shí)現(xiàn)計算器精簡版
本文實(shí)例為大家分享了C#實(shí)現(xiàn)計算器精簡版的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
計算器需求分析
一、界面設(shè)計
1.做一個顯示屏
2.17個按鈕(0-9,±×÷%=,CE)
二、需要實(shí)現(xiàn)的功能
1.輸入第一個數(shù)字
2.選擇運(yùn)算類型
3.輸入第二個數(shù)字
4.按下等號計算出結(jié)果,結(jié)果顯示在顯示屏上
三、實(shí)現(xiàn)步驟
1.先做界面
a.顯示屏 textbox、listbox、label
b.使用17個button,button上的文本改成對應(yīng)的數(shù)字符號
2.補(bǔ)充:申請兩個int類型變量,第一個num1裝第一個數(shù)字
第二個num2裝第二個數(shù)字
(1).輸入第一個數(shù)字,當(dāng)點(diǎn)一個數(shù)字按鈕,屏幕上顯示一個,之前顯示的數(shù)字在前面呆著
a1.添加按鈕的cilck事件
a2.事件觸發(fā),將按鈕代表的數(shù)字顯示textbox1的text
(2).當(dāng)輸入符號的時候,清除屏幕,但是后臺必須記錄好第一個數(shù)字
b1.添加符號按鈕的click事件
b2.當(dāng)點(diǎn)任何一個符號按鈕用一個變量num1裝剛才輸入的textbox1中的數(shù)字
(3).輸入第二個數(shù)字
c1. 當(dāng)點(diǎn)任何一個符號按鈕用一個變量num2裝剛才輸入的textbox1中的數(shù)字
(4).按下等號按鈕,顯示屏上面的文本改變成兩個數(shù)字的運(yùn)算結(jié)果

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 簡單的計算器制作
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? public Form1()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
? ? ? ? private void Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //計算窗口加載居中的位置
? ? ? ? ? ? int left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
? ? ? ? ? ? int top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
? ? ? ? ? ? this.Location = new Point(left,top);
? ? ? ? ? ? //加載的時候獲取焦點(diǎn)
? ? ? ? ? ? button1.TabIndex = 0;
? ? ? ? }
? ? ? ? //當(dāng)我們輸入完第一個數(shù)字之后 ?在輸入運(yùn)算符的時候 我們要記下第一個數(shù)字num1
? ? ? ? //當(dāng)我們輸入完第二個數(shù)字之后 ?在輸入等號的時候 我們要記下第二個數(shù)字num1
? ? ? ? double num1 = 0;
? ? ? ? double num2 = 0;
? ? ? ? bool iskey = false;
? ? ? ? //ce
? ? ? ? private void button1_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //設(shè)置清空
? ? ? ? ? ? textBox1.Text = "";?
? ? ? ? }
? ? ? ?
? ? ? ? //1
? ? ? ? private void button4_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (iskey)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? ? ? iskey = false;
? ? ? ? ? ? }
? ? ? ? ? ? textBox1.Text += "1";
? ? ? ? }
? ? ? ? //2
? ? ? ? private void button5_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (iskey)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? ? ? iskey = false;
? ? ? ? ? ? }
? ? ? ? ? ? textBox1.Text += "2";
? ? ? ? }
? ? ? ? //3
? ? ? ? private void button6_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (iskey)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? ? ? iskey = false;
? ? ? ? ? ? }
? ? ? ? ? ? textBox1.Text += "3";
? ? ? ? }
? ? ? ? //4
? ? ? ? private void button8_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (iskey)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? ? ? iskey = false;
? ? ? ? ? ? }
? ? ? ? ? ? textBox1.Text += "4";
? ? ? ? }
? ? ? ? //5
? ? ? ? private void button9_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (iskey)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? ? ? iskey = false;
? ? ? ? ? ? }
? ? ? ? ? ? textBox1.Text += "5";
? ? ? ? }
? ? ? ? //6
? ? ? ? private void button10_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (iskey)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? ? ? iskey = false;
? ? ? ? ? ? }
? ? ? ? ? ? textBox1.Text += "6";
? ? ? ? }
? ? ? ? //7
? ? ? ? private void button12_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (iskey)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? ? ? iskey = false;
? ? ? ? ? ? }
? ? ? ? ? ? textBox1.Text += "7";
? ? ? ? }
? ? ? ? //8
? ? ? ? private void button13_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (iskey)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? ? ? iskey = false;
? ? ? ? ? ? }
? ? ? ? ? ? textBox1.Text += "8";
? ? ? ? }
? ? ? ? //9
? ? ? ? private void button14_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (iskey)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? ? ? iskey = false;
? ? ? ? ? ? }
? ? ? ? ? ? textBox1.Text += "9";
? ? ? ? }
? ? ? ? //0
? ? ? ? private void button17_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (iskey)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? ? ? iskey = false;
? ? ? ? ? ? }
? ? ? ? ? ? textBox1.Text += "9";
? ? ? ? }
? ? ? ? //.
? ? ? ? private void button16_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (iskey)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? textBox1.Text = "";
? ? ? ? ? ? ? ? iskey = false;
? ? ? ? ? ? }
? ? ? ? ? ? textBox1.Text += ".";
? ? ? ? }
? ? ? ? //定義一個空的來接收符號
? ? ? ? string type=" ";
? ? ? ? //+
? ? ? ? private void button15_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if(textBox1.Text != "")
? ? ? ? ? ? {
? ? ? ? ? ? //獲取運(yùn)算的第一個數(shù)字(前一個數(shù)字);將字符串類型轉(zhuǎn)換為int類型(int.parse())
? ? ? ? ? ? ?// num1 = int.Parse(textBox1.Text);
? ? ? ? ? ? // num1 = Convert.ToInt32(textBox1.Text);
? ? ? ? ? // ?第二種轉(zhuǎn)換方式convert
? ? ? ? num1 = Convert.ToDouble(textBox1.Text);
? ? ? ? ? ? }
? ? ? ? ? ? type = "+";
? ? ? ? ? ? // ?textBox1.Text = "";
? ? ? ? ? ? iskey = true;
? ? ? ? }
? ? ? ? //-
? ? ? ? private void button3_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if(textBox1.Text != ""){
? ? ? ? ? ? ? ? num1 = Convert.ToDouble(textBox1.Text);
? ? ? ? ? ? }
? ? ? ? ? ? type = "-";
? ? ? ? ? ? // textBox1.Text = "";
? ? ? ? ? ? iskey = true;
? ? ? ? }
? ? ? ? //*
? ? ? ? private void button7_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if(textBox1.Text != "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? num1 = Convert.ToDouble(textBox1.Text);
? ? ? ? ? ? }
? ? ? ? ? ? type = "*";
? ? ? ? ? ? // ?textBox1.Text = "";
? ? ? ? ? ? iskey = true;
? ? ? ? }
? ? ? ? //÷
? ? ? ? private void button11_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if(textBox1.Text != "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? num1 = Convert.ToDouble(textBox1.Text);
? ? ? ? ? ? }
? ? ? ? ? ? type = "/";
? ? ? ? ? ? //textBox1.Text = "";
? ? ? ? ? ? iskey = true;
? ? ? ? }
? ? ? ? //%
? ? ? ? private void button18_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? iskey = true;
? ? ? ? ? ? if (textBox1.Text != "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? num1 = Convert.ToDouble(textBox1.Text);
? ? ? ? ? ? }
? ? ? ? ? ? type = "%";
? ? ? ? ? ? //textBox1.Text = "";
? ? ? ? }
? ? ? ? //=
? ? ? ? private void button2_Click(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? if (iskey)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? }
? ? ? ? ? ? iskey = true;
? ? ? ? ? ? if(textBox1.Text != "")
? ? ? ? ? ? {
? ? ? ? ? ? ? ? num2 = Convert.ToDouble(textBox1.Text);
? ? ? ? ? ? }
? ? ? ? ? ? switch (type)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case ?"+":
? ? ? ? ? ? ? ? //括號里進(jìn)行計算,計算的結(jié)果轉(zhuǎn)化為string類型,并顯示在屏幕(textbox1)里;
? ? ? ? ? ? ? ? ? ? textBox1.Text = (num1 + num2).ToString();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "-":
? ? ? ? ? ? ? ? ? ? textBox1.Text = (num1 - num2).ToString();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "*":
? ? ? ? ? ? ? ? ? ? textBox1.Text = (num1 * num2).ToString();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "/":
? ? ? ? ? ? ? ? ? ? textBox1.Text = (num1 / num2).ToString();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case "%":
? ? ? ? ? ? ? ? ? ? textBox1.Text = (num1 % num2).ToString();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? }
? ? }
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入理解C#索引器(一種支持參數(shù)的屬性)與屬性的對比
本篇文章是對C#索引器(一種支持參數(shù)的屬性)與屬性的對比進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
使用數(shù)字簽名實(shí)現(xiàn)數(shù)據(jù)庫記錄防篡改(Java實(shí)現(xiàn))
本文主要介紹了Java中使用數(shù)字簽名實(shí)現(xiàn)數(shù)據(jù)庫記錄防篡改的方法與步驟。具有很好的參考價值,下面跟著小編一起來看下吧2017-01-01
C# 關(guān)于AppDomain的一些總結(jié)
這篇文章主要介紹了C# 關(guān)于AppDomain的一些總結(jié),幫助大家更好的理解和使用c#,感興趣的朋友可以了解下2021-02-02
C#設(shè)計模式之Visitor訪問者模式解決長隆歡樂世界問題實(shí)例
這篇文章主要介紹了C#設(shè)計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結(jié)合具體實(shí)例形式分析了C#使用訪問者模式解決長隆歡樂世界問題的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-09-09

