Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 calculator
- {
- /// <summary>
- /// Logica di interazione per MainWindow.xaml
- /// </summary>
- public partial class MainWindow : Window
- {
- private void Window_KeyDown(object sender, KeyEventArgs e)
- {
- }
- public MainWindow()
- {
- InitializeComponent();
- }
- private void B_Click(object sender, RoutedEventArgs e)
- {
- Button bt = sender as Button;
- switch(bt.Name.ToString())
- {
- case "Del":
- Screen.Content = "0";
- break;
- case "B1":
- if(Screen.Content.ToString()=="0")
- {
- Screen.Content = "";
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "B2":
- if (Screen.Content.ToString() == "0")
- {
- Screen.Content = "";
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "B3":
- if (Screen.Content.ToString() == "0")
- {
- Screen.Content = "";
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "B4":
- if (Screen.Content.ToString() == "0")
- {
- Screen.Content = "";
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "B5":
- if (Screen.Content.ToString() == "0")
- {
- Screen.Content = "";
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "B6":
- if (Screen.Content.ToString() == "0")
- {
- Screen.Content = "";
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "B7":
- if (Screen.Content.ToString() == "0")
- {
- Screen.Content = "";
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "B8":
- if (Screen.Content.ToString() == "0")
- {
- Screen.Content = "";
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "B9":
- if (Screen.Content.ToString() == "0")
- {
- Screen.Content = "";
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "B0":
- if (Screen.Content.ToString() == "0")
- { }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "Vir":
- if (Screen.Content.ToString().EndsWith("+") || Screen.Content.ToString().EndsWith("-") || Screen.Content.ToString().EndsWith("x") || Screen.Content.ToString().EndsWith("/") || Screen.Content.ToString().EndsWith("."))
- break;
- else if (Screen.Content.ToString().Contains("+"))
- {
- string[] dati = Screen.Content.ToString().Split('+');
- if (dati[1].ToString().Contains("."))
- { }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- }
- else if (Screen.Content.ToString().Contains("-"))
- {
- string[] dati = Screen.Content.ToString().Split('-');
- if (dati[1].ToString().Contains("."))
- { }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- }
- else if (Screen.Content.ToString().Contains("x"))
- {
- string[] dati = Screen.Content.ToString().Split('x');
- if (dati[1].ToString().Contains("."))
- { }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- }
- else if (Screen.Content.ToString().Contains("/"))
- {
- string[] dati = Screen.Content.ToString().Split('/');
- if (dati[1].ToString().Contains("."))
- { }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- }
- else if (Screen.Content.ToString().Contains("."))
- break;
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "Somma":
- if (Screen.Content.ToString().Contains("+") || Screen.Content.ToString().Contains("-") || Screen.Content.ToString().Contains("x") || Screen.Content.ToString().Contains("/"))
- break;
- else if (Screen.Content.ToString().EndsWith("."))
- break;
- if (Screen.Content.ToString() == "0")
- { }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "Differenza":
- if (Screen.Content.ToString().Contains("+") || Screen.Content.ToString().Contains("-") || Screen.Content.ToString().Contains("x") || Screen.Content.ToString().Contains("/"))
- break;
- else if (Screen.Content.ToString().EndsWith("."))
- break;
- if (Screen.Content.ToString() == "0")
- { }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "Moltiplicazione":
- if (Screen.Content.ToString().Contains("+") || Screen.Content.ToString().Contains("-") || Screen.Content.ToString().Contains("x") || Screen.Content.ToString().Contains("/"))
- break;
- else if (Screen.Content.ToString().EndsWith("."))
- break;
- if (Screen.Content.ToString() == "0")
- { }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- case "Divisione":
- if (Screen.Content.ToString().Contains("+") || Screen.Content.ToString().Contains("-") || Screen.Content.ToString().Contains("x") || Screen.Content.ToString().Contains("/"))
- break;
- else if (Screen.Content.ToString().EndsWith("."))
- break;
- if (Screen.Content.ToString() == "0")
- { }
- else
- Screen.Content = Screen.Content.ToString() + bt.Content.ToString();
- break;
- }
- }
- private void Ris_Click(object sender, RoutedEventArgs e)
- {
- if(Screen.Content.ToString().Contains("+"))
- {
- string[] addendi = Screen.Content.ToString().Split('+');
- Screen.Content = Convert.ToDouble(addendi[0]) + Convert.ToDouble(addendi[1]);
- }
- else if (Screen.Content.ToString().Contains("-"))
- {
- string[] addendi = Screen.Content.ToString().Split('-');
- Screen.Content = Convert.ToDouble(addendi[0]) - Convert.ToDouble(addendi[1]);
- }
- else if (Screen.Content.ToString().Contains("x"))
- {
- string[] addendi = Screen.Content.ToString().Split('x');
- Screen.Content = Convert.ToDouble(addendi[0]) * Convert.ToDouble(addendi[1]);
- }
- else if (Screen.Content.ToString().Contains("/"))
- {
- string[] addendi = Screen.Content.ToString().Split('/');
- Screen.Content = Convert.ToDouble(addendi[0]) / Convert.ToDouble(addendi[1]);
- }
- }
- }
- }
- <Window x:Class="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:calculator"
- mc:Ignorable="d"
- Title="MainWindow" Height="450" Width="299" KeyDown="Window_KeyDown">
- <Grid HorizontalAlignment="Left" Width="286" Background="Black">
- <Grid.RowDefinitions>
- <RowDefinition></RowDefinition>
- <RowDefinition></RowDefinition>
- <RowDefinition></RowDefinition>
- <RowDefinition></RowDefinition>
- <RowDefinition></RowDefinition>
- <RowDefinition></RowDefinition>
- </Grid.RowDefinitions>
- <Button Name="B1" Content="1" Grid.Row="2" Grid.Column="0" FontSize="40" FontStyle="Italic" Background="Black"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="B2" Content="2" Grid.Row="2" Grid.Column="1" FontSize="40" FontStyle="Italic" Background="Black"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="B3" Content="3" Grid.Row="2" Grid.Column="2" FontSize="40" FontStyle="Italic" Background="Black"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="B4" Content="4" Grid.Row="3" Grid.Column="0" FontSize="40" FontStyle="Italic" Background="Black"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="B5" Content="5" Grid.Row="3" Grid.Column="1" FontSize="40" FontStyle="Italic" Background="Black"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="B6" Content="6" Grid.Row="3" Grid.Column="2" FontSize="40" FontStyle="Italic" Background="Black"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="B7" Content="7" Grid.Row="4" Grid.Column="0" FontSize="40" FontStyle="Italic" Background="Black"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="B8" Content="8" Grid.Row="4" Grid.Column="1" FontSize="40" FontStyle="Italic" Background="Black"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="B9" Content="9" Grid.Row="4" Grid.Column="2" FontSize="40" FontStyle="Italic" Background="Black"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="B0" Content="0" Grid.Row="5" Grid.Column="1" FontSize="40" FontStyle="Italic" Background="Black"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="Vir" Content="." Grid.Row="5" Grid.Column="0" FontSize="40" FontStyle="Italic" Background="Black"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="Ris" Content="=" Grid.Row="5" Grid.Column="2" FontSize="40" FontStyle="Italic" Background="Black"
- Foreground="White" BorderThickness="0" Click="Ris_Click"></Button>
- <Button Name="Somma" Content="+" Grid.Row="2" Grid.Column="4" FontSize="40" FontStyle="Italic" Background="DarkOrange"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="Differenza" Content="-" Grid.Row="3" Grid.Column="4" FontSize="40" FontStyle="Italic" Background="DarkOrange"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="Moltiplicazione" Content="x" Grid.Row="4" Grid.Column="4" FontSize="40" FontStyle="Italic"
- Background="DarkOrange" Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="Divisione" Content="/" Grid.Row="5" Grid.Column="4" FontSize="40" FontStyle="Italic" Background="DarkOrange"
- Foreground="White" BorderThickness="0" Click="B_Click"></Button>
- <Button Name="Del" Content="DEL" Grid.Row="1" Grid.Column="4" FontSize="27" FontStyle="Italic" Background="Black"
- Foreground="red" BorderThickness="0" Click="B_Click"></Button>
- <Label Name="Screen" Content="0" HorizontalContentAlignment="Right" FontSize="45" Grid.Row="0" Grid.ColumnSpan="5"
- Background="Black" Foreground="Green" BorderThickness="0"></Label>
- <Grid.ColumnDefinitions>
- <ColumnDefinition></ColumnDefinition>
- <ColumnDefinition></ColumnDefinition>
- <ColumnDefinition></ColumnDefinition>
- <ColumnDefinition></ColumnDefinition>
- </Grid.ColumnDefinitions>
- </Grid>
- </Window>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement