一个用WPF做的简单计算器源代码_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 一个用WPF做的简单计算器源代码

一个用WPF做的简单计算器源代码

 2013/8/23 20:59:07  奉沈  博客园  我要评论(0)
  • 摘要:一、界面设计XAML代码<Windowx:Class="fengjisuanqi.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="我的简单计算器"Height="700"Width="480"ResizeMode="CanMinimize"><
  • 标签:一个 代码

一、界面设计XAML代码

<Window x:Class="fengjisuanqi.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="我的简单计算器" Height="700" Width="480" ResizeMode="CanMinimize">
    <Grid Margin="0,0,0,0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>
        <!--
            <StackPanel Grid.Row="0" Grid.ColumnSpan="4" Margin="0">
                <TextBlock Name="gongshi" Height="Auto" Margin="0,10,0,0" Background="#FF5D4D4D" HorizontalAlignment="Right" FontSize="36" Width="470" Foreground="White"/>
            <TextBlock Name="jiguo" Height="80" Margin="0" Background="#ff5d4d4d" HorizontalAlignment="Right" Width="470" FontSize="36" Foreground="White"></TextBlock>
        </StackPanel>
        -->
        <TextBlock Name="ss" Grid.Row="0" Grid.ColumnSpan="4" Width="470" Height="Auto" Margin="0,0,0,0" Background="#FF5D4D4D" HorizontalAlignment="Right" FontSize="36" Foreground="White"/>
        <TextBlock Name="gongshi" Grid.Row="0" Grid.ColumnSpan="4" Height="Auto" Margin="0,0,0,0" Background="#FF5D4D4D" HorizontalAlignment="Right" FontSize="36" Foreground="White"/>
        <TextBlock Name="jiguo" Grid.Row="0" Grid.ColumnSpan="4" Height="60" Margin="0" Background="#FF5D4D4D" HorizontalAlignment="Right" FontSize="36" Foreground="White"/>
        <Button Content="7" FontSize="50" Grid.Row="2" Grid.Column="0" Click="Button_Click_1" Name="B7" />
        <Button Content="8" FontSize="50" Grid.Row="2" Grid.Column="1" Click="Button_Click_1" Name="B8" />
        <Button Content="9" FontSize="50" Grid.Row="2" Grid.Column="2" Click="Button_Click_1" Name="B9" />
        <Button Content="4" FontSize="50" Grid.Row="3" Grid.Column="0" Click="Button_Click_1" Name="B4" />
        <Button Content="5" FontSize="50" Grid.Row="3" Grid.Column="1" Click="Button_Click_1" Name="B5" />
        <Button Content="6" FontSize="50" Grid.Row="3" Grid.Column="2" Click="Button_Click_1" Name="B6" />
        <Button Content="1" FontSize="50" Grid.Row="4" Grid.Column="0" Click="Button_Click_1" Name="B1" />
        <Button Content="2" FontSize="50" Grid.Row="4" Grid.Column="1" Click="Button_Click_1" Name="B2" />
        <Button Content="3" FontSize="50" Grid.Row="4" Grid.Column="2" Click="Button_Click_1" Name="B3" />
        <Button Content="." FontSize="50" Grid.Row="5" Grid.Column="0" Click="Button_Click_1" Name="B" />
        <Button Content="0" FontSize="50" Grid.Row="5" Grid.Column="1" Click="Button_Click_1" Name="B0" />
        <Button Name="divide" FontSize="50" Content="/" Grid.Column="3" Grid.Row="2" Click="fuhao_Click_1"/>
        <Button Name="Multiply" FontSize="50" Content="*" Grid.Column="3" Grid.Row="3" Click="fuhao_Click_1"/>
        <Button Name="Minus" FontSize="50" Content="-" Grid.Column="3" Grid.Row="4" Click="fuhao_Click_1"/>
        <Button Name="Add" FontSize="50" Content="+" Grid.Column="3" Grid.Row="5" Click="fuhao_Click_1"/>
        <Button Name="result" FontSize="50" Grid.Column="2" Grid.Row="5" Click="result_Click_1" Content="="/>
        <Button Name="del" FontSize="30" Grid.Column="3" Grid.Row="1" Content="CE" Click="del_Click_1" Background="#FFEE0E0E"/>
        <Button Name="tuige" FontSize="30" Grid.Column="2" Grid.Row="1" Content="退格" Click="tuige_Click_1"/>
        <Rectangle Name="rect" Grid.Row="1" Grid.ColumnSpan="2">
            <Rectangle.Fill>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#FF9DA49F" Offset="0"/>
                    <GradientStop Color="White" Offset="1"/>
                </LinearGradientBrush>
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
</Window>

二、效果图

三、后台逻辑代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace fengjisuanqi
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private string anxiazhi = "";
        private double resultnum = 0.0;
        public MainWindow()
        {
            InitializeComponent();
        }
        //运算方法
        private void OperationNum(string s)
        {
            if (jiguo.Text != "")
            {
                switch (anxiazhi)
                {
                    case "": resultnum = double.Parse(jiguo.Text);
                        anxiazhi = s;
                        break;
                    case "+": resultnum = resultnum + double.Parse(jiguo.Text);
                        anxiazhi = s;
                        break;
                    case "-": resultnum = resultnum - double.Parse(jiguo.Text);
                        anxiazhi = s;
                        break;
                    case "*": resultnum = resultnum * double.Parse(jiguo.Text);
                        anxiazhi = s;
                        break;
                    case "/": if (double.Parse(jiguo.Text) != 0.0)
                        {
                            resultnum = resultnum / double.Parse(jiguo.Text);
                        }
                        else
                        {
                            resultnum = 0.0;
                        }
                        anxiazhi = s;
                        break;
                    default: break;
                }
            }
            else
            {
                anxiazhi = s;
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (anxiazhi == "=")
            {
                gongshi.Text = "";
                jiguo.Text = "";
                anxiazhi = "";
                resultnum = 0.0;
            }
            string s = ((Button)sender).Content.ToString();
            jiguo.Text = jiguo.Text + s;
            gongshi.Text = gongshi.Text + s;
        }
        //按运算符号的事件处理
        private void fuhao_Click_1(object sender, RoutedEventArgs e)
        {
            if (anxiazhi == "=")
            {
                gongshi.Text = jiguo.Text;
                anxiazhi = "";
            }
            string s = ((Button)sender).Content.ToString();//获得按钮文本内容
            gongshi.Text = gongshi.Text + s;
            OperationNum(s);
            jiguo.Text = "";
        }
        //按“=”号计算结果
        private void result_Click_1(object sender, RoutedEventArgs e)
        {
            OperationNum("=");
            jiguo.Text = resultnum.ToString();
        }
        //清除操作
        private void del_Click_1(object sender, RoutedEventArgs e)
        {
            jiguo.Text = "";
            gongshi.Text = "";
            anxiazhi = "";
            resultnum = 0.0;
        }
        //退格
        private void tuige_Click_1(object sender, RoutedEventArgs e)
        {
            //获取字符串长度
            int le = jiguo.Text.Length;
            int le2 = gongshi.Text.Length;
            if (le > 1&&le2>1)
            {
                jiguo.Text = jiguo.Text.Substring(0, le - 1);
                gongshi.Text = gongshi.Text.Substring(0, le2 - 1);
            }
        }
    }
}

 

上一篇: 世界上最准确的镱原子钟 下一篇: 没有下一篇了!
发表评论
用户名: 匿名