1. Este site usa cookies. Ao continuar a usar este site está a concordar com o nosso uso de cookies. Saber Mais.

[Resolvido] Visual C# - Calculadora Simples

Discussão em 'Programação' iniciada por Dkid, 6 de Fevereiro de 2008. (Respostas: 3; Visualizações: 20501)

  1. Dkid

    Dkid Power Member

    O problema foi resolvido...

    Deixo so o codigo desta calculadora para quem estiver interessado

    Código:
    [COLOR=#0600ff]using[/COLOR] [COLOR=#000000]System[/COLOR];
    [COLOR=#0600ff]using[/COLOR] [COLOR=#000000]System[/COLOR].[COLOR=#0000ff]Collections[/COLOR].[COLOR=#0000ff]Generic[/COLOR];
    [COLOR=#0600ff]using[/COLOR] [COLOR=#000000]System[/COLOR].[COLOR=#0000ff]ComponentModel[/COLOR];
    [COLOR=#0600ff]using[/COLOR] [COLOR=#000000]System[/COLOR].[COLOR=#0000ff]Data[/COLOR];
    [COLOR=#0600ff]using[/COLOR] [COLOR=#000000]System[/COLOR].[COLOR=#0000ff]Drawing[/COLOR];
    [COLOR=#0600ff]using[/COLOR] [COLOR=#000000]System[/COLOR].[COLOR=#0000ff]Text[/COLOR];
    [COLOR=#0600ff]using[/COLOR] [COLOR=#000000]System[/COLOR].[COLOR=#0000ff]Windows[/COLOR].[COLOR=#0000ff]Forms[/COLOR];
     
    [COLOR=#0600ff]namespace[/COLOR] Calculadora
    [COLOR=#000000]{[/COLOR]
        [COLOR=#0600ff]public[/COLOR] partial [COLOR=#ff0000]class[/COLOR] Form1 : Form
        [COLOR=#000000]{[/COLOR]
            [COLOR=#0600ff]public[/COLOR] Form1[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
                InitializeComponent[COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
            [COLOR=#000000]}[/COLOR]
     
            [COLOR=#0600ff]private[/COLOR] [COLOR=#0600ff]void[/COLOR] sairToolStripMenuItem_Click[COLOR=#000000]([/COLOR][COLOR=#ff0000]object[/COLOR] sender, EventArgs e[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
                Environment.[COLOR=#0000ff]Exit[/COLOR][COLOR=#000000]([/COLOR][COLOR=#ff0000]0[/COLOR][COLOR=#000000])[/COLOR]; [COLOR=#008080][I]// Sai da aplicação[/I][/COLOR]
            [COLOR=#000000]}[/COLOR]
            [COLOR=#008080][I]/* Para ser possivel calcular dois valores em caixas de texto é necessário converter os valores,
             * é presiso usar o método Parse, pois uma caixa de texto é um conjunto de caracteres e deve ser
             * convertido para int (inteiro) ou outro tipo, como double. Declarei double por ser mais
             * abrangente do que o int.
             */[/I][/COLOR]
            [COLOR=#0600ff]private[/COLOR] [COLOR=#0600ff]void[/COLOR] soma_Click[COLOR=#000000]([/COLOR][COLOR=#ff0000]object[/COLOR] sender, EventArgs e[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]   
                [COLOR=#ff0000]double[/COLOR] N1 = [COLOR=#ff0000]double[/COLOR].[COLOR=#0000ff]Parse[/COLOR][COLOR=#000000]([/COLOR]Tbox1.[COLOR=#0000ff]Text[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#ff0000]double[/COLOR] N2 = [COLOR=#ff0000]double[/COLOR].[COLOR=#0000ff]Parse[/COLOR][COLOR=#000000]([/COLOR]Tbox2.[COLOR=#0000ff]Text[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#ff0000]double[/COLOR] outcome; [COLOR=#008080][I]// necessario para mostrar o resultado[/I][/COLOR]
                outcome = N1 + N2;
                Rbox.[COLOR=#0000ff]Text[/COLOR] = outcome.[COLOR=#0000ff]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
                Xbox.[COLOR=#0000ff]Text[/COLOR] = Tbox1.[COLOR=#0000ff]Text[/COLOR] + [COLOR=#808080]" + "[/COLOR] + Tbox2.[COLOR=#0000ff]Text[/COLOR] + [COLOR=#808080]" = "[/COLOR] + outcome.[COLOR=#0000ff]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
            [COLOR=#000000]}[/COLOR]
     
            [COLOR=#0600ff]private[/COLOR] [COLOR=#0600ff]void[/COLOR] sub_Click[COLOR=#000000]([/COLOR][COLOR=#ff0000]object[/COLOR] sender, EventArgs e[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
                [COLOR=#ff0000]double[/COLOR] N1 = [COLOR=#ff0000]double[/COLOR].[COLOR=#0000ff]Parse[/COLOR][COLOR=#000000]([/COLOR]Tbox1.[COLOR=#0000ff]Text[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#ff0000]double[/COLOR] N2 = [COLOR=#ff0000]double[/COLOR].[COLOR=#0000ff]Parse[/COLOR][COLOR=#000000]([/COLOR]Tbox2.[COLOR=#0000ff]Text[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#ff0000]double[/COLOR] outcome;
                outcome = N1 - N2;
                Rbox.[COLOR=#0000ff]Text[/COLOR] = outcome.[COLOR=#0000ff]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
                Xbox.[COLOR=#0000ff]Text[/COLOR] = Tbox1.[COLOR=#0000ff]Text[/COLOR] + [COLOR=#808080]" - "[/COLOR] + Tbox2.[COLOR=#0000ff]Text[/COLOR] + [COLOR=#808080]" = "[/COLOR] + outcome.[COLOR=#0000ff]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
            [COLOR=#000000]}[/COLOR]
     
            [COLOR=#0600ff]private[/COLOR] [COLOR=#0600ff]void[/COLOR] multi_Click[COLOR=#000000]([/COLOR][COLOR=#ff0000]object[/COLOR] sender, EventArgs e[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
                [COLOR=#ff0000]double[/COLOR] N1 = [COLOR=#ff0000]double[/COLOR].[COLOR=#0000ff]Parse[/COLOR][COLOR=#000000]([/COLOR]Tbox1.[COLOR=#0000ff]Text[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#ff0000]double[/COLOR] N2 = [COLOR=#ff0000]double[/COLOR].[COLOR=#0000ff]Parse[/COLOR][COLOR=#000000]([/COLOR]Tbox2.[COLOR=#0000ff]Text[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#ff0000]double[/COLOR] outcome;
                outcome = N1 * N2;
                Rbox.[COLOR=#0000ff]Text[/COLOR] = outcome.[COLOR=#0000ff]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
                Xbox.[COLOR=#0000ff]Text[/COLOR] = Tbox1.[COLOR=#0000ff]Text[/COLOR] + [COLOR=#808080]" x "[/COLOR] + Tbox2.[COLOR=#0000ff]Text[/COLOR] + [COLOR=#808080]" = "[/COLOR] + outcome.[COLOR=#0000ff]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
            [COLOR=#000000]}[/COLOR]
     
            [COLOR=#0600ff]private[/COLOR] [COLOR=#0600ff]void[/COLOR] divisao_Click[COLOR=#000000]([/COLOR][COLOR=#ff0000]object[/COLOR] sender, EventArgs e[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
                [COLOR=#ff0000]double[/COLOR] N1 = [COLOR=#ff0000]double[/COLOR].[COLOR=#0000ff]Parse[/COLOR][COLOR=#000000]([/COLOR]Tbox1.[COLOR=#0000ff]Text[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#ff0000]double[/COLOR] N2 = [COLOR=#ff0000]double[/COLOR].[COLOR=#0000ff]Parse[/COLOR][COLOR=#000000]([/COLOR]Tbox2.[COLOR=#0000ff]Text[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#ff0000]double[/COLOR] outcome;
                outcome = N1 / N2;
                Rbox.[COLOR=#0000ff]Text[/COLOR] = outcome.[COLOR=#0000ff]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
                Xbox.[COLOR=#0000ff]Text[/COLOR] = Tbox1.[COLOR=#0000ff]Text[/COLOR] + [COLOR=#808080]" / "[/COLOR] + Tbox2.[COLOR=#0000ff]Text[/COLOR] + [COLOR=#808080]" = "[/COLOR] + outcome.[COLOR=#0000ff]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
            [COLOR=#000000]}[/COLOR]
     
            [COLOR=#0600ff]private[/COLOR] [COLOR=#0600ff]void[/COLOR] Resto_Click[COLOR=#000000]([/COLOR][COLOR=#ff0000]object[/COLOR] sender, EventArgs e[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
                [COLOR=#ff0000]double[/COLOR] N1 = [COLOR=#ff0000]double[/COLOR].[COLOR=#0000ff]Parse[/COLOR][COLOR=#000000]([/COLOR]Tbox1.[COLOR=#0000ff]Text[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#ff0000]double[/COLOR] N2 = [COLOR=#ff0000]double[/COLOR].[COLOR=#0000ff]Parse[/COLOR][COLOR=#000000]([/COLOR]Tbox2.[COLOR=#0000ff]Text[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#ff0000]double[/COLOR] outcome;
                outcome = N1 % N2;
                Rbox.[COLOR=#0000ff]Text[/COLOR] = outcome.[COLOR=#0000ff]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
                Xbox.[COLOR=#0000ff]Text[/COLOR] = Tbox1.[COLOR=#0000ff]Text[/COLOR] + [COLOR=#808080]" / "[/COLOR] + Tbox2.[COLOR=#0000ff]Text[/COLOR] + [COLOR=#808080]" = "[/COLOR] + outcome.[COLOR=#0000ff]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR] + [COLOR=#808080]" de resto"[/COLOR];
            [COLOR=#000000]}[/COLOR]
     
            [COLOR=#0600ff]private[/COLOR] [COLOR=#0600ff]void[/COLOR] button1_Click[COLOR=#000000]([/COLOR][COLOR=#ff0000]object[/COLOR] sender, EventArgs e[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
                Tbox1.[COLOR=#0000ff]Text[/COLOR] = [COLOR=#808080]""[/COLOR];
                Tbox2.[COLOR=#0000ff]Text[/COLOR] = [COLOR=#808080]""[/COLOR];
                Rbox.[COLOR=#0000ff]Text[/COLOR] = [COLOR=#808080]""[/COLOR];
                Xbox.[COLOR=#0000ff]Text[/COLOR] = [COLOR=#808080]""[/COLOR];
            [COLOR=#000000]}[/COLOR]
     
     
            [COLOR=#0600ff]private[/COLOR] [COLOR=#0600ff]void[/COLOR] Form1_Load[COLOR=#000000]([/COLOR][COLOR=#ff0000]object[/COLOR] sender, EventArgs e[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
                [COLOR=#0600ff]this[/COLOR].[COLOR=#0000ff]Xbox[/COLOR].[COLOR=#0600ff]ReadOnly[/COLOR] = [COLOR=#0600ff]true[/COLOR]; [COLOR=#008080][I]// nao permite que as textbox sejam alteradas[/I][/COLOR]
                [COLOR=#0600ff]this[/COLOR].[COLOR=#0000ff]Rbox[/COLOR].[COLOR=#0600ff]ReadOnly[/COLOR] = [COLOR=#0600ff]true[/COLOR];
            [COLOR=#000000]}[/COLOR]
     
            [COLOR=#0600ff]private[/COLOR] [COLOR=#0600ff]void[/COLOR] sobreToolStripMenuItem_Click[COLOR=#000000]([/COLOR][COLOR=#ff0000]object[/COLOR] sender, EventArgs e[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
                MessageBox.[COLOR=#0000ff]Show[/COLOR][COLOR=#000000]([/COLOR][COLOR=#808080]"Programado em Visual C# "[/COLOR][COLOR=#000000])[/COLOR];
            [COLOR=#000000]}[/COLOR]
     
            [COLOR=#0600ff]private[/COLOR] [COLOR=#0600ff]void[/COLOR] ficheiroToolStripMenuItem_Click[COLOR=#000000]([/COLOR][COLOR=#ff0000]object[/COLOR] sender, EventArgs e[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
     
            [COLOR=#000000]}[/COLOR]
     
            [COLOR=#0600ff]private[/COLOR] [COLOR=#0600ff]void[/COLOR] E2_Click[COLOR=#000000]([/COLOR][COLOR=#ff0000]object[/COLOR] sender, EventArgs e[COLOR=#000000])[/COLOR]
            [COLOR=#000000]{[/COLOR]
                [COLOR=#ff0000]double[/COLOR] N1 = [COLOR=#ff0000]double[/COLOR].[COLOR=#0000ff]Parse[/COLOR][COLOR=#000000]([/COLOR]Tbox1.[COLOR=#0000ff]Text[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#ff0000]double[/COLOR] outcome;
                outcome = N1* N1;
                Rbox.[COLOR=#0000ff]Text[/COLOR] = outcome.[COLOR=#0000ff]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
                Xbox.[COLOR=#0000ff]Text[/COLOR] = Tbox1.[COLOR=#0000ff]Text[/COLOR] + [COLOR=#808080]"  = "[/COLOR] + outcome.[COLOR=#0000ff]ToString[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR];
           [COLOR=Yellow] [COLOR=#000000]}[/COLOR]
     
        [COLOR=#000000]}[/COLOR]
    [COLOR=#000000]}[/COLOR][/COLOR]
     
    Última edição: 6 de Fevereiro de 2008
  2. Código:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace P03
    {
        public partial class FrmCalc : Form
        {
    
            public FrmCalc()
            {
                InitializeComponent();
            }
    
            public string y;
            public double z;
    
            private void FrmCalc_Load(object sender, EventArgs e)
            {
                MessageBox.Show("Bem Vindo à minha calculadora", "Calculadora!!!");
            }
    
            private void btn02_Click(object sender, EventArgs e)
            {
    
                string vl1 = tbx01.Text;
                int vl11 = int.Parse(vl1);
                label1.Text = vl1.ToString();
                tbx01.Clear();
            }
    
            private void btn03_Click(object sender, EventArgs e)
            {
                string vl3 = tbx01.Text;
                int vl33 = int.Parse(vl3);
                label1.Text = vl3.ToString();
                tbx01.Clear();
            }
    
            private void tbx01_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void btn04_Click(object sender, EventArgs e)
            {
                tbx01.Text = "1";
            }
    
            private void btn05_Click(object sender, EventArgs e)
            {
                tbx01.Text += "2";
            }
    
            private void btn06_Click(object sender, EventArgs e)
            {
                tbx01.Text += "3";
            }
    
            private void btn07_Click(object sender, EventArgs e)
            {
                tbx01.Text += "4";
            }
    
            private void btn08_Click(object sender, EventArgs e)
            {
                tbx01.Text += "5";
            }
    
            private void btn09_Click(object sender, EventArgs e)
            {
                tbx01.Text += "6";
            }
    
            private void btn10_Click(object sender, EventArgs e)
            {
                tbx01.Text += "7";
            }
    
            private void btn11_Click(object sender, EventArgs e)
            {
                tbx01.Text += "8";
            }
    
            private void btn12_Click(object sender, EventArgs e)
            {
                tbx01.Text += "9";
            }
    
            private void btn13_Click(object sender, EventArgs e)
            {
                tbx01.Text += "0";
            }
    
            private void btn14_Click(object sender, EventArgs e)
            {
                tbx01.Clear();
            }
    
            private void btn01_Click(object sender, EventArgs e)
            {
                string vl2 = tbx01.Text;
                int vl22 = int.Parse(vl2);
    
                tbx01.Clear();
                int vl1 = int.Parse(label1.Text);
                int res = vl1 + vl22;
                tbx01.Text = res.ToString();
             
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
    
            }
    
            private void btn15_Click(object sender, EventArgs e)
            {
                tbx01.Text = "";
            }
    
        }
    }
    eu fiz este código mas n consigo fazer a subtracção, alguém me pode ajudar?
    é urgente pfv...abraço!
     
    Última edição pelo moderador: 16 de Fevereiro de 2008
  3. Dkid

    Dkid Power Member

    Tenta comentar o código, pode ser que assim alguém te ajude.... mas nao foi boa ideia teres usado o meu post para resolver o teu problema, uma vez que eu ja meti no titulo "RESOLVIDO" poucas serão as pessoas que irão abrir o post.

    E já agora a Techzone não é o melhor lugar para tirares as tuas dúvidas, pois bem que te podes matar que poucas serão as respostas....

    www.portugal-a-programar.org

    Ai terás mais ajuda !

    Espero ter ajudado de alguma forma
     
  4. Boas! Qual é o teu problema na subtracçao?

    O teu código o esta optimizado, no entanto teria obrigação de funcionar com:

    string vl2 = tbx01.Text;
    int vl22 = int.Parse(vl2);

    tbx01.Clear();
    int vl1 = int.Parse(label1.Text);
    int res = vl1 - vl22;
    tbx01.Text = res.ToString();


    Diz qual é a tua duvida que fica mais facil ajudar :S
     

Partilhar esta Página