elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Guía actualizada para evitar que un ransomware ataque tu empresa


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  Uso de raiz cuadrada en C#
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Uso de raiz cuadrada en C#  (Leído 23,608 veces)
Riudo

Desconectado Desconectado

Mensajes: 146


Si quieres enmendar tu pasado ayuda a los demas...


Ver Perfil
Uso de raiz cuadrada en C#
« en: 28 Febrero 2011, 03:04 am »

Hol amigos, estoy haciendo un proyecto de la uni, debo hacer una calculadora sencilla en c#, debe restar, sumar, mult, div, sacar potencias y raiz cuadrada ya tengo casi todas las clases sin embargo tengo problemas con la raiz cuadrada, no se si estoy usando mal la clase pero no logro correrla me da error, pero no tengo idea de que debo corregir a ver si me ayudan, En la linea del evento  
Código:
butRaiz_Click
, arroja el error: La cadena de entrada no tiene el formato correcto. aca esta el codigo:


Código
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace Proyectogestor
  10. {
  11.    public partial class Form1 : Form
  12.    {
  13.  
  14.        //Variables globales...
  15.  
  16.        float num1;
  17.        string op;
  18.  
  19.  
  20.  
  21.        public Form1()
  22.        {
  23.            InitializeComponent();
  24.        }
  25.  
  26.  
  27.        //Boton igual...
  28.  
  29.        private void butIgual_Click(object sender, EventArgs e)
  30.        {
  31.            float num2;
  32.  
  33.  
  34.            try
  35.            {
  36.                num2 = float.Parse(textVisor.Text);
  37.  
  38.                switch (op)
  39.                {
  40.                    case "sumar":
  41.                        textVisor.Text = "" + suma(num1, num2);
  42.                        break;
  43.  
  44.                    case "restar":
  45.                        textVisor.Text = "" + resta(num1, num2);
  46.                        break;
  47.  
  48.                    case "multiplicar":
  49.                        textVisor.Text = "" + multi(num1, num2);
  50.                        break;
  51.  
  52.                    case "dividir":
  53.                        textVisor.Text = "" + divi(num1, num2);
  54.                        break;
  55.  
  56.                    case "potenciar":
  57.                        textVisor.Text = "" + Math.Pow(num1,num2);
  58.                        break;
  59.  
  60.                    case "raiz":
  61.                        textVisor.Text = "" + Math.Sqrt(num1);
  62.                         break;
  63.  
  64.  
  65.                }  
  66.            }
  67.            catch (Exception aviso)
  68.            {
  69.                MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
  70.                throw;
  71.            }
  72.  
  73.        }
  74.  
  75.  
  76.        //Metodos de las operaciones...
  77.  
  78.        public float suma(float num3, float num4)
  79.        {
  80.            try
  81.            {
  82.                return (num3 + num4);
  83.            }
  84.  
  85.            catch (Exception ex1)
  86.            {
  87.                MessageBox.Show(ex1.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
  88.                throw;
  89.            }
  90.  
  91.        }
  92.        public float resta(float num3, float num4)
  93.        {
  94.            try
  95.            {
  96.                return (num3 - num4);
  97.            }
  98.  
  99.            catch (Exception ex1)
  100.            {
  101.                MessageBox.Show(ex1.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
  102.                throw;
  103.            }
  104.  
  105.        }
  106.        public float multi(float num3, float num4)
  107.        {
  108.            try
  109.            {
  110.                return (num3 * num4);
  111.            }
  112.  
  113.            catch (Exception ex1)
  114.            {
  115.                MessageBox.Show(ex1.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
  116.                throw;
  117.            }
  118.  
  119.        }
  120.        public float divi(float num3, float num4)
  121.        {
  122.            try
  123.            {
  124.                return (num3 / num4);
  125.            }
  126.  
  127.            catch (Exception ex1)
  128.            {
  129.                MessageBox.Show(ex1.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
  130.                throw;
  131.            }
  132.        }
  133.  
  134.  
  135.        //Numeros...
  136.  
  137.        private void but0_Click(object sender, EventArgs e)
  138.        {
  139.            textVisor.Text = textVisor.Text + "0";
  140.        }
  141.  
  142.        private void butComa_Click(object sender, EventArgs e)
  143.        {
  144.            textVisor.Text = textVisor.Text + ",";
  145.            butComa.Enabled = false;
  146.        }
  147.  
  148.        private void but1_Click(object sender, EventArgs e)
  149.        {
  150.            textVisor.Text = textVisor.Text + "1";
  151.        }
  152.  
  153.        private void but2_Click(object sender, EventArgs e)
  154.        {
  155.            textVisor.Text = textVisor.Text + "2";
  156.        }
  157.  
  158.        private void but3_Click(object sender, EventArgs e)
  159.        {
  160.            textVisor.Text = textVisor.Text + "3";
  161.        }
  162.  
  163.        private void but4_Click(object sender, EventArgs e)
  164.        {
  165.            textVisor.Text = textVisor.Text + "4";
  166.        }
  167.  
  168.        private void but5_Click(object sender, EventArgs e)
  169.        {
  170.            textVisor.Text = textVisor.Text + "5";
  171.        }
  172.  
  173.        private void but6_Click(object sender, EventArgs e)
  174.        {
  175.            textVisor.Text = textVisor.Text + "6";
  176.        }
  177.  
  178.        private void but7_Click(object sender, EventArgs e)
  179.        {
  180.            textVisor.Text = textVisor.Text + "7";
  181.        }
  182.  
  183.        private void but8_Click(object sender, EventArgs e)
  184.        {
  185.            textVisor.Text = textVisor.Text + "8";
  186.        }
  187.  
  188.        private void but9_Click(object sender, EventArgs e)
  189.        {
  190.            textVisor.Text = textVisor.Text + "9";
  191.        }
  192.  
  193.  
  194.        //Botones adicionales...
  195.  
  196.        private void butSalir_Click(object sender, EventArgs e)
  197.        {
  198.            Close();
  199.        }
  200.  
  201.        private void butLimpiar_Click(object sender, EventArgs e)
  202.        {
  203.            textVisor.Text = "";
  204.        }
  205.  
  206.  
  207.        //Botones de operaciones...
  208.  
  209.        private void butMas_Click(object sender, EventArgs e)
  210.        {
  211.            butComa.Enabled = true;
  212.  
  213.            try
  214.            {
  215.                num1 = float.Parse(textVisor.Text);
  216.            }
  217.            catch (Exception aviso)
  218.            {
  219.                MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
  220.                throw;
  221.            }
  222.            textVisor.Text = "";
  223.            op = "sumar";
  224.        }
  225.  
  226.        private void butMenos_Click(object sender, EventArgs e)
  227.        {
  228.            butComa.Enabled = true;
  229.  
  230.            try
  231.            {
  232.                num1 = float.Parse(textVisor.Text);
  233.            }
  234.            catch (Exception aviso)
  235.            {
  236.                MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
  237.                throw;
  238.            }
  239.            textVisor.Text = "";
  240.            op = "restar";
  241.        }
  242.  
  243.        private void butPor_Click(object sender, EventArgs e)
  244.        {
  245.            butComa.Enabled = true;
  246.  
  247.            try
  248.            {
  249.                num1 = float.Parse(textVisor.Text);
  250.            }
  251.            catch (Exception aviso)
  252.            {
  253.                MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
  254.                throw;
  255.            }
  256.            textVisor.Text = "";
  257.            op = "multiplicar";
  258.        }
  259.  
  260.        private void butDivis_Click(object sender, EventArgs e)
  261.        {
  262.            butComa.Enabled = true;
  263.  
  264.            try
  265.            {
  266.                num1 = float.Parse(textVisor.Text);
  267.            }
  268.            catch (Exception aviso)
  269.            {
  270.                MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
  271.                throw;
  272.            }
  273.            textVisor.Text = "";
  274.            op = "dividir";
  275.        }
  276.  
  277.        private void butPot_Click(object sender, EventArgs e)
  278.        {
  279.            butComa.Enabled = true;
  280.  
  281.            try
  282.            {
  283.                num1 = float.Parse(textVisor.Text);
  284.            }
  285.            catch (Exception aviso)
  286.            {
  287.                MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
  288.                throw;
  289.            }
  290.            textVisor.Text = "";
  291.            op = "potenciar";
  292.        }
  293.  
  294.        private void butRaiz_Click(object sender, EventArgs e)
  295.        {
  296.            butComa.Enabled = true;
  297.  
  298.            try
  299.            {
  300.                num1 = float.Parse(textVisor.Text);
  301.            }
  302.            catch (Exception aviso)
  303.            {
  304.                MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
  305.                throw;  
  306.            }
  307.            textVisor.Text = "";
  308.            op = "raiz";
  309.        }
  310.  
  311.  
  312.        private void Form1_Load(object sender, EventArgs e)
  313.        {
  314.  
  315.        }
  316.  
  317.  
  318.    }
  319. }
  320.  


« Última modificación: 28 Febrero 2011, 20:15 pm por [D4N93R] » En línea

Sauruxum

Desconectado Desconectado

Mensajes: 117


Ver Perfil WWW
Re: Uso de raiz cuadrada en C#
« Respuesta #1 en: 28 Febrero 2011, 15:45 pm »

Ves la parte del codigo:

Código:
case "raiz":
                        textVisor.Text = "" + Math.Sqrt(num1);
                         break;

Estas concadenando un string con un float ( o double? )
Tienes que convertir ese Math.Sqrt(num1) a un String, esto creo que se hace llamando el metodo toString() asi:

Código:
Math.Sqrt(num1).toString()

Prueba con todo y avisas como te fue.

Saludos


En línea

Riudo

Desconectado Desconectado

Mensajes: 146


Si quieres enmendar tu pasado ayuda a los demas...


Ver Perfil
Re: Uso de raiz cuadrada en C#
« Respuesta #2 en: 28 Febrero 2011, 16:38 pm »

No me funciono del todo, sin embargo al final le quite la propiedad "textVisor.text" al evento clic que tiene que ver con ese boton. Sin embargo tengo otra duda, el ejercicio se ha complicado mas xq ahora resulta que debe llevar clases, polimorfismo y herencia.

¿Como puedo hacer esto?, la verdad no tengo mucha experiencia programando, sin embargo se me ocurre que puedo hacer la potenciacion y la raiz cuadrada usando clases, y de alli mismo puedo hacr herencia y polimorfismo. ¿Es asi?
En línea

Edu


Desconectado Desconectado

Mensajes: 1.082


Ex XXX-ZERO-XXX


Ver Perfil
Re: Uso de raiz cuadrada en C#
« Respuesta #3 en: 28 Febrero 2011, 16:40 pm »

Estas seguro q da error ahi donde dices? yo te digo por las dudas q probes hacer estos cambios:
Código
  1. textVisor.Text = "" + suma(num1, num2);
  2.  
Por esto:
Código
  1. textVisor.Text = suma(num1, num2).ToString();
  2.  

Ya q lo de "" + esta de mas porq se modifica todo el text asique no tienes q borrar nada, y luego tienes q poner un string por eso la conversion q de seguro se te olvido. Asi haces con todos, resta, muli, y en raiz lo mismo.
Despues cambiaria esto:
Código
  1. textVisor.Text = textVisor.Text + "8";
  2.  
Por esto:
Código
  1. textVisor.Text += "8";
  2.  
Y con eso queda muy bueno el codigo

Edit: me fui a comer y ya te habian contestado, primero trata de q te ande este codigo desp haces lo de herencia y polimorfismo
« Última modificación: 28 Febrero 2011, 16:42 pm por XXX-ZERO-XXX » En línea

Riudo

Desconectado Desconectado

Mensajes: 146


Si quieres enmendar tu pasado ayuda a los demas...


Ver Perfil
Re: Uso de raiz cuadrada en C#
« Respuesta #4 en: 28 Febrero 2011, 18:46 pm »

Ahora si me corrio  ;D nada mas me falta lo demas
En línea

Riudo

Desconectado Desconectado

Mensajes: 146


Si quieres enmendar tu pasado ayuda a los demas...


Ver Perfil
Re: Uso de raiz cuadrada en C#
« Respuesta #5 en: 28 Febrero 2011, 19:38 pm »

Como puedo hcer la parte de la herencia,clases y polimorfismo. Aunq de por si ya estoy usando una clase principal...
En línea

[D4N93R]
Wiki

Desconectado Desconectado

Mensajes: 1.646


My software never has bugs. Its just features!


Ver Perfil WWW
Re: Uso de raiz cuadrada en C#
« Respuesta #6 en: 28 Febrero 2011, 20:22 pm »

Hola,

Puedes tener una clase para cada operación, y que cada clase implemente una interfaz o que implementen una clase base. Ambas soluciones son correctas, pero todo depende del alcance que le quieras dar.

INFO:

MSDN:
  - Herencia: http://msdn.microsoft.com/es-es/library/ms173149(v=VS.80).aspx
       - Interfaces: http://msdn.microsoft.com/es-es/library/ms173156(v=VS.80).aspx
       - Polimorfismo: http://msdn.microsoft.com/es-es/library/ms173152(v=VS.80).aspx
       - Clases: http://msdn.microsoft.com/es-es/library/ms173150(v=VS.80).aspx
  - Modificadores de acceso: http://msdn.microsoft.com/es-es/library/ms173121(v=VS.80).aspx

EL GUILLE:
   - http://www.elguille.info/NET/cursoCSharpErik/Entrega1/Entrega1.htm
   - http://www.elguille.info/NET/cursoCSharpErik/Entrega13/Entrega13.htm

Un saludo!
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Raiz cuadrada en c « 1 2 »
Programación C/C++
JOSE23 11 26,657 Último mensaje 21 Febrero 2011, 18:06 pm
por JOSE23
RAIZ Cuadrada en 82C52
ASM
CATBro 1 4,649 Último mensaje 8 Junio 2011, 12:02 pm
por ShotgunLogic
fallo de mi visual basic 6.0¿?Raiz cuadrada « 1 2 »
Programación Visual Basic
Senior++ 10 9,809 Último mensaje 1 Diciembre 2011, 12:34 pm
por Senior++
[C] Raiz Cuadrada sin math.h
Programación C/C++
edr89 5 20,291 Último mensaje 20 Mayo 2013, 06:30 am
por edr89
[Ayuda novato] Raiz cuadrada en C sin sqrt « 1 2 »
Programación C/C++
bourne1191 18 42,502 Último mensaje 6 Diciembre 2013, 20:51 pm
por amchacon
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines