2014-02-13 92 views
0

我目前在C#中做了一个非常简单的计算器程序,我的程序工作正常,因为在加,减,乘和除功能工作正常,虽然我遇到的问题是十进制点无法正常工作。错误是当我用小数添加两个数字时,它无法正常工作。 也就是说,如果我加20和.5,结果将是20.5,但如果我加20和1.5,答案也是20.5。我相信这是因为,由于我写代码的方式,计算器会忽略1.5中的数字1(请参阅我的代码,因为我相信你们可能知道如何解决这个问题)。任何帮助解决这个问题,将不胜感激。C#计算器程序#

(MainPage.xaml中的代码)

<phone:PhoneApplicationPage 
x:Class="Calculator.MainPage" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768" 
FontFamily="{StaticResource PhoneFontFamilyNormal}" 
FontSize="{StaticResource PhoneFontSizeNormal}" 
Foreground="{StaticResource PhoneForegroundBrush}" 
SupportedOrientations="Portrait" Orientation="Portrait" 
shell:SystemTray.IsVisible="True"> 

<!--LayoutRoot is the root grid where all page content is placed--> 
<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 

    <!--TitlePanel contains the name of the application and page title--> 
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"> 
     <TextBlock x:Name="ApplicationTitle" Text="Jason Lynch" Style="{StaticResource PhoneTextNormalStyle}"/> 
     <TextBlock x:Name="PageTitle" Text="Calculator" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/> 
    </StackPanel> 

    <!--ContentPanel - place additional content here--> 
    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> 
     <Button Content="=" Height="116" Margin="218,485,0,0" Name="btnEquals" VerticalAlignment="Top" HorizontalAlignment="Left" Width="127" Background="#FFFF550D" FontSize="56" Click="btnEquals_Click" /> 
     <Button Content="-" Height="116" HorizontalAlignment="Center" Margin="323,485,6,0" Name="btnSubtract" VerticalAlignment="Top" Width="127" FontSize="56" Padding="0,-25,0,0" Click="btnSubtract_Click" /> 
     <Button Content="0" Height="116" HorizontalAlignment="Left" Margin="6,485,0,0" Name="btn0" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn0_Click" /> 
     <Button Content="Clear" Height="73" HorizontalAlignment="Left" Margin="112,485,0,0" Name="btnClear" VerticalAlignment="Top" Width="127" FontSize="28" Background="#FF0008FF" Click="btnClear_Click"></Button> 
     <Button Content="3" Height="116" HorizontalAlignment="Right" Margin="0,389,111,0" Name="btn3" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn3_Click" /> 
     <Button Content="+" Height="116" HorizontalAlignment="Left" Margin="323,389,0,0" Name="btnAdd" VerticalAlignment="Top" Width="127" FontSize="56" Click="btnAdd_Click" /> 
     <Button Content="1" Height="116" HorizontalAlignment="Left" Margin="6,389,0,0" Name="btn1" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn1_Click" /> 
     <Button Content="2" Height="116" HorizontalAlignment="Left" Margin="112,389,0,0" Name="btn2" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn2_Click" /> 
     <Button Content="6" Height="116" HorizontalAlignment="Right" Margin="0,294,111,0" Name="btn6" VerticalAlignment="Top" Width="127" Click="button9_Click" FontSize="56" /> 
     <Button Content="x" Height="116" HorizontalAlignment="Right" Margin="0,294,6,0" Name="btnMultiply" VerticalAlignment="Top" Width="127" FontSize="56" Click="btnMultiply_Click" /> 
     <Button Content="4" Height="116" HorizontalAlignment="Left" Margin="6,294,0,0" Name="btn4" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn4_Click" /> 
     <Button Content="5" Height="116" HorizontalAlignment="Left" Margin="112,294,0,0" Name="btn5" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn5_Click" /> 
     <Button Content="9" Height="116" HorizontalAlignment="Left" Margin="218,199,0,0" Name="btn9" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn9_Click" /> 
     <Button Content="÷" Height="116" HorizontalAlignment="Left" Margin="323,199,0,0" Name="btnDivide" VerticalAlignment="Top" Width="127" FontSize="56" Click="btnDivide_Click" /> 
     <Button Content="7" Height="116" HorizontalAlignment="Left" Margin="6,199,0,0" Name="btn7" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn7_Click" /> 
     <Button Content="8" Height="116" HorizontalAlignment="Left" Margin="112,199,0,0" Name="btn8" VerticalAlignment="Top" Width="127" FontSize="56" Click="btn8_Click" /> 
     <TextBox Height="72" HorizontalAlignment="Left" Margin="0,65,0,0" Name="resultTxtBox" Text="" VerticalAlignment="Top" Width="460" /> 
     <TextBlock Height="53" HorizontalAlignment="Left" Margin="169,26,0,0" Name="textBlock1" Text="Result" VerticalAlignment="Top" FontSize="36" /> 
     <Button Content="." Height="68" HorizontalAlignment="Left" Margin="112,533,0,0" Name="btnDecimalPoint" VerticalAlignment="Top" Width="127" Click="btnDecimalPoint_Click" HorizontalContentAlignment="Center" /> 
    </Grid> 
</Grid> 

<!--Sample code showing usage of ApplicationBar--> 
<!--<phone:PhoneApplicationPage.ApplicationBar> 
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 
     <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/> 
     <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/> 
     <shell:ApplicationBar.MenuItems> 
      <shell:ApplicationBarMenuItem Text="MenuItem 1"/> 
      <shell:ApplicationBarMenuItem Text="MenuItem 2"/> 
     </shell:ApplicationBar.MenuItems> 
    </shell:ApplicationBar> 
</phone:PhoneApplicationPage.ApplicationBar>--> 

(MainPage.xaml.cs中的代码)

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Net; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Documents; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using System.Windows.Media.Animation; 
    using System.Windows.Shapes; 
    using Microsoft.Phone.Controls; 

    namespace Calculator 
    { 
     public partial class MainPage : PhoneApplicationPage 
     { 
      // Constructor 
      public MainPage() 
      { 
       InitializeComponent(); 
      } 

      private double total1 = 0; 
      private double total2 = 0; 
      bool plusButtonClicked = false; 
      bool minusButtonClicked = false; 
      bool multiplyButtonClicked = false; 
      bool divideButtonClicked = false; 


      private void btn0_Click(object sender, RoutedEventArgs e) 
      { 
       resultTxtBox.Text = resultTxtBox.Text + "0"; 
      } 

      private void btn1_Click(object sender, RoutedEventArgs e) 
      { 
       resultTxtBox.Text = resultTxtBox.Text + "1"; 
      } 

      private void btn2_Click(object sender, RoutedEventArgs e) 
      { 
       resultTxtBox.Text = resultTxtBox.Text + "2"; 
      } 

      private void btn3_Click(object sender, RoutedEventArgs e) 
      { 
       resultTxtBox.Text = resultTxtBox.Text + "3"; 
      } 

      private void btn4_Click(object sender, RoutedEventArgs e) 
      { 
       resultTxtBox.Text = resultTxtBox.Text + "4"; 
      } 

      private void btn5_Click(object sender, RoutedEventArgs e) 
      { 
       resultTxtBox.Text = resultTxtBox.Text + "5"; 
      } 

      //MESSED UP BUTTON *** button9 == btn6 
      private void button9_Click(object sender, RoutedEventArgs e) 
      { 
       resultTxtBox.Text = resultTxtBox.Text + "6"; 
      } 

      private void btn7_Click(object sender, RoutedEventArgs e) 
      { 
       resultTxtBox.Text = resultTxtBox.Text + "7"; 
      } 

      private void btn8_Click(object sender, RoutedEventArgs e) 
      { 
       resultTxtBox.Text = resultTxtBox.Text + "8"; 
      } 

      private void btn9_Click(object sender, RoutedEventArgs e) 
      { 
       resultTxtBox.Text = resultTxtBox.Text + "9"; 
      } 

      private void btnAdd_Click(object sender, RoutedEventArgs e) 
      { 
       total1 = total1 += double.Parse(resultTxtBox.Text); 
       resultTxtBox.Text = ""; 

       plusButtonClicked = true; 
       minusButtonClicked = false; 
       multiplyButtonClicked = false; 
       divideButtonClicked = false; 
      } 

      private void btnSubtract_Click(object sender, RoutedEventArgs e) 
      { 
       total1 = total1 + double.Parse(resultTxtBox.Text); 
       resultTxtBox.Text = ""; 

       plusButtonClicked = false; 
       minusButtonClicked = true; 
       multiplyButtonClicked = false; 
       divideButtonClicked = false; 
      } 

      private void btnMultiply_Click(object sender, RoutedEventArgs e) 
      { 
       total1 = total1 + double.Parse(resultTxtBox.Text); 
       resultTxtBox.Text = ""; 

       plusButtonClicked = false; 
       minusButtonClicked = false; 
       multiplyButtonClicked = true; 
       divideButtonClicked = false; 
      } 

      private void btnDivide_Click(object sender, RoutedEventArgs e) 
      { 
       total1 = total1 + double.Parse(resultTxtBox.Text); 
       resultTxtBox.Text = ""; 

       plusButtonClicked = false; 
       minusButtonClicked = false; 
       multiplyButtonClicked = false; 
       divideButtonClicked = true; 
      } 

      private void btnEquals_Click(object sender, RoutedEventArgs e) 
      { 
       if (plusButtonClicked == true) 
       { 
        total2 = total1 + double.Parse(resultTxtBox.Text); 
        resultTxtBox.Text = total2.ToString(); 
        total1 = 0; 
       } 
      else if (minusButtonClicked == true) 
      { 
        total2 = total1 - double.Parse(resultTxtBox.Text); 
        resultTxtBox.Text = total2.ToString(); 
        total1 = 0; 
      } 
      else if (multiplyButtonClicked == true) 
      { 
        total2 = total1 * double.Parse(resultTxtBox.Text); 
        resultTxtBox.Text = total2.ToString(); 
        total1 = 0; 
      } 
      else if (divideButtonClicked == true) 
      { 
        total2 = total1/double.Parse(resultTxtBox.Text); 
        resultTxtBox.Text = total2.ToString(); 
        total1 = 0; 
      } 
     } 

     private void btnClear_Click(object sender, RoutedEventArgs e) 
     { 
       resultTxtBox.Text = ""; 
     } 

     private void btnDecimalPoint_Click(object sender, RoutedEventArgs e) 
     { 
       resultTxtBox.Text = "."; 
     } 

    } 
} 

最后,这里是我的计算器应用程序的撷取画面>>http://gyazo.com/717b396e32b39d4a42e91ad9cf67cdef

感谢您提前提供帮助, Jason。

+0

这似乎是有关DecimalPoint_Click方法。我会尽力回复 – shingonati0n

+0

漂亮的应用程序! – Kevin

回答

3

你必须改变

private void btnDecimalPoint_Click(object sender, RoutedEventArgs e) 
{ 
    resultTxtBox.Text = ","; 
} 

private void btnDecimalPoint_Click(object sender, RoutedEventArgs e) 
{ 
    resultTxtBox.Text += ","; 
} 
+0

感谢您的帮助Akrem,this是我需要的修复:) – JasonL95

2

首先,你不必写

resultTxtBox.Text = resultTxtBox.Text + "8"; 
每次

。简单使用

resultTxtBox.Text += "8"; 

这是一样的。在我看来,它更具可读性。

其次,线

total1 = total1 += double.Parse(resultTxtBox.Text); 

是多余的。

total1 += double.Parse(resultTxtBox.Text); 

是相同的结果。 ;)

第三,对于您的小数点的代码读取

private void btnDecimalPoint_Click(object sender, RoutedEventArgs e) 
{ 
    resultTxtBox.Text = "."; 
} 

所以你小数点替换resultTxtBox.Text文本。所以我无法想象如何在第一时间编写1.5,因为一旦您点击小数点按钮,就会从文本框中删除1。

+0

感谢您对需要修复/提炼的代码的建议。虽然在你看来,你如何得到小数点在这个计算器中工作,因为这是我遇到的问题,我一直无法找到小数点按钮的工作修复程序 – JasonL95