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

 

 


Tema destacado: Rompecabezas de Bitcoin, Medio millón USD en premios


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  porcentaje c#
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: porcentaje c#  (Leído 2,506 veces)
toyeslomao

Desconectado Desconectado

Mensajes: 5


Ver Perfil
porcentaje c#
« en: 28 Abril 2020, 12:28 pm »

hola chicos, llevo no mucho tiempo liado con c#, aprendiendo de tutoriales e internet, es lo que tiene estar confinado, el tema es que estoy haciendo una calculadora, pero tengo un problema en el tema porcentaje %, al hacerlo me sale el calculo de ( x numero *x numero / 100) , por poner un ejemplo, el 12 % de 75 me sale 9, bien yo querria (si tener que hacer nuevo codigo  XD) que cuando haga esa operacion me saliese el resultado final, es decir (75 - 12 % = 66), pero no tengo manera, he tratado con ensayo y error, pero nada, al final mi mujer me echa de casa, porgo el codigo aqui si no os importa


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.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace WindowsFormsApp1
  12. {
  13.    public partial class Form1 : Form
  14.    {
  15.  
  16.        Double valor = 0;
  17.        String operacion = "";
  18.        bool operacion_press = false;
  19.        Double valor1 = 100;
  20.  
  21.        public Form1()
  22.        {
  23.            InitializeComponent();
  24.        }
  25.  
  26.        private void button_click(object sender, EventArgs e)
  27.        {
  28.           if ((resultado.Text == "0")||(operacion_press))
  29.                resultado.Clear();
  30.            operacion_press = false;
  31.            Button b = (Button) sender;
  32.            resultado.Text = resultado.Text + b.Text;
  33.        }
  34.        private void btn_ce_Click(object sender, EventArgs e)
  35.        {
  36.            resultado.Text = "0";
  37.            res_2.Text = "0";
  38.        }
  39.  
  40.        private void operador_click(object sender, EventArgs e)
  41.        {
  42.            Button b = (Button)sender;
  43.            operacion = b.Text;
  44.            valor = Double.Parse(resultado.Text);
  45.            operacion_press = true;
  46.            res_2.Text = valor + " " + operacion;
  47.  
  48.        }
  49.  
  50.        private void btn_igual(object sender, EventArgs e)
  51.        {
  52.            res_2.Text = "";
  53.            switch (operacion)
  54.            {
  55.                case "+":
  56.                    resultado.Text = (valor + Double.Parse(resultado.Text)).ToString();
  57.                    res_2.Text = "El Resultado es " + Double.Parse(resultado.Text).ToString();
  58.                    break;
  59.                case "-":
  60.                    resultado.Text = (valor - Double.Parse(resultado.Text)).ToString();
  61.                    res_2.Text = "El Resultado es " + Double.Parse(resultado.Text).ToString();
  62.                    break;
  63.                case "*":
  64.                    resultado.Text = (valor * Double.Parse(resultado.Text)).ToString();
  65.                    res_2.Text = "El Resultado es " + Double.Parse(resultado.Text).ToString();
  66.                    break;
  67.                case "/":
  68.                    resultado.Text = (valor / Double.Parse(resultado.Text)).ToString();
  69.                    res_2.Text = "El Resultado es " + Double.Parse(resultado.Text).ToString();
  70.                    break;
  71.                case "√":
  72.                    resultado.Text = Math.Sqrt(valor).ToString();
  73.                    res_2.Text = "El Resultado es " + Double.Parse(resultado.Text).ToString();
  74.                    break;
  75.  
  76.  
  77.  
  78.  
  79.            } // fin switch
  80.  
  81.  
  82.  
  83.  
  84.        }
  85.         private void btn4_Click(object sender, EventArgs e)
  86.        {
  87.            resultado.Clear();
  88.            valor = 0;
  89.            res_2.Text = "0";
  90.  
  91.        }
  92.  
  93.        private void button5_Click(object sender, EventArgs e)
  94.        {
  95.            if (resultado.Text.Length > 0)
  96.  
  97.         resultado.Text = resultado.Text.Remove(resultado.Text.Length - 1, 1);
  98.        }
  99.  
  100.        private void btnpor_click(object sender, EventArgs e)
  101.        {
  102.  
  103.  
  104.          [b] [color=red] Double a;
  105.  
  106.  
  107.            a = Convert.ToDouble(resultado.Text) / Convert.ToDouble(100);
  108.            resultado.Text = System.Convert.ToString(a);
  109.            res_2.Text = " Porcentaje " + System.Convert.ToString(a);[/color]
  110. [/b]
  111.        }
  112.    }
  113. }
  114.  
  115. res_2.Text = " Porcentaje " + System.Convert.ToString(a); ***** deciros que el res_2.Text , es porque los datos salen a dos pantallas, tipo  calculadora de windos
  116.  
  117.  


En línea

ThunderCls


Desconectado Desconectado

Mensajes: 455


Coder | Reverser | Gamer


Ver Perfil WWW
Re: porcentaje c#
« Respuesta #1 en: 30 Abril 2020, 17:16 pm »

No creo entenderte bien.
Quieres que el 12% de 75 en lugar de dar 9 que es el resultado esperado de 66?
"12% de 75" y "75 - 12% (de 75)" son dos operaciones distintas, evidentemente necesitas codigo nuevo o modificado...eso o no entendi para nada lo que pretendes hacer  :rolleyes:


En línea

-[ "…I can only show you the door. You're the one that has to walk through it." – Morpheus (The Matrix) ]-
http://reversec0de.wordpress.com
https://github.com/ThunderCls/
**Aincrad**


Desconectado Desconectado

Mensajes: 668



Ver Perfil WWW
Re: porcentaje c#
« Respuesta #2 en: 30 Abril 2020, 17:40 pm »

No creo entenderte bien.
...eso o no entendi para nada lo que pretendes hacer  :rolleyes:

ya somos dos. por me abstuve de responder.  :silbar:
En línea



xskyx

Desconectado Desconectado

Mensajes: 7


Ver Perfil
Re: porcentaje c#
« Respuesta #3 en: 2 Mayo 2020, 06:35 am »

Yo si creo que te entendi  ;D te hice una función espero aun te sirva.

Código
  1. public void Operar(string valores)
  2. {
  3.    string Operador = null;
  4.    string tmp = null;
  5.    double Valor_1 = default(Double);
  6.    double Valor_2 = default(Double);
  7.    string Resultado = null;
  8.    bool porcentaje;
  9.  
  10.    for (var i = 0; i <= valores.Length - 1; i++)
  11.    {
  12.        tmp = tmp + valores.Chars[i];
  13.  
  14.        if (valores.Chars[i] == "+" | valores.Chars[i] == "-" | valores.Chars[i] == "/" | valores.Chars[i] == "*" | valores.Chars[i] == "%")
  15.        {
  16.            if (Operador == null)
  17.                Operador = valores.Chars[i];
  18.            else if (Operador != null & Operador != "%" & valores.Chars[i] == "%")
  19.                porcentaje = true;
  20.            else
  21.            {
  22.                Interaction.MsgBox("Solamente se puede hacer una operación a la vez", MsgBoxStyle.Critical, "Error");
  23.                return null;
  24.            }
  25.  
  26.            if (Valor_1 == default(Double))
  27.            {
  28.                Valor_1 = Conversion.Val(tmp);
  29.                tmp = null;
  30.            }
  31.        }
  32.    }
  33.  
  34.    if (Valor_1 != default(Double))
  35.    {
  36.        Valor_2 = Conversion.Val(tmp);
  37.        tmp = null;
  38.    }
  39.  
  40.    if (Valor_1 != default(Double) & Valor_2 != default(Double) & Operador != null)
  41.    {
  42.        if (porcentaje == true)
  43.        {
  44.            tmp = Conversion.Val(Valor_1) / 100 * Conversion.Val(Valor_2);
  45.            Valor_2 = tmp;
  46.        }
  47.  
  48.        switch (Operador)
  49.        {
  50.            case "+":
  51.                {
  52.                    Resultado = Conversion.Val(Valor_1) + Conversion.Val(Valor_2);
  53.                    break;
  54.                }
  55.  
  56.            case "-":
  57.                {
  58.                    Resultado = Conversion.Val(Valor_1) - Conversion.Val(Valor_2);
  59.                    break;
  60.                }
  61.  
  62.            case "/":
  63.                {
  64.                    Resultado = Conversion.Val(Valor_1) / Conversion.Val(Valor_2);
  65.                    break;
  66.                }
  67.  
  68.            case "*":
  69.                {
  70.                    Resultado = Conversion.Val(Valor_1) * Conversion.Val(Valor_2);
  71.                    break;
  72.                }
  73.  
  74.            case "%":
  75.                {
  76.                    Resultado = Conversion.Val(Valor_1) / 100 * Conversion.Val(Valor_2);
  77.                    break;
  78.                }
  79.        }
  80.        return Resultado;
  81.    }
  82.    else
  83.    {
  84.        Interaction.MsgBox("Algo está mal en la operación", MsgBoxStyle.Critical, "Error");
  85.        return null;
  86.    }
  87. }
  88.  
« Última modificación: 2 Mayo 2020, 06:45 am por xskyx » En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Duda con el uso de porcentaje
Java
arts 2 1,722 Último mensaje 3 Enero 2014, 01:56 am
por arts
porcentaje de uso de cpu
Scripting
navivan 6 6,051 Último mensaje 12 Marzo 2014, 18:07 pm
por navivan
porcentaje en c
Programación C/C++
lucas59 2 6,170 Último mensaje 5 Octubre 2014, 13:36 pm
por eferion
Porcentaje y tiempo estimado Bateria
Hardware
AC1 1 2,808 Último mensaje 2 Noviembre 2021, 17:05 pm
por Saberuneko
Funcion que muestre aciertos y porcentaje
Programación C/C++
Dumy 2 1,484 Último mensaje 4 Noviembre 2023, 23:55 pm
por Dumy
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines