C#簡易版計算器
阿新 • • 發佈:2019-01-08
計算器需求分析
1.介面分析
做一個顯示屏
17個按鈕(0~9 +-*/% =ce)
2.需要實現的功能
選擇第一個數字
選擇運算子
選擇第二個數字
按下等號進行計算,結果顯示在顯示屏中
3.實現步驟
1)先做介面
a.顯示屏:textbox,listbox,label
b.使用17個button ,button上的文字對應數字符號
2)
補充:申請兩個int型別變數,第一個num1裝第一個數字, 第二個num2裝第二個數字,再申請一個變數string用來裝
運算子
a.輸入第一個數字,點一個數字按鈕,螢幕上顯示一個,之前顯示的數字在前面待著
a1. 新增按鈕的click事件
a2,事件觸發,將按鈕代表的數字顯示在textbox中
b.當輸入符號的時候,清除螢幕,但是後臺必須記錄第一個數字
b1. 新增符號按鈕的click事件
b2. 當點選任何一個符號按鈕用一個變數裝剛才的textbox中的數字
c.輸入第二個數字
d.按下等號按鈕,顯示屏上的文字改變成兩個數字的運算結果
小技巧:將textbox中的tabstop設定為false,可以使游標失去焦點;
效果圖:
程式碼:
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 _1228計算器_複雜版_ { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } //1 private void button1_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text+"1"; // textBox1.Text = button1.Text; } //2 private void button2_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "2"; } //3 private void button3_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "3"; } //4 private void button4_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text +"4"; } //5 private void button5_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "5"; } //6 private void button6_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "6"; } //7 private void button7_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "7"; } //8 private void button8_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "8"; } //9 private void button9_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "9"; } //0 private void button10_Click(object sender, EventArgs e) { textBox1.Text = textBox1.Text + "0"; } //這是裝第一個數字的變數盒子 int num1; //這是裝第二個數字的變數盒子 int num2; //這個盒子裝運算子號 string fun; //+ private void button11_Click(object sender, EventArgs e) { jisuan(); fun = "+"; } //提取方法,方法是一個程式碼模組,(右擊,選擇快速操作和重構,點選“提取方法”,自由取名,呼叫該方法) private void jisuan() { num1 = Convert.ToInt32(textBox1.Text); textBox1.Text = ""; } //- private void button12_Click(object sender, EventArgs e) { jisuan(); fun = "-"; } //* private void button13_Click(object sender, EventArgs e) { jisuan(); fun = "*"; } //÷ private void button14_Click(object sender, EventArgs e) { jisuan(); fun = "/"; } //% private void button15_Click(object sender, EventArgs e) { jisuan(); fun = "%"; } //= private void button16_Click(object sender, EventArgs e) { num2 = int.Parse(textBox1.Text); textBox1.Text = ""; if (fun=="+") { textBox1.Text = num1+"+"+ num2+"="+(num1 + num2).ToString(); } if (fun=="-") { textBox1.Text = num1 + "-" + num2 + "=" + (num1 - num2).ToString(); } if (fun=="*") { textBox1.Text = num1 + "×" + num2 + "=" + (num1 * num2).ToString(); } if (fun=="/") { textBox1.Text = num1 + "÷" + num2 + "=" + (num1 / num2).ToString(); } if (fun=="%") { textBox1.Text = num1 + "%" + num2 + "=" + (num1 % num2).ToString(); } } //ce private void button17_Click(object sender, EventArgs e) { textBox1.Text = ""; } } }