Código fuente de la calculadora JAVA
//APPLET
importar java.util.*
importar java.awt.*; >importar java.awt.event.* ;
importar javax.swing.* ;
importar java.applet.
la clase pública SZJSQ extiende JApplet implementa ActionListener
{
booleano i=true;
JButton privado num0= nuevo JButton("0");
JButton privado num1= nuevo JButton("1");
JButton privado num2=nuevo JButton("2");
JButton privado num3=nuevo JButton("3");
JButton privado num4=nuevo JButton("4");
JButton privado num5=nuevo JButton("5");
JButton privado num6=nuevo JButton("6 ");
JButton privado num7=nuevo JButton("7");
JButton privado num8=nuevo JButton("8");
JButton privado num9=nuevo JButton("9");
JButton privado zuok=nuevo JButton("(");
JButton privado youk=nuevo JButton(")"
p> p>JButton privado dian=nuevo JButton(".") )
JButton privado NULL=nuevo JButton("N");
JButton privado plu=nuevo JButton( "+");
JButton privado min=nuevo JButton("-");
JButton privado mul=nuevo JButton("x");
JButton privado div= nuevo JButton("/");
JButton privado equ=nuevo JButton("=");
JButton privado cle=nuevo JButton("C");// clear
JTextField privado space=new JTextField(30);
public void init()
{
JPanel text=nuevo JPanel () ;
text.setLayout(nuevo FlowLayout());
text.add(sp
as);
botones JPanel=nuevo JPanel();
botones.setLayout(nuevo GridLayout(5,4));
botones.add(num9 );
botones.
botones.
agregar(num2);
botones.add(num1);
botones.add(mul);
botones.add(num0);
botones.add(cle);
botones .add(equ);
botones.add(div);
botones.add( zuok);
botones.
botones.add(youk);
botones.add(dian);
botones.add (NULL);
(num9).addActionListener(este);
(num8).addActionListener(este);
(num7).addActionListener(este );
(num6).addActionListener(this);
(num5).addActionListener(this);
(num4).addActionListener(this);
(num3).addActionListener(this);
(num0).addActionListener(this);
(plu).addActionListener(this); p> p>
(min).addActionListener(this);
(mul).addActionListener( this);
(div).addActionListener(this); p>
(equ).addActionListener(this);
(cle).addActionListener(this);
(zuok) .addActionListener(this);
(youk).addActionListener(this);
(dian).addActionListener(this);
setLayout(new BorderLayout());
add( "Norte",texto);
add("Sur",botones);
space.setText("0");
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==num9)
{
if(i==true)
{
space.setText("9");
i=false;
}
else space.setText(space.getText()+'9');
}
if(e.getSource()= =num8 )
{
if (i==true)
{
space.setText("8");
i=false;
}
En caso contrario space.setText(space.getText()+'8');
}
si (e.get
Fuente()==num7)
{
if (i==true)
{
space.setText("7 ");
i=false;
}
else space.getText("8");
}
if (e.setText( space.getText()+'7');
}
if(e.getSource()==num6)
{
if(i==true)
{
space.setText("5");
i=false;
}
más space.setText(space.getText()+'5');
}
if(e.getSource()== num4)
{
if(i==true)
{
espacio. setText("4"); de lo contrario space.setText(space.getText()+'3');
}
if (e.setText("2");
i=false;
}
else space.setText(space.getText()+'2');
}
if(e.getSource()==num1)
{
if(i==true)
{
space.setText("1");
i=false;
}
else space.setText(space.getText()+ '1');
}
if(e.getSource()==num0)
{
if(i= =true)
{
space.setText("0");
i=false;
}
else space.getSource()==zuok)
{
if (i==true)
{
space.setText("(");
i=false;
}
else space.setText(space.getText()+'(') ;
} if(e.getSource()==youk)
{
if(i==false)
espacio .setText(space.getText()+')');
}
if(e.getSource()==dian)
{ p>
if(i==false)
space.setText(space.getText()+'.')
}
si (e.getSource()==plu)
{
space.setText(space.getText()+'+');
i=false ;
}
si (e.getText()+'-');
i=false;
} p>
si (por ejemplo
etSource()==mul)
{
space.setText(space.
i=false;
}
If (e.getSource()==div)
{
space.setText(space.setText("0");
i=true;
}
}
}
Calculadora pública doble (Cadena f)//cálculo científico
{
int i=0,j=0,k;
char c;
StringBuffer s=new StringBuffer();
s .append(f);
s.append('=');
Fórmula de cadena=s.toString();
char[] anArray;
anArray=new char[50];
Pila< Carácter> mystack=nueva Pila< Carácter>();
while( fórmula.charAt(i)! = '=')
{
c=formula.charAt(i);
cambiar(c)
{
case '(': mystack.push(nuevo personaje(c));
i++;
break;
caso ')':
while(mystack.peek().charValue()! = '(')
{
anArray[j++ ]=mystack.pop().charValue();
}
mystack.pop();
i++;
romper ;
caso '+':
caso '-':
while(!mystack.empty()&&mystack.peek().charValue()! ='(')
{
anArray[j++]=mystack.pop().charValue();
}
mystack.push(nuevo carácter(c));
i++;
break;
case '*':
case ' /':
while(!mystack.empty()&&(mystack.peek().charValue()=='*'||mystack.peek().charValue()==' /' ))
{
anArray[j++]=mystack.peek().charValue();
}
mystack.push (nuevo personaje(c));
i++;
break;
case' ': i++;
break; p>
predeterminado: while((c>='0' &&c&
lt;='9')||c=='.')
{
anArray[j++]=c;
i++;
c=formula.charAt(i);
}
anArray[j++]='#';
romper;
}
}
}
i++;
descanso;
caso '-':
a=mystack1.pop().doubleValue();
b=mystack1.pop().doubleValue();
d=b-a;
mystack1.push(new Double(d));
i++;
break;
case '*':
a=mystack1.doubleValue();
b=mystack1.pop().doubleValue();
d=b*a;
mystack1.push(new Double(d));
i++;
break;
case '/':
a =mystack1.pop().doubleValue();
b=mystack1.pop().doubleValue();
if(a!=0)
{
d=b/a;
mystack1.push(nuevo Doble(d));
i++;
}
else
{
System.out.println("¡Error!"
}
); break;
predeterminado:
d=0;count=0;
while((c>='0'&&c<='9'))
{
d=10*d+c-'0';
i++;
c=anArray[i];
}
if(c=='.')
{
i++;
c= anArray[i];
while((c>='0'&&c<='9'))
{
count++;
d=d+(c-'0')/Math.pow(10,count);
i++;
c=anArray[i];
}
}
if(c=='#')
mystack1.push(new Double(d));
i++;
descanso;
}