elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.
 
Inicio Ayuda Buscar Ingresar Registrarse
28 Mayo 2012, 08:40  


Tema destacado: [AIO elhacker.NET] Compilación herramientas análisis y desinfección malware

+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (Moderador: [D4N93R])
| | | |-+  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 2,931 veces)
Riudo

Desconectado Desconectado

Mensajes: 144


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


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

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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace Proyectogestor
{
   public partial class Form1 : Form
   {
 
       //Variables globales...
 
       float num1;
       string op;
 
 
 
       public Form1()
       {
           InitializeComponent();
       }
 
 
       //Boton igual...
 
       private void butIgual_Click(object sender, EventArgs e)
       {
           float num2;
 
 
           try
           {
               num2 = float.Parse(textVisor.Text);
 
               switch (op)
               {
                   case "sumar":
                       textVisor.Text = "" + suma(num1, num2);
                       break;
 
                   case "restar":
                       textVisor.Text = "" + resta(num1, num2);
                       break;
 
                   case "multiplicar":
                       textVisor.Text = "" + multi(num1, num2);
                       break;
 
                   case "dividir":
                       textVisor.Text = "" + divi(num1, num2);
                       break;
 
                   case "potenciar":
                       textVisor.Text = "" + Math.Pow(num1,num2);
                       break;
 
                   case "raiz":
                       textVisor.Text = "" + Math.Sqrt(num1);
                        break;
 
 
               }  
           }
           catch (Exception aviso)
           {
               MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
               throw;
           }
 
       }
 
 
       //Metodos de las operaciones...
 
       public float suma(float num3, float num4)
       {
           try
           {
               return (num3 + num4);
           }
 
           catch (Exception ex1)
           {
               MessageBox.Show(ex1.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
               throw;
           }
 
       }
       public float resta(float num3, float num4)
       {
           try
           {
               return (num3 - num4);
           }
 
           catch (Exception ex1)
           {
               MessageBox.Show(ex1.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
               throw;
           }
 
       }
       public float multi(float num3, float num4)
       {
           try
           {
               return (num3 * num4);
           }
 
           catch (Exception ex1)
           {
               MessageBox.Show(ex1.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
               throw;
           }
 
       }
       public float divi(float num3, float num4)
       {
           try
           {
               return (num3 / num4);
           }
 
           catch (Exception ex1)
           {
               MessageBox.Show(ex1.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
               throw;
           }
       }
 
 
       //Numeros...
 
       private void but0_Click(object sender, EventArgs e)
       {
           textVisor.Text = textVisor.Text + "0";
       }
 
       private void butComa_Click(object sender, EventArgs e)
       {
           textVisor.Text = textVisor.Text + ",";
           butComa.Enabled = false;
       }
 
       private void but1_Click(object sender, EventArgs e)
       {
           textVisor.Text = textVisor.Text + "1";
       }
 
       private void but2_Click(object sender, EventArgs e)
       {
           textVisor.Text = textVisor.Text + "2";
       }
 
       private void but3_Click(object sender, EventArgs e)
       {
           textVisor.Text = textVisor.Text + "3";
       }
 
       private void but4_Click(object sender, EventArgs e)
       {
           textVisor.Text = textVisor.Text + "4";
       }
 
       private void but5_Click(object sender, EventArgs e)
       {
           textVisor.Text = textVisor.Text + "5";
       }
 
       private void but6_Click(object sender, EventArgs e)
       {
           textVisor.Text = textVisor.Text + "6";
       }
 
       private void but7_Click(object sender, EventArgs e)
       {
           textVisor.Text = textVisor.Text + "7";
       }
 
       private void but8_Click(object sender, EventArgs e)
       {
           textVisor.Text = textVisor.Text + "8";
       }
 
       private void but9_Click(object sender, EventArgs e)
       {
           textVisor.Text = textVisor.Text + "9";
       }
 
 
       //Botones adicionales...
 
       private void butSalir_Click(object sender, EventArgs e)
       {
           Close();
       }
 
       private void butLimpiar_Click(object sender, EventArgs e)
       {
           textVisor.Text = "";
       }
 
 
       //Botones de operaciones...
 
       private void butMas_Click(object sender, EventArgs e)
       {
           butComa.Enabled = true;
 
           try
           {
               num1 = float.Parse(textVisor.Text);
           }
           catch (Exception aviso)
           {
               MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
               throw;
           }
           textVisor.Text = "";
           op = "sumar";
       }
 
       private void butMenos_Click(object sender, EventArgs e)
       {
           butComa.Enabled = true;
 
           try
           {
               num1 = float.Parse(textVisor.Text);
           }
           catch (Exception aviso)
           {
               MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
               throw;
           }
           textVisor.Text = "";
           op = "restar";
       }
 
       private void butPor_Click(object sender, EventArgs e)
       {
           butComa.Enabled = true;
 
           try
           {
               num1 = float.Parse(textVisor.Text);
           }
           catch (Exception aviso)
           {
               MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
               throw;
           }
           textVisor.Text = "";
           op = "multiplicar";
       }
 
       private void butDivis_Click(object sender, EventArgs e)
       {
           butComa.Enabled = true;
 
           try
           {
               num1 = float.Parse(textVisor.Text);
           }
           catch (Exception aviso)
           {
               MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
               throw;
           }
           textVisor.Text = "";
           op = "dividir";
       }
 
       private void butPot_Click(object sender, EventArgs e)
       {
           butComa.Enabled = true;
 
           try
           {
               num1 = float.Parse(textVisor.Text);
           }
           catch (Exception aviso)
           {
               MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
               throw;
           }
           textVisor.Text = "";
           op = "potenciar";
       }
 
       private void butRaiz_Click(object sender, EventArgs e)
       {
           butComa.Enabled = true;
 
           try
           {
               num1 = float.Parse(textVisor.Text);
           }
           catch (Exception aviso)
           {
               MessageBox.Show(aviso.Message, "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Information);
               throw;  
           }
           textVisor.Text = "";
           op = "raiz";
       }
 
 
       private void Form1_Load(object sender, EventArgs e)
       {
 
       }
 
 
   }
}
 


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

Desconectado Desconectado

Mensajes: 116


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

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: 144


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 »

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 »

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

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
textVisor.Text = textVisor.Text + "8";
 
Por esto:
Código
textVisor.Text += "8";
 
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 por XXX-ZERO-XXX » En línea
Riudo

Desconectado Desconectado

Mensajes: 144


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 »

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

Desconectado Desconectado

Mensajes: 144


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 »

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]
Moderador
***
Desconectado Desconectado

Mensajes: 1.647


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 »

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
Calcular raiz cuadrada
Programación Visual Basic
zered 5 1,626 Último mensaje 4 Noviembre 2007, 19:13
por zered
raiz cuadrada
Programación C/C++
mapers 7 5,382 Último mensaje 7 Julio 2009, 21:48
por Eternal Idol
Raiz cuadrada en c
Programación C/C++
JOSE23 11 4,337 Último mensaje 21 Febrero 2011, 18:06
por JOSE23
RAIZ Cuadrada en 82C52
ASM
CATBro 1 1,927 Último mensaje 8 Junio 2011, 12:02
por ShotgunLogic
fallo de mi visual basic 6.0¿?Raiz cuadrada
Programación Visual Basic
SixToex (Agares) 10 1,114 Último mensaje 1 Diciembre 2011, 12:34
por SixToex (Agares)
Powered by SMF 1.1.16 | SMF © 2006-2008, Simple Machines