計算器 C# WINForm

現在愈來愈來鬱悶,本想着好好幹java 但是卻因爲對visual studio 2012 很熟悉。所以對用myeclipse 甚至是java有恐懼感。不行一定要hold 住。C#只是學習java的前奏,現在開始一定要放更多時間在java上,界面什麼的都是浮雲,代碼纔是最重要的。

 

這是用C# WIN Form 寫的仿win7計算器標準型   用過的應該知道win7計算器好多功能的,望塵莫及也  加油!

 

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Linq; 
  7. using System.Text; 
  8. using System.Windows.Forms; 
  9.  
  10. namespace Calculator 
  11.     public partial class Form1 : Form 
  12.     { 
  13.         double a, b,m=0;    //a爲前一個數  b爲後一個數 m是爲後面計算sqrt做準備 
  14.         int i=0,flag=0;      //i記錄用了那種運算    flag記錄上輸入框有無式子 1爲有 0爲沒有 
  15.         double z; 
  16.         public Form1() 
  17.         { 
  18.             InitializeComponent(); 
  19.            
  20.  
  21.         } 
  22.  
  23.         private void button1_Click(object sender, EventArgs e)   //”0“   當是顯示框是空時直接輸入0,或者替代前一條式子 
  24.         { 
  25.             if (flag == 0) 
  26.             { 
  27.                 textBox1.Text = textBox1.Text + "0"
  28.                 textBox2.Text = textBox2.Text + "0"
  29.             } 
  30.             else { textBox1.Text = "0"; textBox2.Text = "0"; flag = 0; } 
  31.         } 
  32.  
  33.         private void button2_Click(object sender, EventArgs e)  //”1“ 
  34.         { 
  35.             if (flag == 0) 
  36.             { 
  37.                 textBox1.Text = textBox1.Text + "1"
  38.                 textBox2.Text = textBox2.Text + "1"
  39.             } 
  40.             else { textBox1.Text = "1"; textBox2.Text = "1"; flag = 0; } //”2“ 
  41.         } 
  42.  
  43.         private void button3_Click(object sender, EventArgs e) 
  44.         { 
  45.             if (flag == 0) 
  46.             { 
  47.                 textBox1.Text = textBox1.Text + "2"
  48.                 textBox2.Text = textBox2.Text + "2"
  49.             } 
  50.             else { textBox1.Text = "2"; textBox2.Text = "2"; flag = 0; } 
  51.         } 
  52.  
  53.         private void button4_Click(object sender, EventArgs e) //”3“ 
  54.         { 
  55.             if (flag == 0) 
  56.             { 
  57.                 textBox1.Text = textBox1.Text + "3"
  58.                 textBox2.Text = textBox2.Text + "3"
  59.             } 
  60.             else { textBox1.Text = "3"; textBox2.Text = "3"; flag = 0; } 
  61.         } 
  62.  
  63.         private void button5_Click(object sender, EventArgs e) //”4“ 
  64.         { 
  65.             if (flag == 0) 
  66.             { 
  67.                 textBox1.Text = textBox1.Text + "4"
  68.                 textBox2.Text = textBox2.Text + "4"
  69.             } 
  70.             else { textBox1.Text = "4"; textBox2.Text = "4"; flag = 0; } 
  71.         } 
  72.  
  73.         private void button6_Click(object sender, EventArgs e)   //”5“ 
  74.         { 
  75.  
  76.             if (flag == 0) 
  77.             { 
  78.                 textBox1.Text = textBox1.Text + "5"
  79.                 textBox2.Text = textBox2.Text + "5"
  80.             } 
  81.             else { textBox1.Text = "5"; textBox2.Text = "5"; flag = 0; } 
  82.         } 
  83.  
  84.         private void button7_Click(object sender, EventArgs e) //”6“ 
  85.         { 
  86.  
  87.             if (flag == 0) 
  88.             { 
  89.                 textBox1.Text = textBox1.Text + "6"
  90.                 textBox2.Text = textBox2.Text + "6"
  91.             } 
  92.             else { textBox1.Text = "6"; textBox2.Text = "6"; flag = 0; } 
  93.         } 
  94.  
  95.         private void button8_Click(object sender, EventArgs e) //”7“ 
  96.         { 
  97.  
  98.             if (flag == 0) 
  99.             { 
  100.                 textBox1.Text = textBox1.Text + "7"
  101.                 textBox2.Text = textBox2.Text + "7"
  102.             } 
  103.             else { textBox1.Text = "7"; textBox2.Text = "7"; flag = 0; } 
  104.         } 
  105.  
  106.         private void button9_Click(object sender, EventArgs e) //”8“ 
  107.         { 
  108.  
  109.             if (flag == 0) 
  110.             { 
  111.                 textBox1.Text = textBox1.Text + "8"
  112.                 textBox2.Text = textBox2.Text + "8"
  113.             } 
  114.             else { textBox1.Text = "8"; textBox2.Text = "8"; flag = 0; } 
  115.         } 
  116.  
  117.         private void button10_Click(object sender, EventArgs e) //”9“ 
  118.         { 
  119.  
  120.             if (flag == 0) 
  121.             { 
  122.                 textBox1.Text = textBox1.Text + "9"
  123.                 textBox2.Text = textBox2.Text + "9"
  124.             } 
  125.             else { textBox1.Text = "9"; textBox2.Text = "9"; flag = 0; } 
  126.         } 
  127.  
  128.         private void button11_Click(object sender, EventArgs e) //”小數點“ 
  129.         { 
  130.             if (flag == 0) 
  131.             { 
  132.                 textBox1.Text = textBox1.Text + "."
  133.                 textBox2.Text = textBox2.Text + "."
  134.             } 
  135.             else { textBox1.Text = "."; textBox2.Text = "."; flag = 0; } 
  136.         } 
  137.  
  138.         private void button26_Click(object sender, EventArgs e)  //”+“  清除下輸入框的數據  上輸入框繼續記錄全過程   
  139.         { 
  140.            
  141.                 a = Convert.ToDouble(textBox2.Text); 
  142.                 textBox2.Text = "0"
  143.                 textBox1.Text = textBox1.Text + "+"
  144.                 i = 1; 
  145.         
  146.             
  147.         } 
  148.  
  149.         private void button23_Click(object sender, EventArgs e) //”-“  清除下輸入框的數據  上輸入框繼續記錄全過程  
  150.         { 
  151.             a = Convert.ToDouble(textBox2.Text); 
  152.             textBox2.Text = "0"
  153.             textBox1.Text = textBox1.Text + "-"
  154.             i = 2; 
  155.         } 
  156.  
  157.         private void button24_Click(object sender, EventArgs e)  //”*“  清除下輸入框的數據  上輸入框繼續記錄全過程  
  158.         { 
  159.             a = Convert.ToDouble(textBox2.Text); 
  160.             textBox2.Text = "0"
  161.             textBox1.Text = textBox1.Text + "*"
  162.             i = 3; 
  163.         } 
  164.  
  165.         private void button25_Click(object sender, EventArgs e) //”/“  清除下輸入框的數據  上輸入框繼續記錄全過程  
  166.         { 
  167.             a = Convert.ToDouble(textBox2.Text); 
  168.             textBox2.Text = "0"
  169.             textBox1.Text = textBox1.Text + "/"
  170.             i = 4; 
  171.         } 
  172.  
  173.         private void button18_Click(object sender, EventArgs e) //”%“  清除下輸入框的數據  上輸入框繼續記錄全過程  
  174.         { 
  175.             a = Convert.ToDouble(textBox2.Text); 
  176.             textBox2.Text = "0"
  177.             textBox1.Text = textBox1.Text + "%"
  178.             i = 5; 
  179.         } 
  180.  
  181.         private void button17_Click(object sender, EventArgs e) //”1/x“  清除下輸入框的數據  上輸入框繼續記錄全過程  
  182.         { 
  183.             a = Convert.ToDouble(textBox2.Text); 
  184.             textBox2.Text = "0"
  185.             textBox1.Text = "1/"+textBox1.Text ; 
  186.             i = 6; 
  187.         } 
  188.         private void button19_Click(object sender, EventArgs e)  //”sqrt“  清除下輸入框的數據  上輸入框繼續記錄全過程  
  189.         { 
  190.             a = Convert.ToDouble(textBox2.Text); 
  191.             textBox2.Text = "0"
  192.             textBox1.Text = "sqrt(" + textBox1.Text+")"
  193.             i = 7; 
  194.         } 
  195.         private void button15_Click(object sender, EventArgs e) // ”=“  計算式子 
  196.  
  197.         { 
  198.              
  199.             b=Convert.ToDouble(textBox2.Text); 
  200.              
  201.             double s=0; 
  202.          
  203.  
  204.  
  205.             switch (i) 
  206.             { 
  207.                 case 0: s = b; break
  208.                 case 1: s = a + b; break
  209.                 case 2: s = a - b; break
  210.                 case 3: s = a * b; break
  211.                 case 4: s = a / b; break
  212.                 case 5: s = a % b; break
  213.                 case 6: s = 1 / a; break
  214.                 case 7: if(m!=0) s =Math.Sqrt(m);     //sqrt  是對一個數計算的 , 所以當是式子時應該取最後值 
  215.                     else s = Math.Sqrt(a); 
  216.                     break
  217.                 
  218.             } 
  219.             textBox1.Text = s.ToString(); 
  220.             textBox2.Text = s.ToString(); 
  221.             m = s; 
  222.       
  223.             flag = 1; 
  224.         } 
  225.  
  226.         private void button27_Click(object sender, EventArgs e)  //取數的負值 
  227.         { 
  228.            double a=double.Parse(textBox1.Text)*-1; 
  229.            textBox1.Text = a.ToString(); 
  230.            textBox2.Text = a.ToString(); 
  231.         } 
  232.  
  233.         private void button14_Click(object sender, EventArgs e)  //C   C表示Clear, C則整個的刪除1+2這個計算,你需要重新開始一個計算 
  234.         { 
  235.             textBox1.Text = "0"
  236.             textBox2.Text = "0"
  237.         } 
  238.  
  239.         private void button13_Click(object sender, EventArgs e) //CE  CE表示Clear Error,比如你輸入1+2之後,按CE會清除第二個參數2,你可以繼續輸入其他數和第一個參數1相加。 
  240.         {                                                          //我的想法是相減  textBox1.Text = textBox1.Text - textBox2.Text;   
  241.             if (textBox2.Text != "0"
  242.             { 
  243.                 int n1, n2; 
  244.                 n2 = textBox2.TextLength - 1; 
  245.                 n1 = textBox1.TextLength - textBox2.TextLength + 1; 
  246.  
  247.                 textBox1.Text = textBox1.Text.Remove(n1, n2);   //用Remove(int n1,int n2),其中n1指索引位置,n2指長度 
  248.  
  249.                 textBox2.Text = "0"
  250.             } 
  251.         } 
  252.  
  253.         private void button12_Click(object sender, EventArgs e) //”←“   可以清楚一位數 
  254.         { 
  255.             String a = ""
  256.             for (int i = 0; i < textBox1.TextLength - 1; i++) 
  257.             { 
  258.                 a = a + textBox1.Text[i]; 
  259.             } 
  260.             textBox1.Text = a; 
  261.             a = ""
  262.             for (int i = 0; i < textBox2.TextLength - 1; i++) 
  263.             { 
  264.                 a = a + textBox2.Text[i]; 
  265.             } 
  266.             textBox2.Text = a; 
  267.         } 
  268.  
  269.         private void button21_Click(object sender, EventArgs e)//MS     MS=Memory Save 
  270.         { 
  271.             textBox3.Text = "M"
  272.             z = Convert.ToDouble(textBox2.Text); 
  273.             flag = 1; 
  274.         } 
  275.  
  276.         private void button20_Click(object sender, EventArgs e)//MR    MR=Memory Read 
  277.         { 
  278.             textBox2.Text = z.ToString();  
  279.            textBox1.Text =z.ToString(); 
  280.                 flag = 1; 
  281.         } 
  282.  
  283.         private void button16_Click(object sender, EventArgs e)//MC    MC=Memory Clear 
  284.         { 
  285.             textBox3.Text = ""
  286.             z = 0; 
  287.         } 
  288.  
  289.         private void button28_Click(object sender, EventArgs e)//M+    M+=Memory Add 
  290.         { 
  291.             a = Convert.ToDouble(textBox2.Text); 
  292.             z = z + a; 
  293.             flag = 1; 
  294.  
  295.         } 
  296.  
  297.         private void button22_Click(object sender, EventArgs e)//M-    M-=Memory Minus 
  298.         { 
  299.             a = Convert.ToDouble(textBox2.Text); 
  300.             z = z - a; 
  301.             flag = 1; 
  302.         } 
  303.  
  304.  
  305.        
  306.        
  307.      
  308.  
  309.        
  310.          
  311.     } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章