-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalc.java
More file actions
100 lines (100 loc) · 2.66 KB
/
Calc.java
File metadata and controls
100 lines (100 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Calc extends Applet implements ActionListener
{
String cmd[]={"+","-","*","/","=","C","x^2",".","sin","cos","tan","log","cot","sec","cosec"};
double pv=0;
String op="";
Button b[]=new Button[25];
TextField t1=new TextField(10);
public void init()
{
setLayout(new BorderLayout());
add(t1,"North");
t1.setText("0");
Panel p=new Panel();
p.setLayout(new GridLayout(5,5));
for(int i=0;i<25;i++)
{
if(i<10)
b[i]=new Button(String.valueOf(i));
else
if(i<20)
b[i]=new Button(cmd[i%10]);
else
b[i]=new Button(cmd[i%10+10]);
b[i].setFont(new Font("Arial",Font.BOLD,25));
p.add(b[i]);
add(p,"Center");
b[i].addActionListener(this);
}
}
public void actionPerformed(ActionEvent ae)
{
double res=0;
String cap=ae.getActionCommand();
double cv=Double.parseDouble(t1.getText());
if(cap.equals("C"))
{
t1.setText("0");
pv=0;
cv=0;
res=0;
op="";
}
else
if(cap.equals("="))
{
res=cv;
if(op=="+")
res=pv+cv;
else
if(op=="-")
res=pv-cv;
else
if(op=="*")
res=pv*cv;
else
if(op=="/")
res=pv/cv;
else
if(op=="x^2")
res=pv*pv;
else
if(op==".")
res=(pv+cv/10);
else
if(op=="sin")
res=Math.sin(pv);
else
if(op=="cos")
res=Math.cos(pv);
else
if(op=="tan")
res=Math.tan(pv);
else
if(op=="log")
res=Math.log(pv);
else
if(op=="cot")
res=1/(Math.tan(pv));
else
if(op=="sec")
res=1/(Math.cos(pv));
else
if(op=="cosec")
res=1/(Math.sin(pv));
t1.setText(String.valueOf(res));
}
else
if(cap.equals("+")||cap.equals("-")||cap.equals("*")||cap.equals("/")||cap.equals("x^2")||cap.equals("x^3")||cap.equals("sin")||cap.equals("cos")||cap.equals("tan")||cap.equals("cot")||cap.equals("sec")||cap.equals("cosec"))
{
pv=cv;op=cap;t1.setText("0");
}
else{
double v=cv*10+Double.parseDouble(cap);
t1.setText(String.valueOf(v));
}
}
}