最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

WPF實(shí)現(xiàn)多運(yùn)算符表達(dá)式計(jì)算器

 更新時(shí)間:2020年11月08日 13:45:59   作者:賧  
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)多運(yùn)算符表達(dá)式計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

WPF實(shí)現(xiàn)一個(gè)簡(jiǎn)單的多運(yùn)算符表達(dá)式計(jì)算器,供大家參考,具體內(nèi)容如下

1.先看下效果圖

首先外圍給了一個(gè)grid 把他分成了兩行 第一行用來顯示文本框給了一個(gè)低于第二行的高度 第二行用來存按鈕 窗體的大小自己去調(diào)就好了 我這里給的是380x268

<Grid.RowDefinitions>
 <RowDefinition Height="0.7*"></RowDefinition>
 <RowDefinition></RowDefinition>
</Grid.RowDefinitions>

這是一個(gè)非常簡(jiǎn)單的布局 沒有用到樣式 頭部是用了一個(gè)Border給一個(gè)圓弧實(shí)現(xiàn)的 代碼如下

<Border Margin="5" Padding="5" Background="White" BorderBrush="Black" BorderThickness="3,5,3,5" CornerRadius="10" VerticalAlignment="Top" Height="130" Width="240">
  <TextBlock Name="ShowNumText" Height="100" Width="auto" VerticalAlignment="Top" FontSize="50" HorizontalAlignment="Right" >
  
  </TextBlock>
</Border>

接下來就是按鍵部分了 用了一個(gè)UniformGrid布局 類似于一個(gè)表格 給4行4列 最后再往里面添加按鈕實(shí)現(xiàn)的 分別給每個(gè)按鈕設(shè)置背景顏色,字體顏色以及單擊事件(一共4類單擊事件 分別是數(shù)字的、運(yùn)算符的、等于號(hào)、還有一個(gè)清空C)

<UniformGrid Grid.Row="1" Rows="4" Columns="4" Height="200" Width="250">
  <Button Name="btn1" Content="1" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btn2" Content="2" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btn3" Content="3" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btnD" Content="÷" FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
  <Button Name="btn4" Content="4" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btn5" Content="5" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btn6" Content="6" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btnX" Content="X" FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
  <Button Name="btn7" Content="7" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btn8" Content="8" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btn9" Content="9" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btnM" Content="-" FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
  <Button Name="btnC" Content="C" FontSize="35" Background="Black" Foreground="White" Click="btnC_Click"/>
  <Button Name="btn0" Content="0" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btnE" Content="+" FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
  <Button Name="btnP" Content="=" FontSize="35" Background="Black" Foreground="White" Click="btnP_Click"/>
</UniformGrid>

這樣我們的xaml樣式就寫完了,接下來就是后端了

上面的代碼我們看到我們已經(jīng)定義了單擊事件 首先找到數(shù)字的單擊事件寫上以下代碼

var v = sender as Button;
  switch (v.Content.ToString())
  {
  case "1":
   ShowNumText.Text += 1;
   break;
  case "2":
   ShowNumText.Text += 2;
   break;
  case "3":
   ShowNumText.Text += 3;
   break;
  case "4":
   ShowNumText.Text += 4;
   break;
  case "5":
   ShowNumText.Text += 5;
   break;
  case "6":
   ShowNumText.Text += 6;
   break;
  case "7":
   ShowNumText.Text += 7;
   break;
  case "8":
   ShowNumText.Text += 8;
   break;
  case "9":
   ShowNumText.Text += 9;
   break;
  case "0":
   ShowNumText.Text += 0;
   break;

意思就是判斷一下用戶點(diǎn)擊的是哪一個(gè)數(shù)字 然后把他加到文本框內(nèi)

接下來就是運(yùn)算符的單擊事件 同理數(shù)字的

if (ShowNumText.Text == "")
  return;
  var v1 = sender as Button;
  switch (v1.Content.ToString())
  {
  case "+":
   ShowNumText.Text += "+";
   break;
  case "-":
   ShowNumText.Text += "-";
   break;
  case "X":
   ShowNumText.Text += "X";
   break;
  case "÷":
   ShowNumText.Text += "÷";
   break;
} 

然后導(dǎo)入命名空間

using System.Data;

這個(gè)命名空間里面有一個(gè)超級(jí)好用的方法Compute
Compute的意思簡(jiǎn)單來說就是放入一個(gè)string類型的帶有表達(dá)式的字符串計(jì)算,
找到等于號(hào)的事件 加入代碼

try
 {
  string str= ShowNumText.Text.Replace('X', '*');
  str= str.Replace('÷', '/');
  DataTable dt = new DataTable();
  string v = dt.Compute(str, null).ToString();
  ShowNumText.Text = v.ToString();
  }
  catch { ShowNumText.Text = ""; }

用Replace方法過濾掉 x和÷
因?yàn)镃ompute 是不接收數(shù)學(xué)的乘和除的

最后在清空事件里加入一個(gè)ShowNumText.Text = "";//清空文本框
這樣我們的計(jì)算器就寫完了?。?!

前臺(tái)xaml

<Window x:Class="WpfApplication1.MainWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 xmlns:local="clr-namespace:WpfApplication1"
 mc:Ignorable="d"
 Title="計(jì)算機(jī)" Height="380" Width="268">
 <Grid>
 <Grid.RowDefinitions>
  <RowDefinition Height="0.7*"></RowDefinition>
  <RowDefinition></RowDefinition>
 </Grid.RowDefinitions>
 <Border Margin="5" Padding="5" Background="White" BorderBrush="Black" BorderThickness="3,5,3,5" CornerRadius="10" VerticalAlignment="Top" Height="130" Width="240">
  <TextBlock Name="ShowNumText" Height="100" Width="auto" VerticalAlignment="Top" FontSize="50" HorizontalAlignment="Right" >
  
  </TextBlock>
 </Border>
 <UniformGrid Grid.Row="1" Rows="4" Columns="4" Height="200" Width="250">
  <Button Name="btn1" Content="1" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btn2" Content="2" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btn3" Content="3" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btnD" Content="÷" FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
  <Button Name="btn4" Content="4" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btn5" Content="5" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btn6" Content="6" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btnX" Content="X" FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
  <Button Name="btn7" Content="7" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btn8" Content="8" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btn9" Content="9" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btnM" Content="-" FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
  <Button Name="btnC" Content="C" FontSize="35" Background="Black" Foreground="White" Click="btnC_Click"/>
  <Button Name="btn0" Content="0" FontSize="35" Background="Black" Foreground="White" Click="btn1_Click_1"/>
  <Button Name="btnE" Content="+" FontSize="35" Background="Black" Foreground="White" Click="btnD_Click"/>
  <Button Name="btnP" Content="=" FontSize="35" Background="Black" Foreground="White" Click="btnP_Click"/>
 </UniformGrid>
 </Grid>
</Window>

后臺(tái)代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
using System.Data;
namespace WpfApplication1
{
 /// <summary>
 /// MainWindow.xaml 的交互邏輯
 /// </summary>
 public partial class MainWindow : Window
 {
 public MainWindow()
 {
  InitializeComponent();
  
 }
 private void btn1_Click_1(object sender, RoutedEventArgs e)//0-9綁定同一個(gè)事件
 { 
  var v = sender as Button;
  switch (v.Content.ToString())
  {
  case "1":
   ShowNumText.Text += 1;
   break;
  case "2":
   ShowNumText.Text += 2;
   break;
  case "3":
   ShowNumText.Text += 3;
   break;
  case "4":
   ShowNumText.Text += 4;
   break;
  case "5":
   ShowNumText.Text += 5;
   break;
  case "6":
   ShowNumText.Text += 6;
   break;
  case "7":
   ShowNumText.Text += 7;
   break;
  case "8":
   ShowNumText.Text += 8;
   break;
  case "9":
   ShowNumText.Text += 9;
   break;
  case "0":
   ShowNumText.Text += 0;
   break;
  } 
 }
 private void btnD_Click(object sender, RoutedEventArgs e)//運(yùn)算符也綁定同一個(gè)事件
 {
  if (ShowNumText.Text == "")
  return;
  var v1 = sender as Button;
  switch (v1.Content.ToString())
  {
  case "+":
   ShowNumText.Text += "+";
   break;
  case "-":
   ShowNumText.Text += "-";
   break;
  case "X":
   ShowNumText.Text += "X";
   break;
  case "÷":
   ShowNumText.Text += "÷";
   break;
  } 
 }
 private void btnP_Click(object sender, RoutedEventArgs e)
 {
  try
  {
  string str= ShowNumText.Text.Replace('X', '*');
  str= str.Replace('÷', '/');
  DataTable dt = new DataTable();
  string v = dt.Compute(str, null).ToString();
  ShowNumText.Text = v.ToString();
  }
  catch { ShowNumText.Text = ""; }
 }
 private void btnC_Click(object sender, RoutedEventArgs e)
 {
  ShowNumText.Text = "";//清空文本框
 }
 }
}

感謝你的觀看!

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

拉萨市| 吉水县| 施秉县| 临夏县| 北安市| 巴林右旗| 徐水县| 屏南县| 缙云县| 噶尔县| 洛扎县| 噶尔县| 兴仁县| 张家港市| 连平县| 佛坪县| 伊宁市| 孝感市| 寿光市| 奉节县| 汉中市| 和龙市| 嘉峪关市| 革吉县| 郴州市| 宣恩县| 布拖县| 广元市| 都兰县| 蒙阴县| 靖边县| 罗平县| 巫溪县| 当雄县| 吴堡县| 全椒县| 昌图县| 南部县| 读书| 疏勒县| 南通市|