一、界面设计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. C++学习3

    C++仍然在使用C语言的 char.int.long 等基本数据类型,它们在现代操作系统(Windows XP.Win7.Win10 等)中的长度如下表所示: longlong是C99新增的一种数据类 ...

  2. MS SQL SERVER 数据库日志压缩方法与代码

    MS SQL性能是很不错的,但是数据库用了一段时间之后,数据库却变得很大,实际的数据量不大.一般都是数据库日志引起的!数据库日志的增长可以达到好几百M. DUMP TRANSACTION [数据库名] ...

  3. LaTex学习笔记

    常见符号大全:http://ia.wikipedia.org/wiki/Wikipedia:LaTeX_symbols 字体加粗: \textbf{} 换页:\newpage 大于等于,小于等于    ...

  4. python 调用内部类的两种方法

    class Car:#外部类 class Door:#内部类 def open(self): print('open door') class Wheel: def run(self): print( ...

  5. HDU1116图论

    http://acm.split.hdu.edu.cn/showproblem.php?pid=1116 #include<stdio.h> #include<algorithm&g ...

  6. python学习(二):python基本语法

    前言:python基本的语法与其他语言诸如C,JAVA等类似,但个中有些许不同. 一.常规语法 1.变量名与关键字 与其他语言类似,变量名由字母.数字.下划线组成,且必须由字母开头. 变量使用不需要提 ...

  7. 初探appium之appium的使用

    上一篇中已经讲了python+appium的环境搭建.这里简单的讲一下appium的使用. 我也是第一次使用appium,看了教程问了人.知道appium可以通过模拟也可以连接上手机使用.本篇中,先使 ...

  8. python基础set

    1.set set是一个无序的不重复的集合 li=[11,22,33,11] s=set(li) print(s) {11,22,33}  set提供的方法 1.add(self, *args, ** ...

  9. 默菲定律 [Murphy's Law]

    一.关于默菲定律(Murphy's Law)   “墨菲定律”.“帕金森定律”和“彼德原理”并称为二十世纪西方文化三大发现. “墨菲定律”的原话是这样说的:If there are two or mo ...

  10. apache中若干模块的安装

    第一次手动安装apache,由于在./configure -prefix=/usr/local/apache2 -enable-module=so这样配置,导致后来不得不手动安装一些模块,遇到了一些坑 ...