C#中的计算器程序

时间:2014-02-13 11:08:01

标签: c# xaml calculator

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

(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 = ".";
         }

    }
}

最后这是我的计算器应用程序的屏幕截图&gt;&gt; http://gyazo.com/717b396e32b39d4a42e91ad9cf67cdef

先谢谢你的帮助, 杰森。

3 个答案:

答案 0 :(得分:3)

你必须改变

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

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

答案 1 :(得分: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就会从文本框中删除。

答案 2 :(得分:0)

在我的计算器中,我为计算器上的小数点按钮创建了一个事件处理程序:

XAML

    <Button x:Name ="dot" 
     Click="dot_Click"
     Content="."
     Margin="5"
     Grid.Row="5"
     Grid.Column="2"/>

C#代码如下:

 private void dot_Click(object sender, RoutedEventArgs e)
 {
    //If the calculator output already contains a ".", do nothing
    if (resultLabel.Content.ToString().Contains("."))
    { 
        //Do Nothing 
    }
    else
    {
        //Otherwise, append "." to the existing output string 
        resultLabel.Content = $"{resultLabel.Content}.";
    }
}

此外,您不必为每个按钮创建一个新方法。您可以对所有数字使用一种方法,然后传递所单击按钮的名称:

private void number_Click(object sender, RoutedEventArgs e)
{

    int selectedValue = 0;

    if (sender == zero)
        selectedValue = 0;

    // If the number clicked is "1", "sender" will equal "one" 
    if (sender == one)

        // "selectedValue" will be set to 1
        selectedValue = 1;

    if (sender == two)
        selectedValue = 2;
    if (sender == three)
        selectedValue = 3;
    if (sender == four)
        selectedValue = 4;
    if (sender == five)
        selectedValue = 5;
    if (sender == six)
        selectedValue = 6;
    if (sender == seven)
        selectedValue = 7;
    if (sender == eight)
        selectedValue = 8;
    if (sender == nine)
        selectedValue = 9;

    // If "resultLabel" has not been set to any value already, set it to "selectedValue".
    if (resultLabel.Content.ToString() == "0")
    {
        resultLabel.Content = $"{selectedValue}";
    }
    // Otherwise append "selectedValue" to the existing number.
    else
    {
        resultLabel.Content = $"{resultLabel.Content}{selectedValue}";
    }

    // Note: Anytime this method is called, the "selectedValue" will be set to 0. So, if you typed 7 and then clicked + and then typed 77, the "selectedValue" would equal 0 before you clicked 7 and then 0 again before you clicked 77.  

}

https://www.hannahsky86.com/2020/03/01/2020-03-01/ 该链接提供了更好的解释。