Hesap Makinesi

CorsaiR

Emektar
27 Ara 2005
1,228
18
Çekirdekten
java ile yazılmış basit bir örnek program
//Title: Calculator
//Version:
//Copyright: Copyright (c) 1999
//Author: ALI FIRTINA
//Company: FIRTINA
//Description: Your description


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

public class Calculator extends Applet
{
TextField display; // Display field
int x; // starting x coordination of panel
int y; // starting y coordination of panel
int width; // width of the one button
int height; // height of the one button
String number1; // number pressed with button
char op; // operation pressed with button
char lastop; // save last opration before =
int number2; // change number to integer
int controlsum; // control sum if integer or double
double sum; // sum of the operations
boolean button[]; // button press or not
boolean sec; // if = pressed more than one

// INIT METHOD
public **** init()
{
display= new TextField("0",11); // initilaze text field
display.setEditable(false); // display not editable
button= new boolean[16]; // memory al******** of array
x=105; // declare atarting x coordinate of panel
y=75; // declare starting y coordinate of panel
width=50; // declare width of the one button
height=50; // declare height of the one button
sum=0; // initilaze sum
setBackground(Color.white); //set background color
number1=""+0; // initilaze number1
number2=0; // initilaze number2
sec=false; // initilaze sec for =
for (int i=0;i<=15;i++) // set buttons first position
button=true;
lastop=' ';
op=' ';
add(display); // add text field
}

// PAINT METHOD
public **** paint(Graphics g)
{
int a; // for writing buttons name
int b; // for writing button name
Font f = new Font ("TimesRoman",Font.BOLD,width/2); // set font
g.setFont(f); // set font to graphic object
g.setColor(Color.lightGray);
// draw buttons
for (int i=0;i<=15;i++)
{
int c=0;
if (i>3) c=1;
if (i>7) c=2;
if (i>11) c=3;
g.fill3DRect(x+((i%4)*width),y+(c*height),width,height,button);
}

g.setColor(Color.black);
a=x+(2*(width/4));
b=y+(2*(height/3));
// draw button names
g.drawString("1",a,b);
g.drawString("2",a+width,b);
g.drawString("3",a+(2*width),b);
g.drawString("4",a,b+height);
g.drawString("5",a+width,b+height);
g.drawString("6",a+(2*width),b+height);
g.drawString("7",a,b+(2*height));
g.drawString("8",a+width,b+(2*height));
g.drawString("9",a+(2*width),b+(2*height));
g.drawString("/",a+(3*width),b);
g.drawString("*",a+(3*width),b+height);
g.drawString("-",a+(3*width),b+(2*height));
g.drawString("+",a+(3*width),b+(3*height));
g.drawString("0",a,b+(3*height));
g.drawString("=",a+width,b+(3*height));
g.drawString("C",a+(2*width),b+(3*height));

}
// MOUSE DOWN METHOD
public boolean mouseDown (Event evt,int xcor,int ycor)
{
for (int i=0;i<=15;i++) // initial position of buttons
button=true;

// if 1. button is pressed
if (xcor<(x+width) && xcor>x && ycor<(y+height) && ycor>y)
{
number1+=1;
number2= Integer.parseInt(number1);
display.setText(""+number2);
button[0]=false;
}
// if 2. button pressed
else if (xcor<(x+(2*width)) && xcor>(x+width) && ycor<(y+height) && ycor>y)
{
number1+=2;
number2= Integer.parseInt(number1);
display.setText(""+number2);
button[1]=false;
}
// if 3. button pressed
else if (xcor<(x+(3*width)) && xcor>(x+(2*width)) && ycor<(y+height) && ycor>y)
{
number1+=3;
number2= Integer.parseInt(number1);
display.setText(""+number2);
button[2]=false;
}
// if 4. button pressed
else if (xcor<(x+(4*width)) && xcor>x+(3*width) && ycor<(y+height) && ycor>y)
{
lastop=op;
op='/';
calc();
button[3]=false;
}
// if 5. button pressed
else if (xcor<(x+width) && xcor>x && ycor<(y+(2*height)) && ycor>(y+height))
{
number1+=4;
number2= Integer.parseInt(number1);
display.setText(""+number2);
button[4]=false;
}
// if 6. button pressed
else if (xcor<(x+(2*width)) && xcor>(x+width) && ycor<(y+(2*height)) && ycor>(y+height))
{
number1+=5;
number2= Integer.parseInt(number1);
display.setText(""+number2);
button[5]=false;
}
// if 7. button pressed
else if (xcor<(x+(3*width)) && xcor>(x+(2*width)) && ycor<(y+(2*height)) && ycor>(y+height))
{
number1+=6;
number2= Integer.parseInt(number1);
display.setText(""+number2);
button[6]=false;
}
// if 8. button pressed
else if (xcor<(x+(4*width)) && xcor>(x+(3*width)) && ycor<(y+(2*height)) && ycor>(y+height))
{
lastop=op;
op='*';
calc();
button[7]=false;
}
// if 9. button pressed
else if (xcor<(x+width) && xcor>x && ycor<(y+(3*height)) && ycor>(y+(2*height)))
{
number1+=7;
number2= Integer.parseInt(number1);
display.setText(""+number2);
button[8]=false;
}
// if 10. button pressed
else if (xcor<(x+(2*width)) && xcor>(x+width) && ycor<(y+(3*height)) && ycor>(y+(2*height)))
{
number1+=8;
number2= Integer.parseInt(number1);
display.setText(""+number2);
button[9]=false;
}
// if 11. button pressed
else if (xcor<(x+(3*width)) && xcor>(x+(2*width)) && ycor<(y+(3*height)) && ycor>(y+(2*height)))
{
number1+=9;
number2= Integer.parseInt(number1);
display.setText(""+number2);
button[10]=false;
}
// if 12. button pressed
else if (xcor<(x+(4*width)) && xcor>(x+(3*width)) && ycor<(y+(3*height)) && ycor>(y+(2*height)))
{
lastop=op;
op='-';
calc();
button[11]=false;
}
// if 13. button pressed
else if (xcor<(x+width) && xcor>x && ycor<(y+(4*height)) && ycor>(y+(3*height)))
{
number1+=0;
number2= Integer.parseInt(number1);
display.setText(""+number2);
button[12]=false;
}
// if 14. button pressed
else if (xcor<(x+(2*width)) && xcor>(x+width) && ycor<(y+(4*height)) && ycor>(y+(3*height)))
{
lastop=op;
op='=';
calc();
button[13]=false;
}
// if 15. button pressed
else if (xcor<(x+(3*width)) && xcor>(x+(2*width)) && ycor<(y+(4*height)) && ycor>(y+(3*height)))
{
op='C';
calc();
button[14]=false;
}
// if 16. button pressed
else if (xcor<(x+(4*width)) && xcor>(x+(3*width)) && ycor<(y+(4*height)) && ycor>(y+(3*height)))
{
lastop=op;
op='+';
calc();
button[15]=false;
}
repaint(); // draw buttons with new positions
return true;
}
// MOUSE UP METHOD
public boolean mouseUp (Event evt,int xco,int yco)
{
// set buttons new position
for (int i=0;i<=15;i++)
button=true;
repaint ();
return true;
}
// CALCULATION METHOD
public **** calc()
{
switch (op)
{
case '+': // if operation +
number2= Integer.parseInt(number1); //parse string to int
if (lastop!='=' && lastop !=' ')
{
op=lastop;
lastop='=';
calc();

op='+';
sec=false;
break;
}
//else
if (sum!=0) // if sum not set
sum+=number2;
else sum=number2;
number1=""+0; // clear number
controlsum=(int)sum; // if sum integer or float
if (controlsum==sum) display.setText(""+controlsum);
else display.setText(""+sum);
sec=false;
break;
case '-': // if operation -
number2= Integer.parseInt(number1); // parse string to int
if (lastop != '=' && lastop !=' ')
{
op=lastop;
lastop='=';
calc();

op='-';
sec=false;
break;
}
if (sum!=0) // if sum not set
sum-=number2;
else sum=number2;
number1=""+0; // clear number
controlsum=(int)sum; // if sum integer or float
if (controlsum==sum) display.setText(""+controlsum);
else display.setText(""+sum);
sec=false;
break;
case '/': // if operation /
number2= Integer.parseInt(number1); // parse string to int
if (lastop!='=' && lastop !=' ')
{
op=lastop;
lastop='=';
calc();

op='/';
sec=false;
break;
}
if (number2==0)
{
sec=false;
break;
}
if (sum!=0)
sum/=number2;
else sum=number2;
number1=""+0; // clear number
controlsum=(int)sum; // if sum integer or float
if (controlsum==sum) display.setText(""+controlsum);
else display.setText(""+sum);
sec=false;
break;
case '*': // if operation *
number2= Integer.parseInt(number1); // parse string to int
if (lastop!='=' && lastop !=' ')
{
op=lastop;
lastop='=';
calc();

op='*';
sec=false;
break;
}
if (number2==0)
{
sec=false;
break;
}
if (sum!=0)
sum*=number2;
else sum=number2;
number1=""+0;
controlsum=(int)sum; // if sum integer or float
if (controlsum==sum) display.setText(""+controlsum);
else display.setText(""+sum);
sec=false;
break;
case 'C': // if operation clear
sum=0;
number1=""+0;
lastop=' ';
op=' ';
controlsum=(int)sum; // if sum integer or float
if (controlsum==sum) display.setText(""+controlsum);
else display.setText(""+sum);
sec=false;
break;
case '=': // if operation is equal to
if(sec)
{
controlsum=(int)sum; // if sum integer or float
if (controlsum==sum) display.setText(""+controlsum);
else display.setText(""+sum);
number1=""+0;
}
else
{
op=lastop;
calc();
}
sec=true;
break;


}
}


}
 
Üst

Turkhackteam.org internet sitesi 5651 sayılı kanun’un 2. maddesinin 1. fıkrasının m) bendi ile aynı kanunun 5. maddesi kapsamında "Yer Sağlayıcı" konumundadır. İçerikler ön onay olmaksızın tamamen kullanıcılar tarafından oluşturulmaktadır. Turkhackteam.org; Yer sağlayıcı olarak, kullanıcılar tarafından oluşturulan içeriği ya da hukuka aykırı paylaşımı kontrol etmekle ya da araştırmakla yükümlü değildir. Türkhackteam saldırı timleri Türk sitelerine hiçbir zararlı faaliyette bulunmaz. Türkhackteam üyelerinin yaptığı bireysel hack faaliyetlerinden Türkhackteam sorumlu değildir. Sitelerinize Türkhackteam ismi kullanılarak hack faaliyetinde bulunulursa, site-sunucu erişim loglarından bu faaliyeti gerçekleştiren ip adresini tespit edip diğer kanıtlarla birlikte savcılığa suç duyurusunda bulununuz.