C# :: Aufgabe #177 :: Lösung #4

4 Lösungen Lösungen öffentlich
#177

Schneller als der Taschenrechner

Anfänger - C# von hollst - 16.05.2017 um 16:39 Uhr
Wenn ihr die folgende Aufgabe programmiert habt, könnt ihr auf jeder Party durch eure brilliante Kopfrechenleistung glänzend, ihr werdet schneller sein als jeder andere, selbst wenn er einen Tscherechner oder euren benutzen darf.

Zu programmieren ist ein kleiner, aber sehr spezieller Taschenrechner (Bild 1), der folgendes kann:

1) Entgegennahme einer dreistelligen positiven, geraden Ganzzahl (Bild 2).
2) Vergrößern der Dreistellenzahl auf sechs Stellen, indem die Zahl einfach noch einmal angehängt wird (Bild 3).
3) … 5) (komplizierte) Division der Sechstellenzahl nacheinander durch 22, 7 und 13. Reihenfolge spielt keine Rolle, aber nur jeweils 1x (deshalb deuten rote Buttons auf „nicht mehr benutzbar“ hin).

Im Beispiel (Bild 1 … 6) habt ihr allerdings das Ergebnis (117) wesentlich schneller per Kopf ausgerechnet als euer Freund mit dem Taschenrechner. Das liegt nicht daran, dass der Rechner so langsam rechnet, sondern daran, das das Buttondrücken so lange dauert.

Die ganze Übung vor größerem Publikum solltet ihr allerdings nach etwa höchstens 10 Vorführungen zunächst abbrechen, also noch bevor jemand hinter den Trick kommt. Was der Trick ist? Nun, bitte erst einmal selber nachdenken.
#4
vote_ok
von hollst (13980 Punkte) - 03.06.2017 um 17:32 Uhr
Quellcode ausblenden C#-Code
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;

namespace jocke_calculator  {

    public partial class MainWindow : Window    {

        public MainWindow()        {

            InitializeComponent();
            bt_reset_Click(null, null);
        }

        private uint number;
        private bool[] bo_button_free = new bool[] {true, false, false, false };
        

        private void bt_reset_Click(object sender, RoutedEventArgs e)        {

            this.bo_button_free = new bool[] { true, false, false, false };
            this.set_color();
            this.tb_inout.Clear();
            this.tb_inout.Focus();
        }

        private SolidColorBrush brush_true = Brushes.Green, brush_false = Brushes.Red;
       
        private void set_color()        {

            if (this.bo_button_free[0]) this.bt_expand.Background = brush_true; 
            else this.bt_expand.Background = brush_false;

            if (this.bo_button_free[1]) this.bt_22.Background = brush_true;
            else this.bt_22.Background = brush_false;

            if (this.bo_button_free[2]) this.bt_7.Background = brush_true;
            else this.bt_7.Background = brush_false;

            if (this.bo_button_free[3]) this.bt_13.Background = brush_true;
            else this.bt_13.Background = brush_false;
        }

        private void tb_inout_KeyDown(object sender, KeyEventArgs e)        {
            if (e.Key == Key.Return)
                this.bt_expand_Click(null, null);
        }

        private void error()
        { MessageBox.Show("input not okay! just a positive three-digit even number (100 ... 998)"); }

        private void bt_expand_Click(object sender, RoutedEventArgs e)        {
            if (!bo_button_free[0])
                return;

            uint content;
            bool bo_content_ok = uint.TryParse(this.tb_inout.Text, out content);
            if (!bo_content_ok)
                error();
            else
                if (this.tb_inout.Text.Length != 3)
                    error();
                else    {
                    if (content % 2 == 1)
                        content--;
                    this.number = content * 1001;
                    this.tb_inout.Text = this.number.ToString();
                    this.bo_button_free = new bool[] { false, true, true, true };
                    this.set_color();
                }
        }

        private void bt_22_Click(object sender, RoutedEventArgs e)        {
            if (!bo_button_free[1])
                return;
            this.number /= 22;
            this.tb_inout.Text = this.number.ToString();
            bo_button_free[1] = false;
            this.set_color();
        }

        private void bt_7_Click(object sender, RoutedEventArgs e)        {
            if (!bo_button_free[2])
                return;
            this.number /= 7;
            this.tb_inout.Text = this.number.ToString();
            bo_button_free[2] = false;
            this.set_color();
        }
 
        private void bt_13_Click(object sender, RoutedEventArgs e)        {
            if (!bo_button_free[3])
                return;
            this.number /= 13;
            this.tb_inout.Text = this.number.ToString();
            bo_button_free[3] = false;
            this.set_color();
        }
    }
}

<Window x:Name="main_window" x:Class="jocke_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:jocke_calculator"
        mc:Ignorable="d"
        Title="special calculator" Height="369.231" Width="525" FontFamily="Courier New" Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}" WindowStartupLocation="CenterScreen" WindowStyle="ToolWindow">
    <Grid>
        <StackPanel Margin="50,5">
            <Button x:Name="bt_reset" Content="reset" Height="50" Margin="0,0,0,5" FontSize="36" FontWeight="Bold" Click="bt_reset_Click"/>
            <TextBox x:Name="tb_inout" Height="50" TextWrapping="Wrap" Text="TextBox" Margin="100,0,100,5" FontSize="36" FontWeight="Bold" KeyDown="tb_inout_KeyDown" />
            <Button x:Name="bt_expand" Content="expand" Height="50" Margin="0,0,0,5" FontSize="36" FontWeight="Bold" Click="bt_expand_Click"/>
            <Button x:Name="bt_22" Content="/22" Height="50" Margin="0,0,0,5" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36" FontWeight="Bold" Click="bt_22_Click"/>
            <Button x:Name="bt_7"  Content="/7 " Height="50" Margin="0,0,0,5" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36" FontWeight="Bold" Click="bt_7_Click"/>
            <Button x:Name="bt_13" Content="/13" Height="50" Margin="0,0,0,5" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="36" FontWeight="Bold" Click="bt_13_Click"/>

        </StackPanel>

    </Grid>
</Window>

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben