Windows Phone 8.1 Calculator 计算器

network | | 访问(40)
<Page
    x:Class="Calculator.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Calculator"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" >

    <Grid x:Name="root" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="4*"/>
            <ColumnDefinition Width="4*"/>
            <ColumnDefinition Width="4*"/>
            <ColumnDefinition Width="5*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="63*" />
            <RowDefinition Height="170*" />
            <RowDefinition Height="119*" />
            <RowDefinition Height="117*" />
            <RowDefinition Height="119*" />
            <RowDefinition Height="117*" />
        </Grid.RowDefinitions>
        <Button x:Name="B7" Click="DigitBtn_Click"   Grid.Column="0" Grid.Row="2" Content="7" Height="80"/>
        <Button x:Name="B8" Click="DigitBtn_Click"   Grid.Column="1" Grid.Row="2" Content="8" Height="80"/>
        <Button x:Name="B9" Click="DigitBtn_Click"   Grid.Column="2" Grid.Row="2" Content="9" Height="80"/>
        <Button x:Name="B4" Click="DigitBtn_Click"   Grid.Column="0" Grid.Row="3" Content="4" Height="80"/>
        <Button x:Name="B5" Click="DigitBtn_Click"   Grid.Column="1" Grid.Row="3" Content="5" Height="80"/>
        <Button x:Name="B6" Click="DigitBtn_Click"   Grid.Column="2" Grid.Row="3" Content="6" Height="80"/>
        <Button x:Name="B1" Click="DigitBtn_Click"   Grid.Column="0" Grid.Row="4" Content="1" Height="80"/>
        <Button x:Name="B2" Click="DigitBtn_Click"   Grid.Column="1" Grid.Row="4" Content="2" Height="80"/>
        <Button x:Name="B3" Click="DigitBtn_Click"   Grid.Column="2" Grid.Row="4" Content="3" Height="80"/>
        <Button x:Name="B0" Click="DigitBtn_Click"   Grid.Column="0" Grid.Row="5" Content="0" Height="80"/>
        <Button x:Name="Plus" Click="OperationBtn_Click"   Grid.Column="3" Grid.Row="2" Content="+" Height="80"/>
        <Button x:Name="Minus" Click="OperationBtn_Click"   Grid.Column="3" Grid.Row="3" Content="-" Height="80"/>
        <Button x:Name="Multiply" Click="OperationBtn_Click"   Grid.Column="3" Grid.Row="4" Content="*" Height="80"/>
        <Button x:Name="Divide" Click="OperationBtn_Click"   Grid.Column="3" Grid.Row="5" Content="/" Height="80"/>
        <Button x:Name="Del"    Grid.Column="2" Grid.Row="5" Content="删除" Click="Del_Click" Height="80"/>
        <Button x:Name="Result"    Grid.Column="1" Grid.Row="5" Content="=" Click="Result_Click" Height="80"/>
        <TextBlock x:Name="OperationResult" FontSize="120" Grid.Row="1" Margin="6,17,10,17" Grid.ColumnSpan="4" HorizontalAlignment="Right"></TextBlock>
        <TextBlock x:Name="InputInformation"  FontSize="25" Grid.Row="0" Margin="6,20,10,11" Grid.ColumnSpan="4"  HorizontalAlignment="Right"></TextBlock>
    </Grid>
</Page>
public sealed partial class MainPage: Page {
    private string Operation = "";
    private int num1 = 0;
    public MainPage() {
        InitializeComponent();
    }

    private void DigitBtn_Click(object sender, RoutedEventArgs e) {
        if (Operation == "=") {
            OperationResult.Text = "";
            InputInformation.Text = "";
            Operation = "";
            num1 = 0;
        }
        string s = ((Button) sender).Content.ToString();
        OperationResult.Text = OperationResult.Text + s;
        InputInformation.Text = InputInformation.Text + s;
    }

    private void OperationBtn_Click(object sender, RoutedEventArgs e) {
        if (Operation == "=") {
            InputInformation.Text = OperationResult.Text;
            Operation = "";

        }
        string s = ((Button) sender).Content.ToString();
        InputInformation.Text = InputInformation.Text + s;
        OperationNum(s);
        OperationResult.Text = "";
    }

    private void Result_Click(object sender, RoutedEventArgs e) {
        OperationNum("=");
        OperationResult.Text = num1.ToString();
    }

    private void Del_Click(object sender, RoutedEventArgs e) {
        OperationResult.Text = "";
        InputInformation.Text = "";
        Operation = "";
        num1 = 0;
    }

    private void OperationNum(string s) {
        if (OperationResult.Text != "") {
            switch (Operation) {
            case "":
                num1 = Int32.Parse(OperationResult.Text);
                Operation = s;
                break;
            case "+":
                num1 = num1 + Int32.Parse(OperationResult.Text);
                Operation = s;
                break;
            case "-":
                num1 = num1 - Int32.Parse(OperationResult.Text);
                Operation = s;
                break;
            case "*":
                num1 = num1 * Int32.Parse(OperationResult.Text);
                Operation = s;
                break;
            case "/":
                if (Int32.Parse(OperationResult.Text) != 0) num1 = num1 / Int32.Parse(OperationResult.Text);
                else num1 = 0;
                Operation = s;
                break;
            default:
                break;
            }
        } else {
            Operation = s;
        }
    }
}