Root and power in C#

Asked

Viewed 4,081 times

1

I need to calculate the root and power according to the logic that follows.. so far ta funfando

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace calculadora
{
    public partial class Form1 : Form
    {
        Double value = 0;
        String operacao = "";
        bool operacao_press = false;

        public Form1()
        {
            InitializeComponent();
        }



        private void button_Click(object sender, EventArgs e)
        {
            if ((resultado.Text == "0") || (operacao_press))
                resultado.Clear();

            Button b = (Button)sender;
            resultado.Text = resultado.Text + b.Text;
        }

        private void button16_Click(object sender, EventArgs e)
        {
            resultado.Text = "0";
        }

        private void button20_Click(object sender, EventArgs e)
        {
            resultado.Text = "0";
        }

        private void operador_click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            operacao = b.Text;
            value = Double.Parse(resultado.Text);
            operacao_press = true;
        }

        private void button18_Click(object sender, EventArgs e)
        {                              
            switch (operacao) //OPERAÇÕES MATEMÁTICAS   
            {         

                case "+":
                    resultado.Text = (value + Double.Parse(resultado.Text)).ToString();
                    break;

                case "- ":
                    resultado.Text = (value - Double.Parse(resultado.Text)).ToString();
                    break;

                case "/":
                    resultado.Text = (value / Double.Parse(resultado.Text)).ToString();
                    break;

                case "*":
                    resultado.Text = (value * Double.Parse(resultado.Text)).ToString();
                    break;


                case "RAIZ":
                    resultado.Text = (value  Double.Parse(resultado.Text)).ToString();
                    break;


                case "POTENCIALIZAÇÃO":
                    resultado.Text = (value  Double.Parse(resultado.Text)).ToString();
                    break;

                default:
                    break;

            } //Final do Switch
            operacao_press = false;
        }
    }
}

1 answer

2


            case "RAIZ":
                resultado.Text = Math.Sqrt(value).ToString(); // num
                break;


            case "POTENCIALIZAÇÃO":
                resultado.Text = Math.Pow(value, Double.Parse(resultado.Text)).ToString(); // num, expoente
                break;

To calculate the root of another exponent you can define the following function:

public static void cubicPairs(double n, int expoente)
{
    double root = (System.Math.Pow(n, (1 / expoente)));
    double roundedRoot = Math.Round(root);

    if (Math.Abs(roundedRoot - root) < VERY_SMALL_NUMBER)
        return roundedRoot;
    else
        return root;
}

And call her normally in your code:

            case "RAIZ":
                resultado.Text = cubicPairs(value, Double.Parse(resultado.Text)).ToString();
                break;
  • No need to convert to string back no?

  • @Jeffersonquesado edited the answer.

  • this root of the other exponent fits inside the switch ?

  • In the case of roots of other exponents, it would not be the case to do 1.0/expoente? I believe that C# uses integer division (unlike Python 3 which always goes to rational division, whole division being another operator)

  • 1

    @jaogui_ I organized a little the answer of Cypherpotato, it should help in that doubt

  • 1

    @Cypherpotato, I beg your permission to give a little tidying up, if you have changed the meaning of the answer, I ask for a tug of an ear and forgiveness

  • thanks, already helped out !

  • Quiet @Jeffersonquesado, editions are welcome.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.