一、界面设计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 > &&le2>)
{
jiguo.Text = jiguo.Text.Substring(, le - );
gongshi.Text = gongshi.Text.Substring(, le2 - );
}
}
}
}

一个用WPF做的简单计算器源代码的更多相关文章

  1. PHP做的简单计算器

    使用php做的简易计算器 能够进行+,-,*,/运算. 如下图 <?php if (isset($_POST['button'])) { $num1 = $_POST['num1']; $num ...

  2. 每天2个android小例子----简单计算器源代码

    通过Android4.0 网格布局GridLayout来实现一个简单的计算器界面布局 package com.android.xiong.gridlayoutTest; import java.mat ...

  3. Android之一个简单计算器源代码

    通过Android4.0 网格布局GridLayout来实现一个简单的计算器界面布局   源码如下(欢迎大家指导 批评 ) package com.android.xiong.gridlayoutTe ...

  4. 【C#】简单计算器源代码

    form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.D ...

  5. 平时没有怎么用Excel做 加减乘除 计算,猛地发现,其实Excel 是一个很好的简单计算器

    平时没有怎么用Excel做 加减乘除 计算,猛地发现,其实Excel 是一个很好的简单计算器

  6. 基于MFC的一个简单计算器

    写一个简单的计算器并不是什么很难的事,主要目的是要通过这个程序来学习和分析其中的核心算法.这个简易计算器的核心部分就是对输入的表达式的正确性判断与求值,其中包括对表达式的解析.中缀表达式转后缀表达式. ...

  7. WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体

    原文:WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体 运行结果: 事实上很简单,定义好一个正方体,处理好纹理.关于MeshGeometry3D的正确定义和纹理这里就不多讲 ...

  8. 菜鸟学Android编程——简单计算器《一》

    菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少 ...

  9. TypeC一个微软开发的超简单.NET依赖注入/IoC容器

    控制反转(IoC,Inversion of Control)是由Martin Fowler总结出来的一种设计模式,用来减少代码间的耦合.一般而言,控制反转分为依赖注入(Dependency Injec ...

随机推荐

  1. java:jdk环境变量配置+tomcat环境变量配置

    一:JDK1.先安装jdk  查看jdk版本:   DOC下输入java -version2.配置环境变量(添加系统变量)  JAVA_HOME      D:\study\jdk-6\jdk-6(j ...

  2. [SQL]分组排练进行更新

    --方法(一):分组排练进行更新 ----------------------------------------------------------------------------------- ...

  3. [SQL]select scope_identity()传回插入相同范围之识别资料行中的最后一个识别值

    传回插入相同范围之识别资料行中的最后一个识别值.范围是一个模组:预存程序.触发程序.函数或批次.因此,如果两个陈述式在相同预存程序.函数或批次中,它们就在相同范围中. 语法: SCOPE_IDENTI ...

  4. 用浏览器打开本地html 直接到首页 的解决方法

    方法1:直接把本地文件拖到浏览器就可以打开

  5. WPF更新数据源

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windo ...

  6. Package 'DXCore for Visual Studio' has failed to load properly

    Since installing 13.1  I get Package 'DXCore for Visual Studio' has failed to load properly error wh ...

  7. 简单的python http接口自动化脚本

    今天给大家分享一个简单的Python脚本,使用python进行http的接口测试,脚本很简单,逻辑是:读取excel写好的测试用例,然后根据excel中的用例内容进行调用,判断预期结果中的返回值是否和 ...

  8. 从AlphaGo谈通用型人工智能设计

    最近赢了人机大战的AlphaGo火了,火得一塌糊涂,圈里圈外,是人都在谈AlphaGo.但是AlphaGo毕竟是为特定场景特定应用设计的特定型人工智能,和通用型人工智能还是有很大差别,离人工智能普及更 ...

  9. Unity协程(Coroutine)原理深入剖析(转载)

    记得去年6月份刚开始实习的时候,当时要我写网络层的结构,用到了协程,当时有点懵,完全不知道Unity协程的执行机制是怎么样的,只是知道函数的返回值是IEnumerator类型,函数中使用yield r ...

  10. PMP考试--成本管理中常用的概念

    如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 净现值(NPV)   Net Present Value 在项目计算期内,按行业基准 ...