一个用WPF做的简单计算器源代码
一、界面设计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做的简单计算器源代码的更多相关文章
- PHP做的简单计算器
使用php做的简易计算器 能够进行+,-,*,/运算. 如下图 <?php if (isset($_POST['button'])) { $num1 = $_POST['num1']; $num ...
- 每天2个android小例子----简单计算器源代码
通过Android4.0 网格布局GridLayout来实现一个简单的计算器界面布局 package com.android.xiong.gridlayoutTest; import java.mat ...
- Android之一个简单计算器源代码
通过Android4.0 网格布局GridLayout来实现一个简单的计算器界面布局 源码如下(欢迎大家指导 批评 ) package com.android.xiong.gridlayoutTe ...
- 【C#】简单计算器源代码
form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.D ...
- 平时没有怎么用Excel做 加减乘除 计算,猛地发现,其实Excel 是一个很好的简单计算器
平时没有怎么用Excel做 加减乘除 计算,猛地发现,其实Excel 是一个很好的简单计算器
- 基于MFC的一个简单计算器
写一个简单的计算器并不是什么很难的事,主要目的是要通过这个程序来学习和分析其中的核心算法.这个简易计算器的核心部分就是对输入的表达式的正确性判断与求值,其中包括对表达式的解析.中缀表达式转后缀表达式. ...
- WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体
原文:WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体 运行结果: 事实上很简单,定义好一个正方体,处理好纹理.关于MeshGeometry3D的正确定义和纹理这里就不多讲 ...
- 菜鸟学Android编程——简单计算器《一》
菜鸟瞎搞,高手莫进 本人菜鸟一枚,最近在学Android编程,网上看了一些视频教程,于是想着平时手机上的计算器应该很简单,自己何不尝试着做一个呢? 于是就冒冒失失的开撸了. 简单计算器嘛,功能当然很少 ...
- TypeC一个微软开发的超简单.NET依赖注入/IoC容器
控制反转(IoC,Inversion of Control)是由Martin Fowler总结出来的一种设计模式,用来减少代码间的耦合.一般而言,控制反转分为依赖注入(Dependency Injec ...
随机推荐
- IDEA Community(社区版) 使用Maven创建Web工程 并部署tomcat
由于IDEA社区版(Community)无法直接New一个Web Appplication 所以要使用maven来创建 1.创建一个Project 2. 3. 4.这里在Properties中添加一个 ...
- springmvc的3中路径风格
1.导入相应的jar包,文件放置情况 2.web.xml <?xml version="1.0" encoding="UTF-8"?> <we ...
- spi驱动无法建立spidev问题
参考这里: http://e2e.ti.com/support/arm/sitara_arm/f/791/t/168122.aspx http://communistcode.co.uk/blog/b ...
- js捕获回车事件,并且获取每一条输入内容
<body> <div style="width: 200px; height: 20px;"> <textarea id="inputVa ...
- 紧张:飞测独家のJmeter秘籍,限量发放
飞测说:数月前,小怪我牺牲了周末时间,做了fiddler的扩展开发,从fiddler将请求导出,保存为jmx格式的文件,然后使用jmeter来调用.随后,小怪和同事(心&阳)一同研究jmete ...
- Drupal8开发教程:模块开发——创建新页面
之前我们已经通过<Drupal8开发教程:认识.info.yml文件>对模块的YAML文件有了了解,今天我们来看如何通过模块开发的方式添加一个新的页面. 在 Drupal 7 中,通过模块 ...
- Drupal常用开发工具(一)——Devel模块
进行 Drupal 开发时有许多模块和工具可供使用,其中最常用的两项便是 Devel 及 Drupal for Firebug.本文和<Drupal常用开发工具(二)——Drupal for F ...
- The str method
__str__ is a special method name, like __init__, that is supposed to return a string representation ...
- NSString用法
1 创造字符串 NSString *str1 = @"hello"; NSString *str2 = [NSString string]; NSString *str3 = [N ...
- 《App研发录》知识点汇总
原文链接:http://www.jianshu.com/p/fc8c4638937e <App研发录>这部书是包建强写的,说来也巧,在读这边书之前在看池建强的<Mac 人生元编程&g ...