2024. október 22., kedd

2024.10.22. Számológép

 Egy példa megvalósítás

<Window x:Class="SZGYA_WPF_Calculator.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:SZGYA_WPF_Calculator"
        mc:Ignorable="d"
        Title="Calculator" Height="600" Width="450">

    <DockPanel Height="Auto" VerticalAlignment="Stretch">
        <StackPanel DockPanel.Dock="Top">
            <Label x:Name="lblSecondary" Content="[szamolas placeholder]" HorizontalAlignment="Stretch" Margin="24,0" VerticalContentAlignment="Center" Grid.ColumnSpan="4"  HorizontalContentAlignment="Right" Height="34" FontSize="16"/>
            <Label x:Name="lblCurrent" Content="[eredmeny]" HorizontalAlignment="Stretch" Margin="12,0" Grid.ColumnSpan="4" Height="65" FontSize="36" VerticalContentAlignment="Center" HorizontalContentAlignment="Right"/>
            <CheckBox x:Name="chkTudomanyos" HorizontalAlignment="Left" VerticalContentAlignment="Center" VerticalAlignment="Center" Content="Tudományos" FontSize="20" Margin="24,0" Checked="chkTudomanyos_Checked" Unchecked="chkTudomanyosUnchecked"/>
        </StackPanel>
        <Grid x:Name="gridCalc" VerticalAlignment="Stretch" DockPanel.Dock="Bottom" Margin="10,10">
            <Grid.Resources>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Margin" Value="5,5" />
                    <Style.Resources>
                        <Style TargetType="Border">
                            <Setter Property="CornerRadius" Value="5" />
                        </Style>
                    </Style.Resources>
                </Style>
            </Grid.Resources>
            <Grid.RowDefinitions>
                <RowDefinition Height="0"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
                <RowDefinition Height="1*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1*"/>
                <ColumnDefinition Width="1*"/>
            </Grid.ColumnDefinitions>

            <Button Content="[nincs impl]" Grid.Row="0" Grid.Column="0"/>
            <Button x:Name="btnGyok" Content="√x" Grid.Row="0" Grid.Column="1" Click="opHandler"/>
            <Button x:Name="btnNegyzet" Content="x²" Grid.Row="0" Grid.Column="2" Click="opHandler"/>
            <Button x:Name="btnHatvany" Content="^" Grid.Row="0" Grid.Column="3" Click="opHandler"/>
            <Button x:Name="btnReciprok" Content="⅟x" Grid.Row="1" Grid.Column="0" Click="opHandler"/>
            <Button x:Name="btnDelete" Content="C" Grid.Row="1" Grid.Column="1" Click="btnDelete_Click"/>
            <Button x:Name="btnDeletePartial" Content="Ce" Grid.Row="1" Grid.Column="2" Click="btnDeletePartial_Click"/>
            <Button x:Name="btn7" Content="7" Grid.Row="2" Grid.Column="0" Click="numberBtnClick"/>
            <Button x:Name="btn8" Content="8" Grid.Row="2" Grid.Column="1" Click="numberBtnClick"/>
            <Button x:Name="btn9" Content="9" Grid.Row="2" Grid.Column="2" Click="numberBtnClick"/>
            <Button x:Name="btn4" Content="4" Grid.Row="3" Grid.Column="0" Click="numberBtnClick"/>
            <Button x:Name="btn5" Content="5" Grid.Row="3" Grid.Column="1" Click="numberBtnClick"/>
            <Button x:Name="btn6" Content="6" Grid.Row="3" Grid.Column="2" Click="numberBtnClick"/>
            <Button x:Name="btn1" Content="1" Grid.Row="4" Grid.Column="0" Click="numberBtnClick"/>
            <Button x:Name="btn2" Content="2" Grid.Row="4" Grid.Column="1" Click="numberBtnClick"/>
            <Button x:Name="btn3" Content="3" Grid.Row="4" Grid.Column="2" Click="numberBtnClick"/>
            <Button x:Name="btnNegate" Content="±" Grid.Row="5" Grid.Column="0" Click="opHandler"/>
            <Button x:Name="btn0" Content="0" Grid.Row="5" Grid.Column="1" Click="numberBtnClick"/>
            <Button x:Name="btnTizedes" Content="," Grid.Row="5" Grid.Column="2" Click="numberBtnClick"/>

            <Button x:Name="btonOsztas" Content="/" Grid.Row="1" Grid.Column="4" Click="opHandler"/>
            <Button x:Name="btnSzorzas" Content="X" Grid.Row="2" Grid.Column="4" Click="opHandler"/>
            <Button x:Name="btnMinusz" Content="-" Grid.Row="3" Grid.Column="4" Click="opHandler"/>
            <Button x:Name="btnPlusz" Content="+" Grid.Row="4" Grid.Column="4" Click="opHandler"/>
            <Button x:Name="btnEgyenlo" Content="=" Grid.Row="5" Grid.Column="4" Click="calculateResult"/>
        </Grid>
    </DockPanel>
</Window>


using System.Net.Http;
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;
using static System.Net.Mime.MediaTypeNames;

namespace SZGYA_WPF_Calculator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private double last = 0;
        private double current = 0;
        private string currentStr = "";
        private string secondaryStr = "";
        private string op = "";

        public MainWindow()
        {
            InitializeComponent();
            updateDisplay("0");
            updateSecondaryDisplay(string.Empty);
            this.KeyDown += new KeyEventHandler(OnButtonKeyDown);
        }

        private void OnButtonKeyDown(object sender, KeyEventArgs e)
        {
            var keyDown = e.Key.ToString().Last();
            if (keyDown >= '0' && keyDown <= '9') updateNum($"{keyDown}");
           
        }

        private void numberBtnClick(object sender, RoutedEventArgs e)
        {
            Button b = (Button)sender;
            updateNum(b.Content.ToString());
           
        }

        void updateNum(string num)
        {
            if (num == "," && current == 0) updateDisplay(num);
            else updateDisplay(num, current == 0);
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            updateDisplay("0", true);
            updateSecondaryDisplay(string.Empty);
        }

        private void btnDeletePartial_Click(object sender, RoutedEventArgs e)
        {
            updateDisplay("0", true);
        }

        private void opHandler(object sender, RoutedEventArgs e)
        {
            Button b = (Button)sender;
            if (op == string.Empty)
            {
                op = b.Content.ToString();
                last = current;
                if (op == "x²")
                {
                    updateSecondaryDisplay($"{last}²");
                    current = Math.Pow(current, 2);
                    updateDisplay($"{current}", true);
                    op = string.Empty;
                }
                else if (op == "√x")
                {
                    updateSecondaryDisplay($"√{current}");
                    current = Math.Sqrt(current);
                    updateDisplay($"{current}", true);
                    op = string.Empty;
                }
                else if (op == "±")
                {
                    updateSecondaryDisplay($"negate({current})");
                    current = -current;
                    updateDisplay($"{current}", true);
                    op = string.Empty;
                }
                else if (op == "⅟x")
                {
                    updateSecondaryDisplay($"⅟({current})");
                    if (current == 0)
                    {
                        lblCurrent.Content = "Nullával nem lehet osztani";
                        op = string.Empty;
                    }
                    else
                    {
                        current = 1 / current;
                        updateDisplay($"{current}", true);
                        op = string.Empty;
                    }
                }
                else
                {
                    updateDisplay(string.Empty);
                    updateSecondaryDisplay($"{last} {op}");
                }
            }
            else
            {
                op = b.Content.ToString();
                updateSecondaryDisplay($"{last} {op}");
            }
        }

        private void calculateResult(object sender, RoutedEventArgs e)
        {
            updateSecondaryDisplay($"{last} {op} {current}");
            switch (op)
            {
                case "+":
                    last = last + current;
                    updateDisplay($"{last}", true);
                    op = string.Empty;
                    break;
                case "-":
                    last = last - current;
                    updateDisplay($"{last}", true);
                    op = string.Empty;
                    break;
                case "X":
                    last = last * current;
                    updateDisplay($"{last}", true);
                    op = string.Empty;
                    break;
                case "/":
                    if (current == 0)
                    {
                        lblCurrent.Content = "Nullával nem lehet osztani";
                        op = string.Empty;
                        break;
                    }
                    last = last / current;
                    updateDisplay($"{last}", true);
                    op = string.Empty;
                    break;
                case "^":
                    last = Math.Pow(last, current);
                    updateDisplay($"{last}", true);
                    op = string.Empty;
                    break;
                default:
                    break;
            }
        }

        private void updateDisplay(string content, bool clear = false)
        {
            if (clear || content == string.Empty) currentStr = string.Empty;
            currentStr += content;
            current = currentStr != string.Empty ? double.Parse(currentStr) : 0;
            lblCurrent.Content = currentStr;
        }

        private void updateSecondaryDisplay(string content)
        {
            secondaryStr = content;
            lblSecondary.Content = secondaryStr;
        }

        private void chkTudomanyos_Checked(object sender, RoutedEventArgs e)
        {
            gridCalc.RowDefinitions[0].Height = new GridLength(1, GridUnitType.Star);
        }

        private void chkTudomanyosUnchecked(object sender, RoutedEventArgs e)
        {
            gridCalc.RowDefinitions[0].Height = new GridLength(0, GridUnitType.Pixel);
        }

       
    }
}

2024. október 7., hétfő

2024.10.08. Számológép - WPF

Több lehetőségünk van az elrendezés optimalizálására.

Grid, StackPanel, WrapPanel, DockPanel, ViewBox

Grides elrendezés:

StackPanelekkel:


WrapPanellel:

2025.02.19.[helyettesítés] SQL feladatmegoldás - AI alapú önálló gyakorlás - 2. rész

A hétfői feladatsort újrahasznosítva most a Bolt adatbázist adjuk oda forrásként az AI-nak. A jövő héten pont ilyen feladatokból fogtok dol...