XAML程式码:
<Window x:Class="rekenmachine_variant_true.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:rekenmachine_variant_true"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="118*"/>
<ColumnDefinition Width="683*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="0*"/>
</Grid.RowDefinitions>
<Button Content="1" Click="num_Click" HorizontalAlignment="Left" Margin="27,334,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num1" FontFamily="Courier New" FontSize="72"/>
<Button Content="2" Click="num_Click" HorizontalAlignment="Left" Margin="117,334,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num2" FontFamily="Courier New" FontSize="72" Grid.ColumnSpan="2"/>
<Button Content="3" Click="num_Click" HorizontalAlignment="Left" Margin="90,334,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num3" FontFamily="Courier New" FontSize="72" Grid.Column="1"/>
<Button Content="4" Click="num_Click" HorizontalAlignment="Left" Margin="27,244,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num4" FontFamily="Courier New" FontSize="72"/>
<Button Content="5" Click="num_Click" HorizontalAlignment="Left" Margin="117,244,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num5" FontFamily="Courier New" FontSize="72" Grid.ColumnSpan="2"/>
<Button Content="6" Click="num_Click" HorizontalAlignment="Left" Margin="90,244,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num6" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="7" Click="num_Click" HorizontalAlignment="Left" Margin="27,155,0,0" VerticalAlignment="Top" Height="80" Width="80" Name="num7" FontSize="72" FontFamily="Courier New"/>
<Button Content="8" Click="num_Click" HorizontalAlignment="Left" Margin="117,155,0,0" VerticalAlignment="Top" Height="80" Width="80" x:Name="num8" FontSize="72" FontFamily="Courier New" Grid.ColumnSpan="2"/>
<Button Content="9" Click="num_Click" HorizontalAlignment="Left" Margin="90,155,0,0" VerticalAlignment="Top" Height="80" Width="80" x:Name="num9" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="0" Click="num_Click" HorizontalAlignment="Left" Margin="180,244,0,0" VerticalAlignment="Top" Height="80" Width="80" x:Name="num0" FontSize="72" FontFamily="Courier New" FontStyle="Normal" Grid.Column="1"/>
<Button Content="." Click="decimal_Click" HorizontalAlignment="Left" Margin="180,334,0,0" VerticalAlignment="Top" Height="80" Width="80" x:Name="Nul_Copy" FontSize="72" FontFamily="Courier New" FontStyle="Normal" Grid.Column="1"/>
<Button Content="*" Click="keer_Click" HorizontalAlignment="Left" Margin="391,333,0,0" VerticalAlignment="Top" Height="80" Width="80" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="/" Click="deel_Click" HorizontalAlignment="Left" Margin="483,333,0,0" VerticalAlignment="Top" Height="80" Width="80" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="=" Click="is_Click" HorizontalAlignment="Left" Margin="575,332,0,0" VerticalAlignment="Top" Height="81" Width="80" FontSize="72" FontFamily="Courier New" FontStyle="Normal" Grid.Column="1" />
<Button Content="+" Click="plus_Click" HorizontalAlignment="Left" Margin="391,243,0,0" VerticalAlignment="Top" Height="80" Width="80" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="-" Click="minus_Click" HorizontalAlignment="Left" Margin="483,243,0,0" VerticalAlignment="Top" Height="80" Width="80" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="%" Click="modulo_Click" HorizontalAlignment="Left" Margin="392,155,0,0" VerticalAlignment="Top" Height="80" Width="263" FontSize="72" FontFamily="Courier New" Grid.Column="1"/>
<Button Content="CE" Click="clear_Click" HorizontalAlignment="Left" Margin="180,155,0,0" VerticalAlignment="Top" Height="80" Width="80" FontSize="72" FontFamily="Bahnschrift Condensed" Background="#FFFFE74E" Grid.Column="1"/>
<Button x:Name="minmax" Content="-/+" Click="minusPlus_Click" HorizontalAlignment="Left" Margin="575,243,0,0" VerticalAlignment="Top" Height="81" Width="80" FontSize="36" FontFamily="Courier New" FontStyle="Normal" Grid.Column="1" FontWeight="SemiBold"/>
<TextBox x:Name="TxtBox" HorizontalAlignment="Left" Margin="27,24,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="746" Grid.ColumnSpan="2" Height="110" FontSize="72" FontStyle="Normal" FontFamily="Segoe UI" TextAlignment="Right"/>
</Grid>
</Window>
CS代码:
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 rekenmachine_variant_true
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
decimal valueFirst;
decimal valueSecond;
char operators;
private void decimal_Click(object sender, RoutedEventArgs e)
{
if (!TxtBox.Text.Contains("."))
{
TxtBox.Text += '.';
}
}
private void num_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
TxtBox.Text += btn.Content.ToString();
valueFirst = decimal.Parse(TxtBox.Text);
}
private void minusPlus_Click(object sender, RoutedEventArgs e)
{
if (TxtBox.Text.Contains("-"))
{
TxtBox.Text = TxtBox.Text.Trim('-');
}
else
{
TxtBox.Text = "-" + TxtBox.Text;
}
}
private void minus_Click(object sender, RoutedEventArgs e)
{
valueFirst = decimal.Parse(TxtBox.Text);
TxtBox.Clear();
operators = '-';
}
private void plus_Click(object sender, RoutedEventArgs e)
{
valueFirst = decimal.Parse(TxtBox.Text);
TxtBox.Clear();
operators = '+';
}
private void keer_Click(object sender, RoutedEventArgs e)
{
valueFirst = decimal.Parse(TxtBox.Text);
TxtBox.Clear();
operators = '*';
}
private void deel_Click(object sender, RoutedEventArgs e)
{
valueFirst = decimal.Parse(TxtBox.Text);
TxtBox.Clear();
operators = '/';
}
private void modulo_Click(object sender, RoutedEventArgs e)
{
valueFirst = decimal.Parse(TxtBox.Text);
TxtBox.Clear();
operators = '%';
}
private void is_Click(object sender, RoutedEventArgs e)
{
valueSecond = decimal.Parse(TxtBox.Text);
decimal result = 0;
if(operators == '+')
{
result = valueFirst + valueSecond;
TxtBox.Text = result.ToString();
}
else if (operators == '-')
{
result = valueFirst - valueSecond;
TxtBox.Text = result.ToString();
}
else if (operators == '*')
{
result = valueFirst * valueSecond;
TxtBox.Text = result.ToString();
}
else if (operators == '/')
{
result = valueFirst / valueSecond;
TxtBox.Text = result.ToString();
}
else if (operators == '%')
{
result = valueFirst % valueSecond;
TxtBox.Text = result.ToString();
}
if (TxtBox.Text == "0")
{
TxtBox.Clear();
}
}
private void clear_Click(object sender, RoutedEventArgs e)
{
TxtBox.Clear();
}
public MainWindow()
{
InitializeComponent();
}
}
}
**注意:**我知道minPlus,modulo,& decimal在atm下不起作用,但我想自己来计算它们。(清除按钮并不真正清除值,它只是清除txtbox中的文本)
问题:与按钮2(名称:“num 2”)会把整个计算搞得一团糟,我不明白为什么会这样。是不是小数的某些地方把它搞得一团糟?
示例:
二加六=十二
2 - 6个空白结果
85 + 2 = 4
更改数字2按钮上的内容似乎根本不起作用,它始终给出与上面示例相同的结果。
1条答案
按热度按时间imzjd6km1#
这段代码是错误的:
对于第一个条目,它可能是好的,但在按下操作员按钮后,TxtBox的内容将类似于:
2+6
因此,
valueFirst = decimal.Parse(TxtBox.Text);
将使decimal
尝试解析“2+6”。试试看: