• Lunes 29 de Abril de 2024, 04:01

Autor Tema:  Como Hacer Una Calculadora?  (Leído 5435 veces)

anbema6

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
Como Hacer Una Calculadora?
« en: Miércoles 26 de Diciembre de 2007, 13:11 »
0
Hola tengo que hacer un trabajillo estas navidades, una calculadora cientifica cn el builder, en un entorno grafico.Alguien tiene el codigo de algo parecido para q me pueda orientar?
Un saludo y gracias

giropau

  • Nuevo Miembro
  • *
  • Mensajes: 23
    • Ver Perfil
Re: Como Hacer Una Calculadora?
« Respuesta #1 en: Lunes 31 de Diciembre de 2007, 00:37 »
0
Puedes usar la libreria math.h. Allí encontraras todas las funciones matemáticas. En el tema de código pues, aunque nunca he hecho una, es hacer tantos botones como para todas las opciones y que cada boton llame a una funcion de la libreria con el valor que haya en un TEdit, por ejemplo, y con el siguiente que se introduzca y con el boton de resultado mostralo. Así a priori es lo que se me ocurre.

oasfoose

  • Nuevo Miembro
  • *
  • Mensajes: 2
    • Ver Perfil
Re: Como Hacer Una Calculadora?
« Respuesta #2 en: Lunes 31 de Diciembre de 2007, 05:58 »
0
:comp:   Tengo algo de código de como hice una simple, solo que te tienes q' imaginar los botones y demas pero tienen nombres muy sugestivos:

//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop
#include<math.h>

#include "Unit1.h"

//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TForm1 *Form1;
int op;
float Dig1, Dig2;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnSumarClick(TObject *Sender)
{
 op = 1;
 Dig1 = StrToFloat(edtDisplay -> Text);
 edtDisplay -> Text = "";
 }
//---------------------------------------------------------------------------
void __fastcall TForm1::btnRestarClick(TObject *Sender)
{
  op = 2;
  Dig1 = StrToFloat(edtDisplay -> Text);
  edtDisplay -> Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnMultiplicarClick(TObject *Sender)
{
  op = 3;
  Dig1 = StrToFloat(edtDisplay -> Text);
  edtDisplay -> Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnDividirClick(TObject *Sender)
{
  op = 4;
  Dig1 = StrToFloat(edtDisplay -> Text);
  edtDisplay -> Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnRaizClick(TObject *Sender)
{
  op = 5;
  Dig1 = StrToFloat(edtDisplay -> Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnPotClick(TObject *Sender)
{
  op = 6;
  Dig1 = StrToFloat(edtDisplay -> Text);
  edtDisplay -> Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnPiClick(TObject *Sender)
{
 edtDisplay -> Text = "3,14169";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnLimpiarClick(TObject *Sender)
{
 edtDisplay -> Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn1Click(TObject *Sender)
{
 edtDisplay -> Text = (edtDisplay -> Text+1);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn2Click(TObject *Sender)
{
 edtDisplay -> Text = (edtDisplay -> Text+2);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn3Click(TObject *Sender)
{
 edtDisplay -> Text = (edtDisplay -> Text+3);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn4Click(TObject *Sender)
{
 edtDisplay -> Text = (edtDisplay -> Text+4);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn5Click(TObject *Sender)
{
  edtDisplay -> Text = (edtDisplay -> Text+5);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn6Click(TObject *Sender)
{
  edtDisplay -> Text = (edtDisplay -> Text+6);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn7Click(TObject *Sender)
{
  edtDisplay -> Text = (edtDisplay -> Text+7);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn8Click(TObject *Sender)
{
  edtDisplay -> Text = (edtDisplay -> Text+8);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn9Click(TObject *Sender)
{
  edtDisplay -> Text = (edtDisplay -> Text+9);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn0Click(TObject *Sender)
{
  edtDisplay -> Text = (edtDisplay -> Text+0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnComaClick(TObject *Sender)
{
  edtDisplay -> Text = (edtDisplay-> Text+",");   
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
MessageBeep(MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnSenClick(TObject *Sender)
{
  op = 7;
  Dig1 = StrToFloat(edtDisplay -> Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnCosClick(TObject *Sender)
{
  op = 8;
  Dig1 = StrToFloat(edtDisplay -> Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnTanClick(TObject *Sender)
{
  op = 9;
  Dig1 = StrToFloat(edtDisplay -> Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnLogNatClick(TObject *Sender)
{
  op = 10;
  Dig1 = StrToFloat(edtDisplay -> Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnLog10Click(TObject *Sender)
{
  op = 11;
  Dig1 = StrToFloat(edtDisplay -> Text);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btnIgualClick(TObject *Sender)
{
if (op == 1)
   {
      Dig2 = StrToFloat(edtDisplay -> Text);
      float Suma = Dig1 + Dig2;
      edtDisplay -> Text = FloatToStr(Suma);
    }
else if (op == 2)
   {
      Dig2 = StrToFloat(edtDisplay -> Text);
      float Resta = Dig1 - Dig2;
      edtDisplay -> Text = FloatToStr(Resta);
    }
else if (op == 3)
   {
      Dig2 = StrToFloat(edtDisplay -> Text);
      float Multiplica = Dig1 * Dig2;
      edtDisplay -> Text = FloatToStr(Multiplica);
    }
else if (op == 4)
   {
      Dig2 = StrToFloat(edtDisplay -> Text);
      float Divide = Dig1 / Dig2;
      edtDisplay -> Text = FloatToStr(Divide);
    }
else if (op == 5)
   {
      float Raiz = sqrt(Dig1);
      edtDisplay -> Text = FloatToStr(Raiz);
    }
else if (op == 6)
   {
      Dig2 = StrToFloat(edtDisplay -> Text);
      float Potencia = pow(Dig1, Dig2);
      edtDisplay -> Text = FloatToStr(Potencia);
    }
else if (op == 7)
   {
      float Sen = sin(Dig1);
      edtDisplay -> Text = FloatToStr(Sen);
    }
else if (op == 8)
   {
      float Cos = cos(Dig1);
      edtDisplay -> Text = FloatToStr(Cos);
    }
else if (op == 9)
   {
      float Tan = tan(Dig1);
      edtDisplay -> Text = FloatToStr(Tan);
    }
else if (op == 10)
   {
      float In = log(Dig1);
      edtDisplay -> Text = FloatToStr(In);
    }
else if (op == 11)
   {
      float Log10 = log10(Dig1);
      edtDisplay -> Text = FloatToStr(Log10);
    }   
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
edtDisplay -> Text = "Hecha por Olman Arias Solano. Versión 1.1";        
}
//---------------------------------------------------------------------------

 :unsure:  Espero te sirva... ahi me avisas!

sandyta

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
Re: Como Hacer Una Calculadora?
« Respuesta #3 en: Martes 22 de Enero de 2008, 19:31 »
0
hola!!!

a mi me ha servido de gran ayuda!!! :kicking:  :kicking:

me gustaria ponerle a lo mejor alguna cosilla mas pero con esto ya tengo una buena base.
Aunque tengo una dudilla.. cuando haces una suma, luego si quieres escribir otro numero se me escribe justo despues del que ya esta mostrando del resultado.
Quiero decir.. por ejemplo:
2+3 = 5
Cuando apreto por ejemplo al 4 justo despues de esta operacion, en pantalla muestra:
54

Y luego es una cosa que no se para que sirve:

void __fastcall TForm1::FormCreate(TObject *Sender)
{
MessageBeep(MB_OK);
}

Que es lo que hace?

Bueno de nuevo mil gracias, si sabes de poner alguna cosilla mas de la calculadora en plan: retroceder cuando te equivocas de numero o guardar en memoria un numero ya seria lo mejor.

Un saludo!!!