Ejemplo n.º 1
0
 private void btnIgual_Click(object sender, EventArgs e) // Método para resolver a expressão
 {
     try
     {
         Operacao operacao = null;                                  // objeto responsável por resolver a sequencia
         if (!string.IsNullOrEmpty(txtVisor.Text) && TemNumero() && // caso a expressão não esteja vazia, tenha números e o ultimo caracter é válido
             (txtVisor.Text[txtVisor.Text.Length - 1] == ')' || !Operacao.EhOperador(txtVisor.Text[txtVisor.Text.Length - 1].ToString())))
         {
             if (EstaBalanceada(txtVisor.Text))                             // Verifica se a expressão está balanceada
             {
                 operacao          = new Operacao(txtVisor.Text);           // instanciação do objeto
                 txtResultado.Text = operacao.CalcularExpressao() + "";     // método que retorna o resultado
                 lbInfixa.Text     = "Infixa: " + operacao.SequenciaInfixa; // mostramos as sequencias ao usuário
                 lbPosfixa.Text    = "Posfixa: " + operacao.SequenciaPosfixa;
             }
             else
             {
                 MessageBox.Show("O número de '(' deve ser igual ao número de ')'", "Expressão não balanceada", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
         }
     }
     catch (DivideByZeroException)   // Exceção de divisão por 0
     {
         MessageBox.Show("Impossível calcular", "Divisão por 0", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     catch (NotFiniteNumberException)    // Exceção de raiz negativa
     {
         MessageBox.Show("Impossível calcular", "Raiz negativa", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
 }
Ejemplo n.º 2
0
 private void btnRadiciacao_Click(object sender, EventArgs e)    // Método de quando a raiz quadrada é selecionada
 {
     if (string.IsNullOrEmpty(txtVisor.Text) || (Operacao.EhOperador(txtVisor.Text[txtVisor.Text.Length - 1].ToString()) && txtVisor.Text[txtVisor.Text.Length - 1] != ')'))
     {
         txtVisor.Text += ((Button)sender).Text + "(";
         jaTemVirgula   = false;
     }
 }
Ejemplo n.º 3
0
 private bool TemNumero()    //verifica se há número na expressão
 {
     foreach (char possivelNumero in txtVisor.Text)
     {
         if (!Operacao.EhOperador(possivelNumero + ""))
         {
             return(true);
         }
     }
     return(false);
 }
Ejemplo n.º 4
0
 private void btnFechaParenteses_Click(object sender, EventArgs e)   // Método de quando o ) é selecionado
 {
     if (!string.IsNullOrEmpty(txtVisor.Text) && txtVisor.Text[txtVisor.Text.Length - 1] != '.' && (txtVisor.Text[txtVisor.Text.Length - 1] == ')' || !Operacao.EhOperador(txtVisor.Text[txtVisor.Text.Length - 1] + "")))
     {
         txtVisor.Text += ((Button)sender).Text;
         jaTemVirgula   = false;
     }
 }
Ejemplo n.º 5
0
 private void btnAbreParenteses_Click(object sender, EventArgs e)    // Método de quando o ( é selecionado
 {
     if (string.IsNullOrEmpty(txtVisor.Text) || (txtVisor.Text[txtVisor.Text.Length - 1] != ')' && Operacao.EhOperador(txtVisor.Text[txtVisor.Text.Length - 1].ToString()) && txtVisor.Text[txtVisor.Text.Length - 1] != '.'))
     {
         txtVisor.Text += ((Button)sender).Text;
         jaTemVirgula   = false;
     }
 }
Ejemplo n.º 6
0
 private void btnMultiplicacaoDivisao_Click(object sender, EventArgs e) // Método de quando * ou / é selecionado
 {
     if (!string.IsNullOrEmpty(txtVisor.Text) && txtVisor.Text[txtVisor.Text.Length - 1] != '(' && (!Operacao.EhOperador(txtVisor.Text[txtVisor.Text.Length - 1].ToString()) || txtVisor.Text[txtVisor.Text.Length - 1] == ')') && txtVisor.Text[txtVisor.Text.Length - 1] != '.')
     {
         txtVisor.Text += ((Button)sender).Text;
         jaTemVirgula   = false;
     }
 }
Ejemplo n.º 7
0
 private void btnAdicaoSubtracao_Click(object sender, EventArgs e)   // Método de quando + ou - é selecionado
 {
     if (string.IsNullOrEmpty(txtVisor.Text) || (txtVisor.Text[txtVisor.Text.Length - 1] == 'V' || txtVisor.Text[txtVisor.Text.Length - 1] == '(' || !Operacao.EhOperador(txtVisor.Text[txtVisor.Text.Length - 1].ToString()) && txtVisor.Text[txtVisor.Text.Length - 1] != '.'))
     {
         txtVisor.Text += ((Button)sender).Text;
         jaTemVirgula   = false;
     }
 }
Ejemplo n.º 8
0
 private void btnPonto_Click(object sender, EventArgs e) // Método de quando o . é selecionado
 {
     if (txtVisor.Text != "" && !jaTemVirgula && txtVisor.Text[txtVisor.Text.Length - 1] != '.' && !Operacao.EhOperador(txtVisor.Text[txtVisor.Text.Length - 1] + ""))
     {
         txtVisor.Text += ".";
         jaTemVirgula   = true;
     }
 }