一个用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 ...
随机推荐
- relative和absolute的效果
我对这样几个效果不是特别理解: 1.float的效果: 就是搞不清楚我想要什么效果的时候可以将某个标签设置为float,一直没总结出什么规律. 2.relative和absolute的效果: 也是不清 ...
- Angularjs路由.让人激动的技术.真给前端长脸了.
先看文件的摆放 不废话,直接上代码. detail.html: <hr/> <h3>路由 <span style="color: red;">{ ...
- delphi TServerSocket阻塞线程单元 实例
TServerSocket阻塞线程单元,希望对你有所帮助.需要注意的是:1.如果你使用TServerSocket的stNonBlocking模式,重写TServerClientThread线程时要重载 ...
- 2013 ACM 通化邀请赛 A. Tutor
A. Tutor Description Lilin was a student of Tonghua Normal University. She is studying at University ...
- NOIP201305转圈游戏
2016.1.25 试题描述 有n个小伙伴(编号从0到n-1)围坐一圈玩游戏.按照顺时针方向给n个位置编号,从0到n-1.最初,第0号小伙伴在第0号位置,第1号小伙伴在第1号位置,……,依此类推. ...
- 通过srvctl add命令添加database信息到srvctl管理器-转
这是我在实际中遇到的一个问题,rac+dg架构将备库切为主库,srvctl管理器中没有database信息. 对于dbca创建的数据库,srvctl中包含了数据库和实例的信息.但是对于备份恢复的RAC ...
- ofstream的问题
ofstream在多字节编码的项目中, 写入中文目录写不了, 英文目录可以 换成c库的fopen, fwrite可以
- jmeter测试手机app
具体步骤:1.电脑启动jmeter2.jmeter在测试计划新建线程组,在工作台新建http代理服务器3.设置IE代理到本地4.手机wifi设置代理连接到PC5.[启动]jmeter代理服务器6.现在 ...
- gis 导出 dwg,shp
当我们在webgis 想要把某个地块或者多个地块导出dwg或者shp文件的时候怎么办?这个时候最好就是用后台的方式.首先把web gis上的graphic 的polygon提取为坐标的形式(类似于x, ...
- 关于Rational Functional Tester (RFT)的简单介绍
前段时间给客户做了个RFT的简单培训,以下.因为涉及到公司的框架,所以中间省去了很多框架里的细节,只留了一个框架的总体结构的概览. RFT IBM Rational Functional Tester ...