Ir ao conteúdo
  • Cadastre-se

Como Programar uma calculadora no c#?


EdwardTheHead

Posts recomendados

Meu Professor de lingugem de programaçao pediu q fizessemos uma calculadora usando o metdo mvc mais nao estou conseguindo , pls me ajudem

Camada model:

amespace Calculadora2.Model
{
    public class Calcula
    {
        public class CALCULA
        {
            public enum Operacao
            {
                Soma = 1,
                Subtracao = 2,
                Divisao = 3,
                Multiplicacao = 4,
                Nenhuma = 5
            }
            private Decimal _Resultado;
            private Decimal _Memoria;
            private Operacao _AcaoPendente;


            public Decimal Resultado
            {
                get { return _Resultado; }
                set { _Resultado = value; }
            }
            public Decimal Memoria
            {
                get { return _Memoria; }
                set { _Memoria = value; }
            }
            public Operacao AcaoPendente
            {
                get { return _AcaoPendente; }
                set { _AcaoPendente = value; }
            }

            public Decimal CalculaOperacao(Decimal valor)
            {
                if (AcaoPendente == Operacao.Soma)
                {
                    _Resultado = _Resultado+valor;
                }

                else if (AcaoPendente == Operacao.Subtracao)
                {
                    _Resultado=_Resultado - valor;
                }
                else if (AcaoPendente == Operacao.Multiplicacao)
                {
                    _Resultado=_Resultado * valor;
                }
                else if (AcaoPendente == Operacao.Divisao)
                {
                    _Resultado = _Resultado / valor;
                }
                return Resultado;
            }
        }
    }
}

Camada Control:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Calculadora2.Model;
namespace Calculadora2.Control
{
    public class CCalculadora
    {
        Calcula aAcao = new Calcula();

        private Decimal CalculaOperacao(Decimal AcaoPendente)
        {
            aAcao.AcaoPendente = AcaoPendente;
            return aAcao.CalculaOpercao();
        }


        

    }
}
Camada view:


        public Form1()
        {
            InitializeComponent();
        }

        //botões dos Números
        private void BtnUm_Click(object sender, EventArgs e)
        {
            LblResultado.Text = LblResultado.Text + 1;
        }

        private void BtnDois_Click(object sender, EventArgs e)
        {
            LblResultado.Text = LblResultado.Text + 2;
        }

        private void BtnTres_Click(object sender, EventArgs e)
        {
            LblResultado.Text = LblResultado.Text + 3;
        }

        private void BtnQuatro_Click(object sender, EventArgs e)
        {
            LblResultado.Text = LblResultado.Text + 4;
        }

        private void BtnCinco_Click(object sender, EventArgs e)
        {
            LblResultado.Text = LblResultado.Text + 5;
        }

        private void BtnSeis_Click(object sender, EventArgs e)
        {
            LblResultado.Text = LblResultado.Text + 6;
        }

        private void BtnSete_Click(object sender, EventArgs e)
        {
            LblResultado.Text = LblResultado.Text + 7;
        }

        private void BtnOito_Click(object sender, EventArgs e)
        {
            LblResultado.Text = LblResultado.Text + 8;
        }

        private void BtnNove_Click(object sender, EventArgs e)
        {
            LblResultado.Text = LblResultado.Text + 9;
        }

        private void BtnZero_Click(object sender, EventArgs e)
        {
            LblResultado.Text = LblResultado.Text + 0;
        }

        private void BtnVirgula_Click(object sender, EventArgs e)
        {
            LblResultado.Text = LblResultado.Text + ",";
        }

        //Botão BackSpace e LimpaTela
        private void BtnLimpa_Click(object sender, EventArgs e)
        {
            LblResultado.Text = "";

        }

        private void BtnBackSpace_Click(object sender, EventArgs e)
        {
            int quantidade = 0;
            quantidade = LblResultado.Text.Length;
            if (quantidade > 0)
                LblResultado.Text = LblResultado.Text.Substring(0, quantidade - 1);
        }

        //Botões Operações
        private void BtnDivide_Click(object sender, EventArgs e)
        {

        }

        private void BtnMultiplica_Click(object sender, EventArgs e)
        {

        }

        private void BtnMenos_Click(object sender, EventArgs e)
        {

        }

        private void BtnMais_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}
 

Link para o comentário
Compartilhar em outros sites

Segue abaixo

namespace Calculadora_csharp
{
    public partial class Form1 : Form
    {
        char Operator;
        double Result;
        string Input;
        string Operand1;
        string Operand2;

        public Form1()
        {
            InitializeComponent();
            Result = 0.0;
            Input = string.Empty;
            Operand1 = string.Empty;
            Operand2 = string.Empty;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        { // Button 0
            if(Input.Length == 1)
            {
                if (Input[0] == '0')
                    return;
            }

            Input += 0;
            textBox1.ResetText();
            textBox1.AppendText(Input);
            textBox1.Update();
        }

        private void button3_Click(object sender, EventArgs e)
        { // Button ,
            if (Input == string.Empty)
                return;

            Input += ',';
            textBox1.ResetText();
            textBox1.AppendText(Input);
            textBox1.Update();

            button3.Enabled = false;
        }

        private void button5_Click(object sender, EventArgs e)
        { // Button 1
            if(Input.Length == 1)
            {
                if (Input[0] == '0')
                    Input = string.Empty;
            }

            if (Input.Length < 16)
            {
                Input += 1;
                textBox1.ResetText();
                textBox1.AppendText(Input);
                textBox1.Update();
            }
        }

        private void button6_Click(object sender, EventArgs e)
        { // Button 2
            if (Input.Length == 1)
            {
                if (Input[0] == '0')
                    Input = string.Empty;
            }

            if (Input.Length < 16)
            {
                Input += 2;
                textBox1.ResetText();
                textBox1.AppendText(Input);
                textBox1.Update();
            }
        }

        private void button7_Click(object sender, EventArgs e)
        { // Button 3
            if (Input.Length == 1)
            {
                if (Input[0] == '0')
                    Input = string.Empty;
            }

            if (Input.Length < 16)
            {
                Input += 3;
                textBox1.ResetText();
                textBox1.AppendText(Input);
                textBox1.Update();
            }
        }

        private void button9_Click(object sender, EventArgs e)
        { // Button 4
            if (Input.Length == 1)
            {
                if (Input[0] == '0')
                    Input = string.Empty;
            }

            if (Input.Length < 16)
            {
                Input += 4;
                textBox1.ResetText();
                textBox1.AppendText(Input);
                textBox1.Update();
            }
        }

        private void button10_Click(object sender, EventArgs e)
        { // Button 5
            if (Input.Length == 1)
            {
                if (Input[0] == '0')
                    Input = string.Empty;
            }

            if (Input.Length < 16)
            {
                Input += 5;
                textBox1.ResetText();
                textBox1.AppendText(Input);
                textBox1.Update();
            }
        }

        private void button11_Click(object sender, EventArgs e)
        { // Button 6
            if (Input.Length == 1)
            {
                if (Input[0] == '0')
                    Input = string.Empty;
            }

            if (Input.Length < 16)
            {
                Input += 6;
                textBox1.ResetText();
                textBox1.AppendText(Input);
                textBox1.Update();
            }
        }

        private void button13_Click(object sender, EventArgs e)
        { // Button 7
            if (Input.Length == 1)
            {
                if (Input[0] == '0')
                    Input = string.Empty;
            }

            if (Input.Length < 16)
            {
                Input += 7;
                textBox1.ResetText();
                textBox1.AppendText(Input);
                textBox1.Update();
            }
        }

        private void button14_Click(object sender, EventArgs e)
        { // Button 8
            if (Input.Length == 1)
            {
                if (Input[0] == '0')
                    Input = string.Empty;
            }

            if (Input.Length < 16)
            {
                Input += 8;
                textBox1.ResetText();
                textBox1.AppendText(Input);
                textBox1.Update();
            }
        }

        private void button15_Click(object sender, EventArgs e)
        { // Button 9
            if (Input.Length == 1)
            {
                if (Input[0] == '0')
                    Input = string.Empty;
            }

            if(Input.Length < 16)
            {
                Input += 9;
                textBox1.ResetText();
                textBox1.AppendText(Input);
                textBox1.Update();
            }
        }

        private void button19_Click(object sender, EventArgs e)
        { // Button < :Pop back last entry
            int lastInput = (Input.Length - 1);

            if (Input.Length > 0)
            {
                if (Input[lastInput] == ',')
                    button3.Enabled = true;
                    
                Input = Input.Remove(lastInput);
            }
            if(Input.Length == 0)
                Input += 0;

            textBox1.ResetText();
            textBox1.AppendText(Input);
            textBox1.Update();
        }

        private void button17_Click(object sender, EventArgs e)
        { // Button CE :Empty current entry
            Input = string.Empty;
            Input += 0;
            textBox1.ResetText();
            textBox1.AppendText(Input);
            textBox1.Update();

            button3.Enabled = true;
        }

        private void button18_Click(object sender, EventArgs e)
        { // Button C :Empty operation
            Input = string.Empty;
            Input += 0;
            textBox1.ResetText();
            textBox1.AppendText(Input);
            textBox1.Update();

            Operand1 = string.Empty;
            Operand2 = string.Empty;

            button8.Enabled = true;
            button16.Enabled = true;
            button12.Enabled = true;
            button20.Enabled = true;
            button4.Enabled = true;
            button3.Enabled = true;
            button17.Enabled = true;
            button19.Enabled = true;
        }

        private void button8_Click(object sender, EventArgs e)
        { // Operator +
            Operator = '+';

            Operand1 = Input;
            Input = string.Empty;
            button8.Enabled = false;
            button16.Enabled = false;
            button12.Enabled = false;
            button20.Enabled = false;
            button3.Enabled = true;
        }

        private void button16_Click(object sender, EventArgs e)
        { // Operator *
            Operator = '*';

            Operand1 = Input;
            Input = string.Empty;
            button8.Enabled = false;
            button16.Enabled = false;
            button12.Enabled = false;
            button20.Enabled = false;
            button3.Enabled = true;
        }

        private void button12_Click(object sender, EventArgs e)
        { // Operator -
            Operator = '-';

            Operand1 = Input;
            Input = string.Empty;
            button8.Enabled = false;
            button16.Enabled = false;
            button12.Enabled = false;
            button20.Enabled = false;
            button3.Enabled = true;
        }

        private void button20_Click(object sender, EventArgs e)
        { // Operator /
            Operator = '/';

            Operand1 = Input;
            Input = string.Empty;
            button8.Enabled = false;
            button16.Enabled = false;
            button12.Enabled = false;
            button20.Enabled = false;
            button3.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        { // Switch input sign (plus-minus)
            // TODO: switch sign of the number in input
        }

        private void button4_Click(object sender, EventArgs e)
        { // Operator =
            // TODO: Do multiple operations without clearing the result
            if (Input == string.Empty)
                return;
            if (Operand1 == string.Empty)
                return;

            Operand2 = Input;
            Input = string.Empty;

            double op1 = Convert.ToDouble(Operand1);
            double op2 = Convert.ToDouble(Operand2);

            switch (Operator)
            {
                case '+':
                    Result = op1 + op2;
                    break;
                case '-':
                    Result = op1 - op2;
                    break;
                case '*':
                    Result = op1 * op2;
                    break;
                case '/':
                    Result = op1 / op2;
                    break;
            }

            Input += Result;
            textBox1.ResetText();
            textBox1.AppendText(Input);
            textBox1.Update();

            button4.Enabled = false;
            button3.Enabled = false;
            button17.Enabled = false;
            button19.Enabled = false;
        }

        private void Form1_KeyPress(object sender, KeyPressEventArgs e)
        { // Keyboard button press handler
            // TODO: receive number key press and translate to button click
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        { // Update fontsize while Input.Length > 10
            int fontSize = 28;

            if (Input.Length > 10)
            {
                int iLength = Input.Length;
                int newSize = fontSize - ((Input.Length + 4) - 10);
                textBox1.Font = new Font(textBox1.Font.FontFamily, newSize);
                textBox1.Update();
            }
            else
            {
                textBox1.Font = new Font(textBox1.Font.FontFamily, fontSize);
                textBox1.Update();
            }
        }
    }
}

 

Calculadora csharp.7z

Link para o comentário
Compartilhar em outros sites

Visitante
Este tópico está impedido de receber novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...