Pessoal este código cria uma calculadora no C# semelhante a do Windows. Guardando as devidas proporções.
Abaixo estão as listagens do arquivos
MainWindow.xaml
MainWindow.xaml.cs
MainWindow.xaml
<Window x:Class="Calculadora.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="404" Width="426" Background="#FFEBE5E5" WindowStyle="ThreeDBorderWindow">
<Grid Width="358">
<Button Content="7" Height="28" HorizontalAlignment="Left" Margin="91,127,0,0" Name="sete" VerticalAlignment="Top" Width="37" Click="sete_Click" />
<TextBox Height="53" HorizontalAlignment="Right" Margin="0,34,54,0" Name="display" VerticalAlignment="Top" Width="213" FontFamily="Arial" FontSize="28" HorizontalContentAlignment="Right" />
<Button Content="8" Height="28" HorizontalAlignment="Left" Margin="134,127,0,0" Name="oito" VerticalAlignment="Top" Width="37" Click="oito_Click" />
<Button Content="9" Height="28" HorizontalAlignment="Left" Margin="177,127,0,0" Name="nove" VerticalAlignment="Top" Width="37" Click="nove_Click" />
<Button Content="4" Height="28" HorizontalAlignment="Left" Margin="91,161,0,0" Name="quatro" VerticalAlignment="Top" Width="37" Click="quatro_Click" />
<Button Content="5" Height="28" HorizontalAlignment="Left" Margin="134,161,0,0" Name="cinco" VerticalAlignment="Top" Width="37" Click="cinco_Click" />
<Button Content="6" Height="28" HorizontalAlignment="Left" Margin="177,161,0,0" Name="seis" VerticalAlignment="Top" Width="37" Click="seis_Click" />
<Button Content="1" Height="28" HorizontalAlignment="Left" Margin="91,195,0,0" Name="um" VerticalAlignment="Top" Width="37" Click="um_Click" />
<Button Content="2" Height="28" HorizontalAlignment="Left" Margin="134,195,0,0" Name="dois" VerticalAlignment="Top" Width="37" Click="dois_Click" />
<Button Content="3" Height="28" HorizontalAlignment="Left" Margin="177,195,0,0" Name="tres" VerticalAlignment="Top" Width="37" Click="tres_Click" />
<Button Content="0" Height="28" HorizontalAlignment="Left" Margin="91,229,0,0" Name="zero" VerticalAlignment="Top" Width="80" Click="zero_Click" />
<Button Content="," Height="28" HorizontalAlignment="Left" Margin="177,229,0,0" Name="virgula" VerticalAlignment="Top" Width="37" FontSize="18" Click="virgula_Click" />
<Button Content="+" Height="28" HorizontalAlignment="Left" Margin="220,229,0,0" Name="soma" VerticalAlignment="Top" Width="37" Click="soma_Click" />
<Button Content="-" Height="28" HorizontalAlignment="Left" Margin="220,195,0,0" Name="subtracao" VerticalAlignment="Top" Width="37" Click="subtracao_Click" />
<Button Content="*" Height="28" HorizontalAlignment="Left" Margin="220,0,0,176" Name="multiplicacao" VerticalAlignment="Bottom" Width="37" Click="multiplicacao_Click" />
<Button Content="/" Height="28" HorizontalAlignment="Left" Margin="220,127,0,0" Name="divisao" VerticalAlignment="Top" Width="37" Click="divisao_Click" />
<Button Content="=" Height="60" HorizontalAlignment="Left" Margin="267,198,0,0" Name="igual" VerticalAlignment="Top" Width="37" Click="igual_Click" />
<Button Content="1/x" Height="28" HorizontalAlignment="Left" Margin="267,0,0,176" Name="inverso" VerticalAlignment="Bottom" Width="37" Click="inverso_Click" />
<Button Content="%" Height="28" HorizontalAlignment="Left" Margin="267,127,0,0" Name="percente" VerticalAlignment="Top" Width="37" />
<Button Content="<--" Height="28" HorizontalAlignment="Left" Margin="91,93,0,0" Name="voltar" VerticalAlignment="Top" Width="37" Click="voltar_Click" />
<Button Content="CE" Height="28" HorizontalAlignment="Left" Margin="134,93,0,0" Name="button1" VerticalAlignment="Top" Width="37" Click="button1_Click" />
<Button Content="C" Height="28" HorizontalAlignment="Left" Margin="177,93,0,0" Name="button2" VerticalAlignment="Top" Width="37" Click="button2_Click" />
<Button Height="28" HorizontalAlignment="Right" Margin="0,93,54,0" Name="raiz" VerticalAlignment="Top" Width="37" Content="√" Click="raiz_Click" />
<Button Height="28" HorizontalAlignment="Left" Margin="220,93,0,0" Name="maisoumenos" VerticalAlignment="Top" Width="37" Content="±" Click="maisoumenos_Click" />
</Grid>
</Window>
MainWindow.xaml.cs
System;
using System.Collections.Generic;
using System.Linq;
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;
namespace Calculadora
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
double acumula = 0;
string operacao = "";
public MainWindow()
{
InitializeComponent();
}
private void sete_Click(object sender, RoutedEventArgs e)
{
display.Text += "7";
}
private void zero_Click(object sender, RoutedEventArgs e)
{
display.Text += "0";
}
private void um_Click(object sender, RoutedEventArgs e)
{
display.Text += "1";
}
private void dois_Click(object sender, RoutedEventArgs e)
{
display.Text += "2";
}
private void tres_Click(object sender, RoutedEventArgs e)
{
display.Text += "3";
}
private void quatro_Click(object sender, RoutedEventArgs e)
{
display.Text += "4";
}
private void cinco_Click(object sender, RoutedEventArgs e)
{
display.Text += "5";
}
private void seis_Click(object sender, RoutedEventArgs e)
{
display.Text += "6";
}
private void oito_Click(object sender, RoutedEventArgs e)
{
display.Text += "8";
}
private void nove_Click(object sender, RoutedEventArgs e)
{
display.Text += "9";
}
private void soma_Click(object sender, RoutedEventArgs e)
{
if (operacao == "*" || operacao=="-" || operacao == "/")
{
operacao = "+";
}
else
{
acumula += double.Parse(display.Text);
display.Text = "";
operacao = "+";
}
}
private void igual_Click(object sender, RoutedEventArgs e)
{
if (operacao == "+")
{
acumula += double.Parse(display.Text);
display.Text = acumula.ToString();
}
else if (operacao == "-")
{
acumula -= double.Parse(display.Text);
display.Text = acumula.ToString();
}
else if (operacao=="*"){
acumula *= double.Parse(display.Text);
display.Text = acumula.ToString();
}
else if (operacao == "/") {
if (double.Parse(display.Text) != 0)
{
acumula /= double.Parse(display.Text);
display.Text = acumula.ToString();
}
else {
display.Text = "Divisão por zero";
}
}
}
private void virgula_Click(object sender, RoutedEventArgs e)
{
display.Text += ",";
}
private void subtracao_Click(object sender, RoutedEventArgs e)
{
if (operacao == "*" || operacao == "+" || operacao == "/")
{
operacao = "-";
} else {
acumula = double.Parse(display.Text);
display.Text = "";
operacao = "-";
}
}
private void multiplicacao_Click(object sender, RoutedEventArgs e)
{
if (operacao == "-" || operacao == "+" || operacao == "/")
{
operacao = "*";
}
else
{
acumula = double.Parse(display.Text);
display.Text = "";
operacao = "*";
}
}
private void divisao_Click(object sender, RoutedEventArgs e)
{
if (operacao == "*" || operacao == "+" || operacao == "-")
{
operacao = "/";
}
else
{
acumula = double.Parse(display.Text);
display.Text = "";
operacao = "/";}
}
private void voltar_Click(object sender, RoutedEventArgs e)
{
int x = display.Text.Length-1;
if (x >= 0)
{
display.Text = display.Text.Substring(0, x);
}
}
private void inverso_Click(object sender, RoutedEventArgs e)
{
try
{
double d = double.Parse(display.Text);
if (d == 0)
{
display.Text = "Divisão por zero!!!";
}
else
{
d = 1 / d;
display.Text = d.ToString();
}
}
catch (Exception Ex) {
for (int i = 0; i < 100000; i++)
{
display.Text = "vc é bobo. ";
// em espera.
}
display.Text = "";
}
}
private void raiz_Click(object sender, RoutedEventArgs e)
{
double x = double.Parse(display.Text);
if (x < 0)
{
display.Text = "Valor invalido.";
}
else {
x = Math.Sqrt(x);
display.Text = x.ToString();
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
acumula = 0;
display.Text = "";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
display.Text = "";
operacao = "";
}
private void maisoumenos_Click(object sender, RoutedEventArgs e)
{
double x = double.Parse(display.Text)*(-1);
display.Text = x.ToString();
}
}
}
Espero que tenham gostado do exemplo, vai um + ai? Mostre também para seus colegas o que estão aprendendo no curso através das redes sociais. Um Forte abraço e até o próximo post.