現在愈來愈來鬱悶,本想着好好幹java 但是卻因爲對visual studio 2012 很熟悉。所以對用myeclipse 甚至是java有恐懼感。不行一定要hold 住。C#只是學習java的前奏,現在開始一定要放更多時間在java上,界面什麼的都是浮雲,代碼纔是最重要的。
這是用C# WIN Form 寫的仿win7計算器標準型 用過的應該知道win7計算器好多功能的,望塵莫及也 加油!
- 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 Calculator
- {
- public partial class Form1 : Form
- {
- double a, b,m=0; //a爲前一個數 b爲後一個數 m是爲後面計算sqrt做準備
- int i=0,flag=0; //i記錄用了那種運算 flag記錄上輸入框有無式子 1爲有 0爲沒有
- double z;
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e) //”0“ 當是顯示框是空時直接輸入0,或者替代前一條式子
- {
- if (flag == 0)
- {
- textBox1.Text = textBox1.Text + "0";
- textBox2.Text = textBox2.Text + "0";
- }
- else { textBox1.Text = "0"; textBox2.Text = "0"; flag = 0; }
- }
- private void button2_Click(object sender, EventArgs e) //”1“
- {
- if (flag == 0)
- {
- textBox1.Text = textBox1.Text + "1";
- textBox2.Text = textBox2.Text + "1";
- }
- else { textBox1.Text = "1"; textBox2.Text = "1"; flag = 0; } //”2“
- }
- private void button3_Click(object sender, EventArgs e)
- {
- if (flag == 0)
- {
- textBox1.Text = textBox1.Text + "2";
- textBox2.Text = textBox2.Text + "2";
- }
- else { textBox1.Text = "2"; textBox2.Text = "2"; flag = 0; }
- }
- private void button4_Click(object sender, EventArgs e) //”3“
- {
- if (flag == 0)
- {
- textBox1.Text = textBox1.Text + "3";
- textBox2.Text = textBox2.Text + "3";
- }
- else { textBox1.Text = "3"; textBox2.Text = "3"; flag = 0; }
- }
- private void button5_Click(object sender, EventArgs e) //”4“
- {
- if (flag == 0)
- {
- textBox1.Text = textBox1.Text + "4";
- textBox2.Text = textBox2.Text + "4";
- }
- else { textBox1.Text = "4"; textBox2.Text = "4"; flag = 0; }
- }
- private void button6_Click(object sender, EventArgs e) //”5“
- {
- if (flag == 0)
- {
- textBox1.Text = textBox1.Text + "5";
- textBox2.Text = textBox2.Text + "5";
- }
- else { textBox1.Text = "5"; textBox2.Text = "5"; flag = 0; }
- }
- private void button7_Click(object sender, EventArgs e) //”6“
- {
- if (flag == 0)
- {
- textBox1.Text = textBox1.Text + "6";
- textBox2.Text = textBox2.Text + "6";
- }
- else { textBox1.Text = "6"; textBox2.Text = "6"; flag = 0; }
- }
- private void button8_Click(object sender, EventArgs e) //”7“
- {
- if (flag == 0)
- {
- textBox1.Text = textBox1.Text + "7";
- textBox2.Text = textBox2.Text + "7";
- }
- else { textBox1.Text = "7"; textBox2.Text = "7"; flag = 0; }
- }
- private void button9_Click(object sender, EventArgs e) //”8“
- {
- if (flag == 0)
- {
- textBox1.Text = textBox1.Text + "8";
- textBox2.Text = textBox2.Text + "8";
- }
- else { textBox1.Text = "8"; textBox2.Text = "8"; flag = 0; }
- }
- private void button10_Click(object sender, EventArgs e) //”9“
- {
- if (flag == 0)
- {
- textBox1.Text = textBox1.Text + "9";
- textBox2.Text = textBox2.Text + "9";
- }
- else { textBox1.Text = "9"; textBox2.Text = "9"; flag = 0; }
- }
- private void button11_Click(object sender, EventArgs e) //”小數點“
- {
- if (flag == 0)
- {
- textBox1.Text = textBox1.Text + ".";
- textBox2.Text = textBox2.Text + ".";
- }
- else { textBox1.Text = "."; textBox2.Text = "."; flag = 0; }
- }
- private void button26_Click(object sender, EventArgs e) //”+“ 清除下輸入框的數據 上輸入框繼續記錄全過程
- {
- a = Convert.ToDouble(textBox2.Text);
- textBox2.Text = "0";
- textBox1.Text = textBox1.Text + "+";
- i = 1;
- }
- private void button23_Click(object sender, EventArgs e) //”-“ 清除下輸入框的數據 上輸入框繼續記錄全過程
- {
- a = Convert.ToDouble(textBox2.Text);
- textBox2.Text = "0";
- textBox1.Text = textBox1.Text + "-";
- i = 2;
- }
- private void button24_Click(object sender, EventArgs e) //”*“ 清除下輸入框的數據 上輸入框繼續記錄全過程
- {
- a = Convert.ToDouble(textBox2.Text);
- textBox2.Text = "0";
- textBox1.Text = textBox1.Text + "*";
- i = 3;
- }
- private void button25_Click(object sender, EventArgs e) //”/“ 清除下輸入框的數據 上輸入框繼續記錄全過程
- {
- a = Convert.ToDouble(textBox2.Text);
- textBox2.Text = "0";
- textBox1.Text = textBox1.Text + "/";
- i = 4;
- }
- private void button18_Click(object sender, EventArgs e) //”%“ 清除下輸入框的數據 上輸入框繼續記錄全過程
- {
- a = Convert.ToDouble(textBox2.Text);
- textBox2.Text = "0";
- textBox1.Text = textBox1.Text + "%";
- i = 5;
- }
- private void button17_Click(object sender, EventArgs e) //”1/x“ 清除下輸入框的數據 上輸入框繼續記錄全過程
- {
- a = Convert.ToDouble(textBox2.Text);
- textBox2.Text = "0";
- textBox1.Text = "1/"+textBox1.Text ;
- i = 6;
- }
- private void button19_Click(object sender, EventArgs e) //”sqrt“ 清除下輸入框的數據 上輸入框繼續記錄全過程
- {
- a = Convert.ToDouble(textBox2.Text);
- textBox2.Text = "0";
- textBox1.Text = "sqrt(" + textBox1.Text+")";
- i = 7;
- }
- private void button15_Click(object sender, EventArgs e) // ”=“ 計算式子
- {
- b=Convert.ToDouble(textBox2.Text);
- double s=0;
- switch (i)
- {
- case 0: s = b; break;
- case 1: s = a + b; break;
- case 2: s = a - b; break;
- case 3: s = a * b; break;
- case 4: s = a / b; break;
- case 5: s = a % b; break;
- case 6: s = 1 / a; break;
- case 7: if(m!=0) s =Math.Sqrt(m); //sqrt 是對一個數計算的 , 所以當是式子時應該取最後值
- else s = Math.Sqrt(a);
- break;
- }
- textBox1.Text = s.ToString();
- textBox2.Text = s.ToString();
- m = s;
- flag = 1;
- }
- private void button27_Click(object sender, EventArgs e) //取數的負值
- {
- double a=double.Parse(textBox1.Text)*-1;
- textBox1.Text = a.ToString();
- textBox2.Text = a.ToString();
- }
- private void button14_Click(object sender, EventArgs e) //C C表示Clear, C則整個的刪除1+2這個計算,你需要重新開始一個計算
- {
- textBox1.Text = "0";
- textBox2.Text = "0";
- }
- private void button13_Click(object sender, EventArgs e) //CE CE表示Clear Error,比如你輸入1+2之後,按CE會清除第二個參數2,你可以繼續輸入其他數和第一個參數1相加。
- { //我的想法是相減 textBox1.Text = textBox1.Text - textBox2.Text;
- if (textBox2.Text != "0")
- {
- int n1, n2;
- n2 = textBox2.TextLength - 1;
- n1 = textBox1.TextLength - textBox2.TextLength + 1;
- textBox1.Text = textBox1.Text.Remove(n1, n2); //用Remove(int n1,int n2),其中n1指索引位置,n2指長度
- textBox2.Text = "0";
- }
- }
- private void button12_Click(object sender, EventArgs e) //”←“ 可以清楚一位數
- {
- String a = "";
- for (int i = 0; i < textBox1.TextLength - 1; i++)
- {
- a = a + textBox1.Text[i];
- }
- textBox1.Text = a;
- a = "";
- for (int i = 0; i < textBox2.TextLength - 1; i++)
- {
- a = a + textBox2.Text[i];
- }
- textBox2.Text = a;
- }
- private void button21_Click(object sender, EventArgs e)//MS MS=Memory Save
- {
- textBox3.Text = "M";
- z = Convert.ToDouble(textBox2.Text);
- flag = 1;
- }
- private void button20_Click(object sender, EventArgs e)//MR MR=Memory Read
- {
- textBox2.Text = z.ToString();
- textBox1.Text =z.ToString();
- flag = 1;
- }
- private void button16_Click(object sender, EventArgs e)//MC MC=Memory Clear
- {
- textBox3.Text = "";
- z = 0;
- }
- private void button28_Click(object sender, EventArgs e)//M+ M+=Memory Add
- {
- a = Convert.ToDouble(textBox2.Text);
- z = z + a;
- flag = 1;
- }
- private void button22_Click(object sender, EventArgs e)//M- M-=Memory Minus
- {
- a = Convert.ToDouble(textBox2.Text);
- z = z - a;
- flag = 1;
- }
- }
- }