有人能给我一个减少计算器代码中函数数量的方法吗

本文关键字:函数 代码 计算器 数数 方法 一个 | 更新日期: 2023-09-27 18:25:58

这是我的XAML代码-

<Window x:Class="CalculatorGridTrial.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:CalculatorGridTrial"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <TextBox TextWrapping="Wrap" x:Name="operationxml"  Grid.Row="0"     Grid.Column="0" Grid.ColumnSpan="5" Text= "{Binding Path=Display}" />
    <TextBox TextWrapping="Wrap" x:Name="resultxml" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="5" Text="{Binding Path=Display}"/> 
    <Button x:Name="numberonexml"  Content="1" Grid.Row="2" Grid.Column="0" Click="numberonexml_Click"  />
    <Button x:Name="numbertwoxml" Content="2" Grid.Row="2" Grid.Column="1" Click="numbertwoxml_Click"   />
    <Button x:Name="numberthreexml" Content="3" Grid.Row="2" Grid.Column="2"  Click="numberthreexml_Click" />
    <Button x:Name="numberfourxml" Content="4" Grid.Row="3" Grid.Column="0" Click="numberfourxml_Click"  />
    <Button x:Name="numberfivexml" Content="5" Grid.Row="3" Grid.Column="1"  Click="numberfivexml_Click"  />
    <Button x:Name="numbersixxml" Content="6" Grid.Row="3" Grid.Column="2"  Click="numbersixxml_Click"  />
    <Button x:Name="numbersevenxml" Content="7" Grid.Row="4" Grid.Column="0" Click="numbersevenxml_Click"  />
    <Button x:Name="numbereightxml" Content="8" Grid.Row="4" Grid.Column="1" Click="numbereightxml_Click"  />
    <Button x:Name="numberninexml" Content="9" Grid.Row="4" Grid.Column="2" Click="numberninexml_Click"  />
    <Button x:Name="numberzeroxml" Content="0" Grid.Row="5" Grid.Column="1"  Click="numberzeroxml_Click"  />
    <Button x:Name="pointxml" Content="." Grid.Row="5" Grid.Column="0" Click="pointxml_Click"  />
    <Button x:Name="clearxml" Content="C" Grid.Row="5" Grid.Column="2" Click="clearxml_Click" />
    <Button x:Name="plusxml" Content="+" Grid.Row="2" Grid.Column="3" Click="plusxml_Click"  />
    <Button x:Name="minusxml" Content="-" Grid.Row="2" Grid.Column="4" Click="minusxml_Click"  />
    <Button x:Name="mulxml" Content="*" Grid.Row="3" Grid.Column="3" Click="mulxml_Click"  />
    <Button x:Name="divxml" Content="/" Grid.Row="3" Grid.Column="4" Click="divxml_Click"  />
    <Button x:Name="equalxml" Content="=" Grid.Row="4" Grid.Column="3" Grid.ColumnSpan="2" Click="equalxml_Click"  /> 
</Grid>

这是我的C#代码。我刚开始编码才几天。我是新来的,有很多东西要学。我已经详细介绍了数据绑定的概念,并在这段代码中实现了它。有人能帮我创建数量较少的函数吗。我在想,如果我们能为所有的数字函数创建1个函数,为所有的运算符创建1个。

提前谢谢。

using System;
using System.Collections.Generic;
using System.ComponentModel;
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 CalculatorGridTrial
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public class CalculatorViewModel : INotifyPropertyChanged
{
    private string display;
    public string Display
    {
        get
        {
            return display;
        }
        set
        {
            if(display!= value)
            {
                display = value;
                OnPropertyChanged("Display");
            }
        }
    }
    public void OnPropertyChanged(string propertyName)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
}
public partial class MainWindow : Window
{
    CalculatorViewModel calc = new CalculatorViewModel();
    public MainWindow()
    {
        InitializeComponent();
        operationxml.DataContext = calc;
    }
    List<double> NumbersList = new List<double>();

    List<char> OperatorsList = new List<char>();

    List<string> BufferList = new List<string>();


    char operation;
    string bufferNumber = " ";
    double total = 0;
    private void numberonexml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "1";
        bufferNumber = "1";
        BufferFunction(bufferNumber);
    }
    private void numbertwoxml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "2";
        bufferNumber = "2";
        BufferFunction(bufferNumber);
    }
    private void numberthreexml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "3";
        bufferNumber = "3";
        BufferFunction(bufferNumber);
    }
    private void numberfourxml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "4";
        bufferNumber = "4";
        BufferFunction(bufferNumber);
    }
    private void numberfivexml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "5";
        bufferNumber = "5";
        BufferFunction(bufferNumber);
    }
    private void numbersixxml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "6";
        bufferNumber = "6";
        BufferFunction(bufferNumber);
    }
    private void numbersevenxml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "7";
        bufferNumber = "7";
        BufferFunction(bufferNumber);
    }
    private void numbereightxml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "8";
        bufferNumber = "8";
        BufferFunction(bufferNumber);
    }
    private void numberninexml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "9";
        bufferNumber = "9";
        BufferFunction(bufferNumber);
    }
    private void numberzeroxml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "0";
        bufferNumber = "0";
        BufferFunction(bufferNumber);
    }
    private void pointxml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += ".";
        bufferNumber = ".";
        BufferFunction(bufferNumber);
    }
    private void clearxml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display = null;
        NumbersList.Clear();
        OperatorsList.Clear();
        BufferList.Clear();
    }
    private void plusxml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "+";
        operation = '+';
        OperatorsFunction(operation);
    }
    private void minusxml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "-";
        operation = '-';
        OperatorsFunction(operation);
    }
    private void mulxml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "*";
        operation = '*';
        OperatorsFunction(operation);
    }
    private void divxml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "/";
        operation = '/';
        OperatorsFunction(operation);
    }

    private void equalxml_Click(object sender, RoutedEventArgs e)
    {
        calc.Display += "=";
        total = NumbersList[0];
        //List<double> TempBufferList2 = BufferList.ConvertAll(s => double.Parse(s));
        //NumbersList.AddRange(TempBufferList2);//Adding the last element(i.e. before '=' to the numberlist
        NumbersList.Add(ConvertBufferArrayToNumber());
        var operatorswitch = ' ';
        for(int loopvariable = 1; loopvariable < NumbersList.Count; loopvariable++)
        {
            operatorswitch = OperatorsList.ElementAt(loopvariable - 1);
            switch(operatorswitch)
            {
                case '+': total += NumbersList.ElementAt(loopvariable); break;
                case '-': total -= NumbersList.ElementAt(loopvariable); break;
                case '*': total *= NumbersList.ElementAt(loopvariable); break;
                case '/': total /= NumbersList.ElementAt(loopvariable); break;
            }
        }
        calc.Display = Convert.ToString(total);
        NumbersList.Clear();
        OperatorsList.Clear();
    }

    private void OperatorsFunction(char operation)
    {
        OperatorsList.Add(operation);//Adding operation to Operatorslist
        //var number = Convert.ToDouble(String.Join("", BufferList));
        NumbersList.Add(ConvertBufferArrayToNumber());
    }
    public void BufferFunction(string bufferNumber)
    {
        BufferList.Add(bufferNumber);// Adding numbers to Bufferlist in string format
    }
    private double ConvertBufferArrayToNumber()
    {
        var number = Convert.ToDouble(String.Join("", BufferList));
        BufferList.Clear();
        return number;
    }
}

}

有人能给我一个减少计算器代码中函数数量的方法吗

我不认为每个按钮都需要单独的事件处理程序。创建一个。你可以做如下操作。。

  1. 将事件处理程序的发送方解析为按钮
  2. 获取内容
  3. 将其解析为数字
  4. 如果不是,请检查其他操作员的编号
  5. 得到结果后,您可以继续使用代码。。已经存在