Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <Window x:Class="WpfApp1.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:WpfApp1"
- mc:Ignorable="d"
- Title="Rechner" Height="415.254" Width="525">
- <Grid>
- <Button x:Name="btSeven" Content="7" HorizontalAlignment="Left" Margin="42,92,0,0" VerticalAlignment="Top" Width="84" Height="45" FontSize="18" Click="btSeven_Click"/>
- <Button x:Name="btEight" Content="8" HorizontalAlignment="Left" Height="45" Margin="131,92,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btEight_Click"/>
- <Button x:Name="btFour" Content="4" HorizontalAlignment="Left" Height="45" Margin="42,142,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btFour_Click"/>
- <Button x:Name="btTwo" Content="2" HorizontalAlignment="Left" Height="45" Margin="131,192,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btTwo_Click"/>
- <Button x:Name="btOne" Content="1" HorizontalAlignment="Left" Height="45" Margin="42,192,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btOne_Click"/>
- <Button x:Name="btFive" Content="5" HorizontalAlignment="Left" Height="45" Margin="131,142,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btFive_Click"/>
- <Button x:Name="btNine" Content="9" HorizontalAlignment="Left" Height="45" Margin="220,92,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btNine_Click"/>
- <Button x:Name="btClear" Content="C" HorizontalAlignment="Left" Height="45" Margin="220,242,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btClear_Click"/>
- <Button x:Name="btSix" Content="6" HorizontalAlignment="Left" Height="45" Margin="220,142,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btSix_Click"/>
- <Button x:Name="btEquals" Content="=" HorizontalAlignment="Left" Height="45" Margin="42,310,0,0" VerticalAlignment="Top" Width="412" FontSize="22" Click="btEquals_Click"/>
- <Button x:Name="btDivision" Content="/" HorizontalAlignment="Left" Height="45" Margin="370,92,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btDivision_Click"/>
- <Button x:Name="btMultiplikation" Content="*" HorizontalAlignment="Left" Height="45" Margin="370,142,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btMultiplikation_Click"/>
- <Button x:Name="btPlus" Content="+" HorizontalAlignment="Left" Height="45" Margin="370,192,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btPlus_Click"/>
- <Button x:Name="btZero" Content="0" HorizontalAlignment="Left" Height="45" Margin="42,242,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btZero_Click"/>
- <Button x:Name="btComma" Content="." HorizontalAlignment="Left" Height="45" Margin="131,242,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btComma_Click"/>
- <Button x:Name="btThree" Content="3" HorizontalAlignment="Left" Height="45" Margin="220,192,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btThree_Click"/>
- <Button x:Name="btMinus" Content="-" HorizontalAlignment="Left" Height="45" Margin="370,242,0,0" VerticalAlignment="Top" Width="84" FontSize="18" Click="btMinus_Click"/>
- <Label x:Name="lbOut" Content="" HorizontalAlignment="Left" Height="60" Margin="42,16,0,0" VerticalAlignment="Top" Width="412" FontSize="36" RenderTransformOrigin="0.502,0.441"/>
- </Grid>
- </Window>
- 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 WpfApp1
- {
- /// <summary>
- /// Interaktionslogik für MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- string input = string.Empty;
- string operand1 = string.Empty;
- string operand2 = string.Empty;
- char Operation;
- double result = 0.0;
- public MainWindow()
- {
- InitializeComponent();
- }
- private void Calculate()
- {
- lbOut.Content = "";
- operand2 = input;
- double num1, num2;
- double.TryParse(operand1, out num1);
- double.TryParse(operand2, out num2);
- if (Operation == '+')
- {
- result = num1 + num2;
- lbOut.Content = result.ToString();
- }
- else if (Operation == '-')
- {
- result = num1 - num2;
- lbOut.Content = result.ToString();
- }
- else if (Operation == '*')
- {
- result = num1 * num2;
- lbOut.Content = result.ToString();
- }
- else if (Operation == '/')
- {
- if (num2 != 0)
- {
- result = num1 / num2;
- lbOut.Content = result.ToString();
- }
- else
- {
- lbOut.Content = "DIV/Zero!";
- }
- }
- input = string.Empty;
- operand1 = string.Empty;
- }
- private void btSeven_Click(object sender, RoutedEventArgs e)
- {
- input += "7";
- if (operand1 == string.Empty)
- {
- lbOut.Content = input;
- }
- else
- {
- lbOut.Content += "7";
- }
- }
- private void btEight_Click(object sender, RoutedEventArgs e)
- {
- input += "8";
- if (operand1 == string.Empty)
- {
- lbOut.Content = input;
- }
- else
- {
- lbOut.Content += "8";
- }
- }
- private void btNine_Click(object sender, RoutedEventArgs e)
- {
- input += "9";
- if (operand1 == string.Empty)
- {
- lbOut.Content = input;
- }
- else
- {
- lbOut.Content += "9";
- }
- }
- private void btDivision_Click(object sender, RoutedEventArgs e)
- {
- if (operand1 == string.Empty)
- {
- if (lbOut.Content.ToString() != "DIV/Zero!")
- {
- if (input == string.Empty)
- {
- operand1 = result.ToString();
- }
- else
- {
- operand1 = input;
- }
- }
- else
- {
- lbOut.Content = "";
- operand1 = string.Empty;
- }
- Operation = '/';
- result = 0.0;
- input = string.Empty;
- }
- else
- {
- Calculate();
- Operation = '/';
- operand1 = result.ToString();
- result = 0.0;
- }
- lbOut.Content += "/";
- }
- private void btFour_Click(object sender, RoutedEventArgs e)
- {
- input += "4";
- if (operand1 == string.Empty)
- {
- lbOut.Content = input;
- }
- else
- {
- lbOut.Content += "4";
- }
- }
- private void btFive_Click(object sender, RoutedEventArgs e)
- {
- input += "5";
- if (operand1 == string.Empty)
- {
- lbOut.Content = input;
- }
- else
- {
- lbOut.Content += "5";
- }
- }
- private void btSix_Click(object sender, RoutedEventArgs e)
- {
- input += "6";
- if (operand1 == string.Empty)
- {
- lbOut.Content = input;
- }
- else
- {
- lbOut.Content += "6";
- }
- }
- private void btMultiplikation_Click(object sender, RoutedEventArgs e)
- {
- if (operand1 == string.Empty)
- {
- if (lbOut.Content.ToString() != "DIV/Zero!")
- {
- if (input == string.Empty)
- {
- operand1 = result.ToString();
- }
- else
- {
- operand1 = input;
- }
- }
- else
- {
- lbOut.Content = "";
- operand1 = string.Empty;
- }
- Operation = '*';
- result = 0.0;
- input = string.Empty;
- }
- else
- {
- Calculate();
- Operation = '*';
- operand1 = result.ToString();
- result = 0.0;
- }
- lbOut.Content += "*";
- }
- private void btOne_Click(object sender, RoutedEventArgs e)
- {
- input += "1";
- if (operand1 == string.Empty)
- {
- lbOut.Content = input;
- }
- else
- {
- lbOut.Content += "1";
- }
- }
- private void btTwo_Click(object sender, RoutedEventArgs e)
- {
- input += "2";
- if (operand1 == string.Empty)
- {
- lbOut.Content = input;
- }
- else
- {
- lbOut.Content += "2";
- }
- }
- private void btThree_Click(object sender, RoutedEventArgs e)
- {
- input += "3";
- if (operand1 == string.Empty)
- {
- lbOut.Content = input;
- }
- else
- {
- lbOut.Content += "3";
- }
- }
- private void btPlus_Click(object sender, RoutedEventArgs e)
- {
- if (operand1 == string.Empty)
- {
- if (lbOut.Content.ToString() != "DIV/Zero!")
- {
- if (input == string.Empty)
- {
- operand1 = result.ToString();
- }
- else
- {
- operand1 = input;
- }
- }
- else
- {
- lbOut.Content = "";
- operand1 = string.Empty;
- }
- Operation = '+';
- result = 0.0;
- input = string.Empty;
- }
- else
- {
- Calculate();
- Operation = '+';
- operand1 = result.ToString();
- result = 0.0;
- }
- lbOut.Content += "+";
- }
- private void btZero_Click(object sender, RoutedEventArgs e)
- {
- input += "0";
- if (operand1 == string.Empty)
- {
- lbOut.Content = input;
- }
- else
- {
- lbOut.Content += "0";
- }
- }
- private void btComma_Click(object sender, RoutedEventArgs e)
- {
- lbOut.Content = "";
- if (input == string.Empty)
- {
- input += "0.";
- }
- else
- {
- input += ".";
- }
- lbOut.Content = input;
- }
- private void btClear_Click(object sender, RoutedEventArgs e)
- {
- lbOut.Content = "";
- input = string.Empty;
- operand1 = string.Empty;
- operand2 = string.Empty;
- }
- private void btMinus_Click(object sender, RoutedEventArgs e)
- {
- if (operand1 == string.Empty)
- {
- if (lbOut.Content.ToString() != "DIV/Zero!")
- {
- if (input == string.Empty)
- {
- operand1 = result.ToString();
- }
- else
- {
- operand1 = input;
- }
- }
- else
- {
- lbOut.Content = "";
- operand1 = string.Empty;
- }
- Operation = '-';
- result = 0.0;
- input = string.Empty;
- }
- else
- {
- Calculate();
- Operation = '-';
- operand1 = result.ToString();
- result = 0.0;
- }
- lbOut.Content += "-";
- }
- private void btEquals_Click(object sender, RoutedEventArgs e)
- {
- Calculate();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement