首页 > 代码库 > 一个简单的Windows计算器

一个简单的Windows计算器

 

 

UI

 

功能

很简单用Grid5*4布局,填充Button

跨越多个单元格,设置控件的Grid.ColumnSpan Grid.RowSpan属性

 1         <Grid Width="400" Grid.Row="1" Height="300" HorizontalAlignment="Center" VerticalAlignment="Center"> 2             <Grid.RowDefinitions> 3                 <RowDefinition /> 4                 <RowDefinition /> 5                 <RowDefinition /> 6                 <RowDefinition /> 7             </Grid.RowDefinitions> 8             <Grid.ColumnDefinitions> 9                 <ColumnDefinition />10                 <ColumnDefinition />11                 <ColumnDefinition />12                 <ColumnDefinition />13                 <ColumnDefinition />14             </Grid.ColumnDefinitions>15             <Button Content="1" Grid.Row="0" Grid.Column="0" Click="Button_Click"/>16             <Button Content="2" Grid.Row="0" Grid.Column="1" Click="Button_Click"/>17             <Button Content="3" Grid.Row="0" Grid.Column="2" Click="Button_Click"/>18             <Button Content="4" Grid.Row="1" Grid.Column="0" Click="Button_Click"/>19             <Button Content="5" Grid.Row="1" Grid.Column="1" Click="Button_Click"/>20             <Button Content="6" Grid.Row="1" Grid.Column="2" Click="Button_Click"/>21             <Button Content="7" Grid.Row="2" Grid.Column="0" Click="Button_Click"/>22             <Button Content="8" Grid.Row="2" Grid.Column="1" Click="Button_Click"/>23             <Button Content="9" Grid.Row="2" Grid.Column="2" Click="Button_Click"/>24             <Button Content="0" Grid.Row="3" Grid.Column="0" Click="Button_Click"/>25             <Button Content="." Grid.Row="3" Grid.Column="1" Click="Button_Click"/>26             <Button x:Name="AnsButton" Background="Orange" Content="Ans" Grid.Row="3" Grid.Column="2" Click="AnsButton_Click"/>27 28             <Grid Grid.Column="3" Grid.RowSpan="3" Grid.ColumnSpan="2">29                 <Grid.RowDefinitions>30                     <RowDefinition />31                     <RowDefinition />32                     <RowDefinition />33                 </Grid.RowDefinitions>34                 <Grid.ColumnDefinitions>35                     <ColumnDefinition />36                     <ColumnDefinition />37                 </Grid.ColumnDefinitions>38                 <Grid.Resources>39                     <Style TargetType="Button" BasedOn="{StaticResource {x:Type Button}}">40                         <Setter Property="Background" Value="Purple"/>41                     </Style>42                 </Grid.Resources>43                 <Button Content="(" Grid.Row="0" Grid.Column="0" Click="Button_Click"/>44                 <Button Content=")" Grid.Row="0" Grid.Column="1" Click="Button_Click"/>45 46                 <Button Content="+" Grid.Row="1" Grid.Column="0" Click="Button_Click"/>47                 <Button Content="-" Grid.Row="1" Grid.Column="1" Click="Button_Click"/>48 49                 <Button Content="*" Grid.Row="2" Grid.Column="0" Click="Button_Click"/>50                 <Button Content="/" Grid.Row="2" Grid.Column="1" Click="Button_Click"/>51             </Grid>52 53             54 55 56             <Button x:Name="ResultButton" Background="#FF00A000" Content="=" Grid.Row="3" Grid.Column="3" Grid.ColumnSpan="2" Click="ResultButton_Click"/>57 58         </Grid>

 

结果

设置input TextBox,Ouput TextBox,background属性为transparent,使得显示效果似乎是一个TextBox

 1 <Grid> 2             <Grid.Resources> 3                 <Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}"> 4                     <Setter Property="BorderThickness" Value="0"/> 5                     <Setter Property="BorderBrush" Value="Transparent"/> 6                     <Setter Property="Background" Value="Transparent"/> 7                     <Setter Property="VerticalAlignment" Value="Center"/> 8                     <Setter Property="Margin" Value="5,0"/> 9                 </Style>10             </Grid.Resources>11             <Grid.RowDefinitions>12                 <RowDefinition />13                 <RowDefinition />14             </Grid.RowDefinitions>15             <Border Grid.RowSpan="2"/>16             <Grid>17                 <Grid.ColumnDefinitions>18                     <ColumnDefinition />19                     <ColumnDefinition Width="70"/>20                 </Grid.ColumnDefinitions>21                 <TextBox Text="{Binding Input}" FontSize="13.3" CaretBrush="#FF646400" TextChanged="TextBox_TextChanged"/>22                 <Button x:Name="ClearButton" Background="Salmon" Grid.Column="1" Content="×" Click="ClearButton_Click"/>23             </Grid>24             <Grid Grid.Row="1">25                 <Grid.ColumnDefinitions>26                     <ColumnDefinition />27                     <ColumnDefinition Width="70"/>28                 </Grid.ColumnDefinitions>29                 <TextBox IsReadOnly="True" Text="{Binding Output}" FontSize="20" TextAlignment="Right"/>30                 <Button x:Name="ACButton" Grid.Column="1" Background="#FF00A000" Content="AC" Click="ACButton_Click"/>31             </Grid>32         </Grid>

 

对Input表达式求值和返回错误信息(如果存在)

  1 class CalResult  2         {  3             public double Result { get; set; }  4             public bool IsSucc { get; set; }  5             public string Note { get; set; }  6         }  7         CalResult CalByStr(string s)  8         {  9  10             var r = new CalResult { IsSucc = false }; 11  12             int[] st = new int[1000]; 13             int[] c = new int[1000]; 14             double[] a = new double[1000]; 15             int n = 0, p = 0, f = 0, i = 0, j = 0, x = 0, y = 0; 16  17             st[+] = st[-] = 1; 18             st[*] = st[/] = 2; 19  20             if (string.IsNullOrWhiteSpace(s)) 21             { 22                 r.Note = "空白的输入"; 23                 return r; 24             } 25  26             for (i = 0; i < s.Length; i++) while (i < s.Length && s[i] ==  ) s = s.Remove(i, 1); 27  28  29             //删除无用的空格; 30  31             n = s.Length; 32             for (i = 0; i < n; i++) if (s[i] > 9 || s[i] < ( || s[i] == ,) 33                 { 34                     r.Note = "无效表达式!表达式中包含计算器无法识别的字符,请注意使用英文符号"; 35                     return r; 36                 } 37  38             //以上完成对表达式的基本检查 39  40             for (i = 0; i < n; i++) 41             { 42                 if (s[i] == )) 43                 { 44                     if (x == 0) 45                     { 46                         r.Note = "无效表达式!  括号不匹配,请核对你的输入"; 47                         return r; 48                     } 49                     --x; 50                 } 51                 if (s[i] == () ++x; 52             } 53  54             if (x != 0) 55             { 56                 r.Note = "无效表达式!  括号不匹配,请核对你的输入"; 57                 return r; 58             } 59  60             //以上完成检查括号是否匹配 61  62  63  64             for (i = 0; i < s.Length; i++) if (s[i] == .) 65                     if (i == 0 || !char.IsDigit(s[i - 1])) s = s.Insert(i, "0"); 66                     else if (i + 1 == s.Length || !char.IsDigit(s[i + 1])) s = s.Insert(i + 1, "0"); 67             //        . , .x , x. 有效输入 并为其扩充值 68  69  70             for (i = 0; i < s.Length; i++) if (isas(s[i])) 71                 { 72                     int t = i + 1; 73                     while (t < s.Length && isas(s[t])) 74                         if (s[t] == -) 75                         { 76                             if (s[i] == +) s = s.Remove(i, 1); 77                             else if (s[i] == -) 78                             { 79                                 s = s.Insert(i, "+"); 80                                 s = s.Remove(i + 1, 2); 81                             } 82                         } 83                         else s = s.Remove(t, 1); 84                 } 85  86             //处理多余的+和-,加和减作为正负号处理 87  88  89  90             for (i = 0; i < s.Length; i++) 91                 if (isas(s[i]) && (i == 0 || s[i - 1] == ()) s = s.Insert(i, "0"); 92  93             //对于 -3,+3  处理为 0-3,0+3; 94  95  96             for (i = 1; i < s.Length; i++) 97             { 98                 if (s[i] == ( && (isd(s[i - 1]) || s[i - 1] == ))) s = s.Insert(i, "*"); 99                 if (s[i] == ) && i + 1 < s.Length && isd(s[i + 1])) s = s.Insert(i + 1, "*");100                 if (s[i] == ) && s[i - 1] == ()101                 {102                     r.Note = "无效表达式!  ()内缺少内容,请核对你的输入";103                     return r;104                 }105             }106 107             //处理括号问题  1.()为无效输入;2.  )(   )x  x( 为有效输入 等效为 )*(  )*x  x*(;108 109 110             n = s.Length;111 112             if (s[0] == * || s[0] == /)113             {114                 r.Note = "无效表达式!  乘和除出现在第一位,请核对你的输入";115                 return r;116             }117             //乘和除出现在第一位 为无效输入118 119 120             for (i = 1; i < n; i++)121                 if (s[i] == .)122                 {123                     p = 0;124 125                     for (j = i - 1; j > 0; j--) if (s[j] == .)126                         {127                             p = j;128                             break;129                         }130 131                     for (j = p + 1; j < i; j++) if (isf(s[j]))132                             p = 0;133 134                     if (p != 0)135                     {136                         r.Note = "无效表达式!  同一个数字包含多个小数点,请核对你的输入";137                         return r;138                     }139                 }140                 // 一个数字包含超过1个.为无效输入141 142                 else if (isf(s[i]))143                 {144                     p = 1;145                     if (i + 1 == n) p = 0;146                     if (!(s[i - 1] >= 0 && s[i - 1] <= 9 || s[i - 1] == ))) p = 0;147                     if (i + 1 < n) if (!(s[i + 1] >= 0 && s[i + 1] <= 9 || s[i + 1] == ()) p = 0;148                     if (p == 0)149                     {150                         r.Note = "无效表达式!  表达式中出现连续的运算符,请核对你的输入";151                         return r;152                     }153                 }154 155             //  表达式中出现连续的运算符为无效输入156 157 158 159 160             //以上完成对表达式的检验和标准化矫正161 162 163 164             //以下为对表达式的求值过程165 166 167             for (i = 0; i < n; i++)168                 if (s[i] < 0 && s[i] != .)169                 {170                     if (s[i] != ()171                     {172                         if (isd(s[i - 1])) double.TryParse(s.Substring(f, i - f), out a[++x]);173                         if (s[i] == ))174                         {175                             while (c[y] != ()176                             {177                                 --x;178                                 a[x] = cal(a[x], a[x + 1], c[y]);179                                 --y;180                             }181                             y--;182                         }183                         else184                         {185                             while (st[c[y]] >= st[s[i]])186                             {187                                 --x;188                                 a[x] = cal(a[x], a[x + 1], c[y]);189                                 --y;190                             }191                             c[++y] = s[i];192                         }193                     }194                     else c[++y] = s[i];195                     f = i + 1;196                 }197 198 199             if (s[i - 1] != )) double.TryParse(s.Substring(f, i - f), out a[++x]);200 201             //若表达式末尾不为右括号,则可能还存在一个数字没有处理202 203 204             while (y > 0)205             {206                 --x;207                 a[x] = cal(a[x], a[x + 1], c[y]);208                 --y;209             }210 211             //以上完成对表达式的求值212             r.IsSucc = true;213             r.Result = a[1];214             return r;215 216             //显示表达式的值217         }
View Code

 

 

源solution

 http://files.cnblogs.com/lightz/calculartor.zip

一个简单的Windows计算器