1、窗口设计
<Window x:Class="A._2._2.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:A._2._2"
mc:Ignorable="d"
Title="简单计算器" Height="400" Width="600">
<Grid>
<GroupBox Name="group1" Header="请选择运算类型" HorizontalAlignment="Left" Height="97" Margin="75,44,0,0" Width="431" FontSize="16" VerticalAlignment="Top">
<StackPanel Name="stackpanel_1" HorizontalAlignment="Left" Height="48" Margin="10,10,0,0" VerticalAlignment="Top" Width="394" Orientation="Horizontal">
<RadioButton Name="r_1" Content="加法" Height="28" VerticalAlignment="Top" Margin="10,10,10,0" Click="RadioButton_Click"/>
<RadioButton Name="r_2" Content="减法" Margin="10" Click="RadioButton_Click"/>
<RadioButton Name="r_3" Content="乘法" Margin="10" Click="RadioButton_Click"/>
<RadioButton Name="r_4" Content="除法" Margin="10" Click="RadioButton_Click"/>
<RadioButton Name="r_5" Content="取模" Margin="10" Click="RadioButton_Click"/>
</StackPanel>
</GroupBox>
<GroupBox Name="group2" Header="" HorizontalAlignment="Left" Height="100" Margin="75,231,0,0" VerticalAlignment="Top" Width="431">
<StackPanel HorizontalAlignment="Left" Height="51" Margin="36,10,0,0" VerticalAlignment="Top" Width="351" Orientation="Horizontal">
<TextBox Name="first" Height="23" TextWrapping="Wrap" Text="" FontSize="16" Margin="0,14,0,14.2" Width="64" TextAlignment="Center"/>
<TextBlock Name="fuhao" TextWrapping="Wrap" Text="" Margin="0,14,0,14.2" TextAlignment="Center" FontSize="16" Width="30.4"/>
<TextBox Name="second" TextWrapping="Wrap" Text="" Width="64" Margin="0,14,0,14.2" FontSize="16" TextAlignment="Center"/>
<TextBlock TextWrapping="Wrap" Text=" = " Margin="0,14,0,14.2" FontSize="16"/>
<TextBox Name="result" TextWrapping="Wrap" Text="" Width="64" Margin="0,14,0,14.2" FontSize="16" IsReadOnly="True" TextAlignment="Center"/>
<Button Content="计算" Width="54" Margin="20,14,20,14.2" Click="Button_Click"/>
</StackPanel>
</GroupBox>
</Grid>
</Window>
2、函数功能实现
① 选择运算方式的时候,要判断并更新符号
② 计算函数要获取操作数的值,并且判断是否选择运算方式
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;
namespace A._2._2
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
bool flag = false;//判断是否选取符号
private void Button_Click(object sender, RoutedEventArgs e)
{
double a, b;
double c=0;
bool flat1 = double.TryParse(first.Text, out a);//获取第一个操作数
bool flat2 = double .TryParse(second.Text, out b);//获取第二个操作数
if (!flat1 || !flat2 || !flag)
result.Text = "?";
else
{
switch (fuhao.Text)
{
case "+":
c = a + b;
break;
case "-":
c = a - b;
break;
case "*":
c = a * b; break;
case "/":
c = (double)a/ b; break;
case "%":
c = a % b; break;
default:
flag = false;
break;
}
result.Text = c.ToString();
}
}
private void RadioButton_Click(object sender, RoutedEventArgs e)
{
if(r_1.IsChecked==true)
{
group2.Header = r_1.Content;
fuhao.Text = "+";
first.Text = "";
second.Text = "";
result.Text = "";
}
if (r_2.IsChecked == true)
{
group2.Header = r_2.Content;
fuhao.Text = "-";
first.Text = "";
second.Text = "";
result.Text = "";
}
if (r_3.IsChecked == true)
{
group2.Header = r_3.Content;
fuhao.Text = "*";
first.Text = "";
second.Text = "";
result.Text = "";
}
if (r_4.IsChecked == true)
{
group2.Header = r_4.Content;
fuhao.Text = "/";
first.Text = "";
second.Text = "";
result.Text = "";
}
if (r_5.IsChecked == true)
{
group2.Header = r_5.Content;
fuhao.Text = "%";
first.Text = "";
second.Text = "";
result.Text = "";
}
flag = true;
}
}
}