Advertisement
tambo21

Untitled

Oct 21st, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15.  
  16. namespace calculator
  17. {
  18.     /// <summary>
  19.     /// Logica di interazione per MainWindow.xaml
  20.     /// </summary>
  21.     public partial class MainWindow : Window
  22.     {
  23.         private void Window_KeyDown(object sender, KeyEventArgs e)
  24.         {
  25.  
  26.         }
  27.         public MainWindow()
  28.         {
  29.             InitializeComponent();
  30.         }
  31.  
  32.         private void B_Click(object sender, RoutedEventArgs e)
  33.         {
  34.             Button bt = sender as Button;
  35.  
  36.             switch(bt.Name.ToString())
  37.             {
  38.                 case "Del":
  39.                     Screen.Content = "0";
  40.                     break;
  41.                 case "B1":
  42.                     if(Screen.Content.ToString()=="0")
  43.                     {
  44.                         Screen.Content = "";
  45.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  46.                     }
  47.                     else
  48.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  49.                     break;
  50.                 case "B2":
  51.                     if (Screen.Content.ToString() == "0")
  52.                     {
  53.                         Screen.Content = "";
  54.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  55.                     }
  56.                     else
  57.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  58.                     break;
  59.                 case "B3":
  60.                     if (Screen.Content.ToString() == "0")
  61.                     {
  62.                         Screen.Content = "";
  63.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  64.                     }
  65.                     else
  66.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  67.                     break;
  68.                 case "B4":
  69.                     if (Screen.Content.ToString() == "0")
  70.                     {
  71.                         Screen.Content = "";
  72.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  73.                     }
  74.                     else
  75.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  76.                     break;
  77.                 case "B5":
  78.                     if (Screen.Content.ToString() == "0")
  79.                     {
  80.                         Screen.Content = "";
  81.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  82.                     }
  83.                     else
  84.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  85.                     break;
  86.                 case "B6":
  87.                     if (Screen.Content.ToString() == "0")
  88.                     {
  89.                         Screen.Content = "";
  90.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  91.                     }
  92.                     else
  93.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  94.                     break;
  95.                 case "B7":
  96.                     if (Screen.Content.ToString() == "0")
  97.                     {
  98.                         Screen.Content = "";
  99.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  100.                     }
  101.                     else
  102.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  103.                     break;
  104.                 case "B8":
  105.                     if (Screen.Content.ToString() == "0")
  106.                     {
  107.                         Screen.Content = "";
  108.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  109.                     }
  110.                     else
  111.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  112.                     break;
  113.                 case "B9":
  114.                     if (Screen.Content.ToString() == "0")
  115.                     {
  116.                         Screen.Content = "";
  117.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  118.                     }
  119.                     else
  120.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  121.                     break;
  122.                 case "B0":
  123.                     if (Screen.Content.ToString() == "0")
  124.                     { }
  125.                     else
  126.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  127.                     break;
  128.                 case "Vir":
  129.                     if (Screen.Content.ToString().EndsWith("+") || Screen.Content.ToString().EndsWith("-") || Screen.Content.ToString().EndsWith("x") || Screen.Content.ToString().EndsWith("/") || Screen.Content.ToString().EndsWith("."))
  130.                         break;
  131.                     else if (Screen.Content.ToString().Contains("+"))
  132.                     {
  133.                         string[] dati = Screen.Content.ToString().Split('+');
  134.                         if (dati[1].ToString().Contains("."))
  135.                         { }
  136.                         else
  137.                             Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  138.                     }
  139.                     else if (Screen.Content.ToString().Contains("-"))
  140.                     {
  141.                         string[] dati = Screen.Content.ToString().Split('-');
  142.                         if (dati[1].ToString().Contains("."))
  143.                         { }
  144.                         else
  145.                             Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  146.                     }
  147.                     else if (Screen.Content.ToString().Contains("x"))
  148.                     {
  149.                         string[] dati = Screen.Content.ToString().Split('x');
  150.                         if (dati[1].ToString().Contains("."))
  151.                         { }
  152.                         else
  153.                             Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  154.                     }
  155.                     else if (Screen.Content.ToString().Contains("/"))
  156.                     {
  157.                         string[] dati = Screen.Content.ToString().Split('/');
  158.                         if (dati[1].ToString().Contains("."))
  159.                         { }
  160.                         else
  161.                             Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  162.                     }
  163.                     else if (Screen.Content.ToString().Contains("."))
  164.                         break;
  165.                     else
  166.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  167.                     break;
  168.                 case "Somma":
  169.                     if (Screen.Content.ToString().Contains("+") || Screen.Content.ToString().Contains("-") || Screen.Content.ToString().Contains("x") || Screen.Content.ToString().Contains("/"))
  170.                         break;
  171.                     else if (Screen.Content.ToString().EndsWith("."))
  172.                         break;
  173.                     if (Screen.Content.ToString() == "0")
  174.                     { }
  175.                     else
  176.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  177.                     break;
  178.                 case "Differenza":
  179.                     if (Screen.Content.ToString().Contains("+") || Screen.Content.ToString().Contains("-") || Screen.Content.ToString().Contains("x") || Screen.Content.ToString().Contains("/"))
  180.                         break;
  181.                     else if (Screen.Content.ToString().EndsWith("."))
  182.                         break;
  183.                     if (Screen.Content.ToString() == "0")
  184.                     { }
  185.                     else
  186.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  187.                     break;
  188.                 case "Moltiplicazione":
  189.                     if (Screen.Content.ToString().Contains("+") || Screen.Content.ToString().Contains("-") || Screen.Content.ToString().Contains("x") || Screen.Content.ToString().Contains("/"))
  190.                         break;
  191.                     else if (Screen.Content.ToString().EndsWith("."))
  192.                         break;
  193.                     if (Screen.Content.ToString() == "0")
  194.                     { }
  195.                     else
  196.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  197.                     break;
  198.                 case "Divisione":
  199.                     if (Screen.Content.ToString().Contains("+") || Screen.Content.ToString().Contains("-") || Screen.Content.ToString().Contains("x") || Screen.Content.ToString().Contains("/"))
  200.                         break;
  201.                     else if (Screen.Content.ToString().EndsWith("."))
  202.                         break;
  203.                     if (Screen.Content.ToString() == "0")
  204.                     { }
  205.                     else
  206.                         Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
  207.                     break;
  208.             }
  209.         }
  210.  
  211.         private void Ris_Click(object sender, RoutedEventArgs e)
  212.         {
  213.             if(Screen.Content.ToString().Contains("+"))
  214.             {
  215.                 string[] addendi = Screen.Content.ToString().Split('+');
  216.                 Screen.Content = Convert.ToDouble(addendi[0]) + Convert.ToDouble(addendi[1]);
  217.             }
  218.             else if (Screen.Content.ToString().Contains("-"))
  219.             {
  220.                 string[] addendi = Screen.Content.ToString().Split('-');
  221.                 Screen.Content = Convert.ToDouble(addendi[0]) - Convert.ToDouble(addendi[1]);
  222.             }
  223.             else if (Screen.Content.ToString().Contains("x"))
  224.             {
  225.                 string[] addendi = Screen.Content.ToString().Split('x');
  226.                 Screen.Content = Convert.ToDouble(addendi[0]) * Convert.ToDouble(addendi[1]);
  227.             }
  228.             else if (Screen.Content.ToString().Contains("/"))
  229.             {
  230.                 string[] addendi = Screen.Content.ToString().Split('/');
  231.                 Screen.Content = Convert.ToDouble(addendi[0]) / Convert.ToDouble(addendi[1]);
  232.             }
  233.         }
  234.     }
  235. }
  236.  
  237. <Window x:Class="calculator.MainWindow"
  238.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  239.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  240.         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  241.         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  242.         xmlns:local="clr-namespace:calculator"
  243.         mc:Ignorable="d"
  244.         Title="MainWindow" Height="450" Width="299" KeyDown="Window_KeyDown">
  245.     <Grid HorizontalAlignment="Left" Width="286" Background="Black">
  246.         <Grid.RowDefinitions>
  247.             <RowDefinition></RowDefinition>
  248.             <RowDefinition></RowDefinition>
  249.             <RowDefinition></RowDefinition>
  250.             <RowDefinition></RowDefinition>
  251.             <RowDefinition></RowDefinition>
  252.             <RowDefinition></RowDefinition>
  253.         </Grid.RowDefinitions>
  254.         <Button Name="B1" Content="1" Grid.Row="2" Grid.Column="0" FontSize="40" FontStyle="Italic" Background="Black"
  255.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  256.         <Button Name="B2" Content="2" Grid.Row="2" Grid.Column="1" FontSize="40" FontStyle="Italic" Background="Black"
  257.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  258.         <Button Name="B3" Content="3" Grid.Row="2" Grid.Column="2" FontSize="40" FontStyle="Italic" Background="Black"
  259.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  260.         <Button Name="B4" Content="4" Grid.Row="3" Grid.Column="0" FontSize="40" FontStyle="Italic" Background="Black"
  261.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  262.         <Button Name="B5" Content="5" Grid.Row="3" Grid.Column="1" FontSize="40" FontStyle="Italic" Background="Black"
  263.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  264.         <Button Name="B6" Content="6" Grid.Row="3" Grid.Column="2" FontSize="40" FontStyle="Italic" Background="Black"
  265.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  266.         <Button Name="B7" Content="7" Grid.Row="4" Grid.Column="0" FontSize="40" FontStyle="Italic" Background="Black"
  267.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  268.         <Button Name="B8" Content="8" Grid.Row="4" Grid.Column="1" FontSize="40" FontStyle="Italic" Background="Black"
  269.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  270.         <Button Name="B9" Content="9" Grid.Row="4" Grid.Column="2" FontSize="40" FontStyle="Italic" Background="Black"
  271.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  272.         <Button Name="B0" Content="0" Grid.Row="5" Grid.Column="1" FontSize="40" FontStyle="Italic" Background="Black"
  273.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  274.         <Button Name="Vir" Content="." Grid.Row="5" Grid.Column="0" FontSize="40" FontStyle="Italic" Background="Black"
  275.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  276.         <Button Name="Ris" Content="=" Grid.Row="5" Grid.Column="2" FontSize="40" FontStyle="Italic" Background="Black"
  277.          Foreground="White" BorderThickness="0" Click="Ris_Click"></Button>
  278.         <Button Name="Somma" Content="+" Grid.Row="2" Grid.Column="4" FontSize="40" FontStyle="Italic" Background="DarkOrange"
  279.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  280.         <Button Name="Differenza" Content="-" Grid.Row="3" Grid.Column="4" FontSize="40" FontStyle="Italic" Background="DarkOrange"
  281.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  282.         <Button Name="Moltiplicazione" Content="x" Grid.Row="4" Grid.Column="4" FontSize="40" FontStyle="Italic"
  283.          Background="DarkOrange" Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  284.         <Button Name="Divisione" Content="/" Grid.Row="5" Grid.Column="4" FontSize="40" FontStyle="Italic" Background="DarkOrange"
  285.          Foreground="White" BorderThickness="0" Click="B_Click"></Button>
  286.         <Button Name="Del" Content="DEL" Grid.Row="1" Grid.Column="4" FontSize="27" FontStyle="Italic" Background="Black"
  287.          Foreground="red" BorderThickness="0" Click="B_Click"></Button>
  288.         <Label Name="Screen" Content="0" HorizontalContentAlignment="Right" FontSize="45" Grid.Row="0" Grid.ColumnSpan="5"
  289.          Background="Black" Foreground="Green" BorderThickness="0"></Label>
  290.         <Grid.ColumnDefinitions>
  291.             <ColumnDefinition></ColumnDefinition>
  292.             <ColumnDefinition></ColumnDefinition>
  293.             <ColumnDefinition></ColumnDefinition>
  294.             <ColumnDefinition></ColumnDefinition>
  295.         </Grid.ColumnDefinitions>
  296.     </Grid>
  297. </Window>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement