C#计算器

最近刚刚学习C#,在师傅的要求下,写了个计算器的程序

  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.Threading.Tasks;
  9 using System.Windows.Forms;
 10 
 11 namespace WindowsFormsApplication7
 12 {
 13     
 14     public partial class Form1 : Form
 15     {
 16         public Form1()
 17         {
 18             InitializeComponent();
 19         }
 20 
 21         private void button1_Click(object sender, EventArgs e)
 22         {
 23             if(sender is Button)
 24             {
 25                 Button b=(Button)sender;
 26 
 27                 if (b.Name == "BtnPlus" | b.Name == "BtnJian" | b.Name == "BtnCheng" | b.Name == "BtnChu" | b.Name == "BtnPoint")//判断所按键是否为符号键
 28                 {
 29                     if (textBox1.Text.Substring(textBox1.Text.Length - 1, 1) == "+" | textBox1.Text.Substring(textBox1.Text.Length - 1, 1) == "." | textBox1.Text.Substring(textBox1.Text.Length - 1, 1) == "-" | textBox1.Text.Substring(textBox1.Text.Length - 1, 1) == "*" | textBox1.Text.Substring(textBox1.Text.Length - 1, 1) == "/")
 30                     {
 31                         textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);//当当前字符串最后位为符号时,用当前符号替换原有符号
 32                         textBox1.Text += b.Text;
 33                     }
 34                     else
 35                     {
 36                         textBox1.Text += b.Text;//如果当前字符串最后位为非符号位,添加符号
 37                     }
 38                 }
 39                 else if(b.Name=="BtnC")//当前位为C时,清除当前文本
 40                 {
 41                     textBox1.Text="";
 42                 }
 43                 else if(b.Name=="BtnEqual")//当前按键为“=”时
 44                 {
 45                 
 46                     List<int> int1 = new List<int>();//此数组为了保存当前文本字符串符号位的索引
 47                     List<string> string1 = new List<string>();//此处保存当前符号位索引对应的符号
 48                     List<string> string2 = new List<string>();//此处保存符号位之间的文本信息
 49                     for (int i = 0; i<textBox1.Text.Length; i++)//巡回查找当前字符,并对三个list进行操作
 50                     {
 51               
 52                         if(textBox1.Text[i]==+)
 53                         {
 54                           int1.Add(i);
 55                           string1.Add("+");
 56                         }
 57                         else if (textBox1.Text[i] == -)
 58                         {
 59                             int1.Add(i);
 60                             string1.Add("-");
 61 
 62                         }
 63                         else if (textBox1.Text[i] == *)
 64                         {
 65                             int1.Add(i);
 66                             string1.Add("*");
 67                         }
 68                         else if (textBox1.Text[i] == /)
 69                         {
 70                             int1.Add(i);
 71                             string1.Add("/");
 72                         }  
 73 
 74                     }
 75 
 76                    string2.Add(textBox1.Text.Substring(0,int1[0]));
 77 
 78                     for (int j = 0; j < string1.Count-1; j++)
 79                     {
 80                         string2.Add(textBox1.Text.Substring(int1[j]+1,int1[j+1]-int1[j]-1));
 81                     }
 82                     string2.Add(textBox1.Text.Substring(int1[int1.Count - 1]+1));
 83                     double P = Convert.ToDouble(string2[0]);//P为计算的最终值
 84                     for(int j=0;j<string1.Count;j++)
 85                     {
 86                         switch(string1[j])
 87                         {
 88                                
 89                             case "+":
 90                                 P+= Convert.ToDouble(string2[j + 1]);
 91                                 break;
 92                             case "-":
 93                                 P -= Convert.ToDouble(string2[j + 1]);
 94                                 break;
 95                             case "*":
 96                                 P *= Convert.ToDouble(string2[j + 1]);
 97                                 break;
 98                             case "/":
 99                                 P /= Convert.ToDouble(string2[j + 1]);
100                                 break;
101                         }
102                         textBox1.Text = P.ToString();
103                         
104                     }
105 
106                 }
107                   
108                 else
109                 {
110                     textBox1.Text += b.Text;
111                 }
112             }
113         }
114 
115         private void textBox1_TextChanged(object sender, EventArgs e)
116         {
117 
118         }
119 
120         private void Form1_Load(object sender, EventArgs e)
121         {
122             this.textBox1.Focus();
123         }
124     }
125 }

附上界面照片

C#计算器

 

C#计算器

上一篇:win7系统服务print spooler 无法启动解决方法(开启及关闭方法)


下一篇:Windows Azure 超值体验1元一个月,还可获得CSDN 500C币