package ad;
import javax.swing.;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class c extends JFrame implements ActionListener
{
private String[] KEYS={"7","8","9","","4","5","6","-","1","2","3","+","0","÷","=",".","c"};
private JButton keys[]=new JButton[KEYS.length];
private JTextField resultText = new JTextField("0.0");
private String b="";
public c()
{
super("计算器");
this.setLayout(null);
resultText.setBounds(20, 5, 255, 40);
resultText.setHorizontalAlignment(JTextField.RIGHT);
resultText.setEditable(false);
this.add(resultText);
int x=20,y=55;
for (int i=0;i<KEYS.length;i++)
{
keys[i] = new JButton();
keys[i].setText(KEYS[i]);
keys[i].setBounds(x, y, 60, 40);
if(x<215)
{
x+=65;
}
else
{
x = 20;
y+=45;
}
this.add(keys[i]);
}
for (int i = 0; i <KEYS.length; i++)
{
keys[i].addActionListener(this);
}
this.setResizable(false);
this.setBounds(500, 200, 300, 400);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
String label = e.getActionCommand();
if (label=="c"||label=="=")
{
if(label=="=")
{
String s[]=yunsuan(this.b);
String result=Result(s);
this.b=result+"";
resultText.setText(this.b);
}
else
{
this.b="";
resultText.setText("0");
}
}
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186else { this.b=this.b+label; resultText.setText(this.b); } } private String[] yunsuan(String str) { String s=""; char a[]=new char[100]; String jieguo[]=new String[100]; int top=-1,j=0; for (int i=0;i<str.length();i++) { if ("0123456789.".indexOf(str.charAt(i))>=0) { s=""; for (;i<str.length()&&"0123456789.".indexOf(str.charAt(i))>=0;i++) { s=s+str.charAt(i); } i--; jieguo[j]=s; j++; } else if ("(".indexOf(str.charAt(i))>=0) { top++; a[top]=str.charAt(i); } else if (")".indexOf(str.charAt(i))>=0) { for (;;) { if (a[top]!='(') { jieguo[j]=a[top]+""; j++; top--; } else { top--; break; } } } else if ("*%÷".indexOf(str.charAt(i))>=0) { if (top==-1) { top++; a[top]=str.charAt(i); } else { if ("*%÷".indexOf(a[top])>=0) { jieguo[j]=a[top]+""; j++; a[top]=str.charAt(i); } else if ("(".indexOf(a[top])>=0) { top++; a[top]=str.charAt(i); } else if ("+-".indexOf(a[top])>=0) { top++; a[top]=str.charAt(i); } } } else if ("+-".indexOf(str.charAt(i))>=0) { if (top==-1) { top++; a[top]=str.charAt(i); } else { if ("%*÷".indexOf(a[top])>=0) { jieguo[j]=a[top]+""; j++; a[top]=str.charAt(i); } else if ("(".indexOf(a[top])>=0) { top++; a[top]=str.charAt(i); } else if ("+-".indexOf(a[top])>=0) { jieguo[j]=a[top]+""; j++; a[top]=str.charAt(i); } } } } for (;top!=-1;) { jieguo[j]=a[top]+""; j++; top--; } return jieguo; } public String yunsuan2(String str) { String result=""; double a=Double.parseDouble(str),b=0; b=Math.sqrt(a); result=String.valueOf(b); return result; } public String yunsuan3(String str) { String result=""; double a=Double.parseDouble(str),b=0; b=Math.pow(a, 2); result=String.valueOf(b); return result; } public String Result(String str[]) { String Result[]=new String[100]; int Top=-1; for (int i=0;str[i]!=null;i++) { if ("+-*÷".indexOf(str[i])<0) { Top++; Result[Top]=str[i]; } if ("+-*÷".indexOf(str[i])>=0) { double x,y,n; x=Double.parseDouble(Result[Top]); Top--; y=Double.parseDouble(Result[Top]); Top--; if ("-".indexOf(str[i])>=0) { n=y-x; Top++; Result[Top]=String.valueOf(n); } if ("+".indexOf(str[i])>=0) { n=y+x; Top++; Result[Top]=String.valueOf(n); } if ("*".indexOf(str[i])>=0) { n=y*x; Top++; Result[Top]=String.valueOf(n); } if ("÷".indexOf(str[i])>=0) { if (x==0) { String s="ERROR"; return s; } else { n=y/x; Top++; Result[Top]=String.valueOf(n); } } } } return Result[Top]; } public static void main(String arg[]) { c a=new c(); }
}
转载于:https://www.cnblogs.com/1786722420yang/p/11546939.html
最后
以上就是安静身影最近收集整理的关于计算机的全部内容,更多相关计算机内容请搜索靠谱客的其他文章。
发表评论 取消回复