一、需求分析:

日常生活中,我们经常会碰到一些计算问题,因计算量大而复杂,是人头痛,所以计算器就诞生了。计算器这一小小的程序机器实际上是从计算机中割裂出来的衍生品,但因其方便快捷的操作模式,已经被广泛应用于商业等日常生活中,极大的方便了人们对于数字的整合运算。

二、程序功法及说明:

1 /*实现基本数学运算、函数等功能:加、减、乘、除、阶乘、正弦、余弦和指数运算。
  2 界面将模拟Windows中的计算器程序。*/
  3 import javax.swing.*;
  4 import java.awt.*;
  5 import java.awt.event.*;
  6 import java.io.FileInputStream;
  7 import java.io.FileOutputStream;
  8 import java.io.ObjectInputStream;
  9 import java.io.ObjectOutputStream;
 10 import java.util.Hashtable;
 11 
 12 public class Calculator extends Frame implements ActionListener{
 13 
 14  Panel txtpanel,btnpanel,southpanel;
 15  JTextField txtinput;
 16  JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0,bdes,bf;
 17  JButton chengbtn,chubtn,jiabtn,jianbtn,clearbtn,equalbtn,sinbtn,cosbtn,jiechengbtn,sqrtbtn,zhishubtn,daoshubtn;
 18  double sum,result;
 19  Menu editM,searchM,helpM;
 20  MenuBar bar;
 21  String sign;
 22  boolean bool=true;
 23 
 24  public Calculator()
 25  {
 26    super("计算器");
 27    txtpanel=new Panel();
 28    btnpanel=new Panel();
 29    southpanel=new Panel();
 30    txtinput=new JTextField(40);
 31    txtinput.setEditable(false);
 32    txtinput.setText("");
 33    bar=new MenuBar();
 34    
 35    helpM=new Menu("帮助");
 36    helpM.add(new MenuItem("帮助主题"));
 37    helpM.addSeparator();
 38    helpM.add(new MenuItem("sin 正弦"));
 39    helpM.add(new MenuItem("cos 余弦"));
 40    helpM.add(new MenuItem("!  阶乘"));
 41    helpM.add(new MenuItem("C   清除"));
 42    helpM.add(new MenuItem("÷  除"));
 43    helpM.add(new MenuItem("*   乘"));  
 44    helpM.add(new MenuItem("+   加"));
 45    helpM.add(new MenuItem("-   减"));
 46    helpM.add(new MenuItem("+/- 正负变号"));
 47    helpM.add(new MenuItem("√  开方"));
 48    helpM.add(new MenuItem("1/x 倒数x^y"));
 49    helpM.add(new MenuItem("x^y 次方"));
 50    helpM.add(new MenuItem("1~9 数字键"));
 51    helpM.add(new MenuItem("=   等于"));
 52    bar.add(helpM);
 53    
 54    setMenuBar(bar);
 55    //关联各键与符号并设置键的符号字体大小及颜色
 56    b1=new JButton("1");
 57    b1.setFont (new Font("1",Font.BOLD,20));
 58    b1.setForeground(Color.black);
 59    
 60    b2=new JButton("2");
 61    b2.setFont (new Font("2",Font.BOLD,20));
 62    b2.setForeground(Color.black);
 63    
 64    b3=new JButton("3");
 65    b3.setFont (new Font("3",Font.BOLD,20));
 66    b3.setForeground(Color.black);
 67    
 68    b4=new JButton("4");
 69    b4.setFont (new Font("4",Font.BOLD,20));
 70    b4.setForeground(Color.black);
 71    
 72    b5=new JButton("5");
 73    b5.setFont (new Font("5",Font.BOLD,20));
 74    b5.setForeground(Color.black);
 75    
 76    b6=new JButton("6");
 77    b6.setFont (new Font("6",Font.BOLD,20));
 78    b6.setForeground(Color.black);
 79    
 80    b7=new JButton("7");
 81    b7.setFont (new Font("7",Font.BOLD,20));
 82    b7.setForeground(Color.black);
 83    
 84    b8=new JButton("8");
 85    b8.setFont (new Font("8",Font.BOLD,20));
 86    b8.setForeground(Color.black);
 87    
 88    b9=new JButton("9");
 89    b9.setFont (new Font("9",Font.BOLD,20));
 90    b9.setForeground(Color.black);
 91    
 92    b0=new JButton("0");
 93    b0.setFont (new Font("0",Font.BOLD,20));
 94    b0.setForeground(Color.black);
 95    
 96    bdes=new JButton(".");
 97    bdes.setFont (new Font(".",Font.BOLD,20));
 98    bdes.setForeground(Color.black);
 99    
100    bf=new JButton("+/-");
101    bf.setFont (new Font("+/-",Font.BOLD,20));
102    bf.setForeground(Color.red);
103    
104    chengbtn=new JButton("*");
105    chengbtn.setFont(new Font("*",Font.BOLD,20));
106    chengbtn.setForeground(Color.red);
107    
108    chubtn=new JButton("÷");
109    chubtn.setFont(new Font("÷",Font.BOLD,20));
110    chubtn.setForeground(Color.red);
111    
112    jiabtn=new JButton("+");
113    jiabtn.setFont(new Font("+",Font.BOLD,20));
114    jiabtn.setForeground(Color.red);
115    
116    jianbtn=new JButton("-");
117    jianbtn.setFont(new Font("-",Font.BOLD,20));
118    jianbtn.setForeground(Color.red);
119    
120    clearbtn=new JButton("C");
121    clearbtn.setFont(new Font("C",Font.BOLD,20));
122    clearbtn.setForeground(Color.red);
123    clearbtn.addActionListener(new ActionListener()//创建监听器
124    {
125        public void actionPerformed(ActionEvent e){
126          txtinput.setText("");
127          }
128        });
129    
130    sinbtn=new JButton("sin");
131    sinbtn.setFont(new Font("sin",Font.BOLD,15));
132    sinbtn.setForeground(Color.red);
133    
134    cosbtn=new JButton("cos");
135    cosbtn.setFont(new Font("cos",Font.BOLD,15));
136    cosbtn.setForeground(Color.red);
137    
138    sqrtbtn=new JButton("√");
139    sqrtbtn.setFont(new Font("√",Font.BOLD,15));
140    sqrtbtn.setForeground(Color.red);
141    
142    zhishubtn=new JButton("x^y");
143    zhishubtn.setFont(new Font("x^y",Font.BOLD,15));
144    zhishubtn.setForeground(Color.red);
145    
146    daoshubtn=new JButton("1/x");
147    daoshubtn.setFont(new Font("1/x",Font.BOLD,15));
148    daoshubtn.setForeground(Color.red);
149    
150    jiechengbtn=new JButton("!");
151    jiechengbtn.setFont(new Font("!",Font.BOLD,20));
152    jiechengbtn.setForeground(Color.red);
153 
154    equalbtn=new JButton("=");
155    equalbtn.setFont(new Font("=",Font.BOLD,20));
156    equalbtn.setForeground(Color.red);
157    //设置键位分布
158    setLayout(new BorderLayout());
159 
160    txtpanel.add(txtinput);
161    add("North",txtpanel);
162 
163    btnpanel.setLayout(new GridLayout(5,4,1,3));//五行四列分布
164    
165    btnpanel.add(sinbtn);
166    btnpanel.add(cosbtn);
167    btnpanel.add(jiechengbtn);
168    btnpanel.add(clearbtn);
169    btnpanel.add(b1);
170    btnpanel.add(b2);
171    btnpanel.add(b3);
172    btnpanel.add(chubtn);
173    btnpanel.add(b4);
174    btnpanel.add(b5);
175    btnpanel.add(b6);
176    btnpanel.add(chengbtn);
177    btnpanel.add(b7);
178    btnpanel.add(b8);
179    btnpanel.add(b9);
180    btnpanel.add(jianbtn);
181    btnpanel.add(b0);
182    btnpanel.add(bdes);
183    btnpanel.add(bf);
184    btnpanel.add(jiabtn);
185  
186    add("Center",btnpanel);
187    
188    southpanel.setLayout(new GridLayout(1,4,1,3));//一行四列分布
189    southpanel.add(sqrtbtn);
190    southpanel.add(zhishubtn);
191    southpanel.add(daoshubtn);
192    southpanel.add(equalbtn);
193    
194    add("South",southpanel);
195    //监听按键
196    b1.addActionListener(this);
197    b2.addActionListener(this);
198    b3.addActionListener(this);
199    b4.addActionListener(this);
200    b5.addActionListener(this);
201    b6.addActionListener(this);
202    b7.addActionListener(this);
203    b8.addActionListener(this);
204    b9.addActionListener(this);
205    b0.addActionListener(this);
206    bdes.addActionListener(this);
207    bf.addActionListener(this);
208    
209    sinbtn.addActionListener(this);
210    cosbtn.addActionListener(this);
211    jiechengbtn.addActionListener(this);
212    sqrtbtn.addActionListener(this);
213    zhishubtn.addActionListener(this);
214    daoshubtn.addActionListener(this);
215    chengbtn.addActionListener(this);
216    chubtn.addActionListener(this);
217    jiabtn.addActionListener(this);
218    jianbtn.addActionListener(this);
219    equalbtn.addActionListener(this);
220 
221    setSize(280,290);
222    setVisible(true);
223    setLocation(400,200);
224    addWindowListener(new WindowCloser());
225 
226  }
227  
228  class WindowCloser extends WindowAdapter{
229        public void windowClosing(WindowEvent e){
230            System.exit(0);
231        }
232  }
233 
234  public void actionPerformed(ActionEvent e){
235      
236    Object obj=e.getSource();
237    //设置相关计算方法
238    if(obj==b1)
239        txtinput.setText(txtinput.getText()+"1");
240    else if(obj==b2)
241        txtinput.setText(txtinput.getText()+"2");
242    else if(obj==b3)
243        txtinput.setText(txtinput.getText()+"3");
244    else if(obj==b4)
245        txtinput.setText(txtinput.getText()+"4");
246    else if(obj==b5)
247        txtinput.setText(txtinput.getText()+"5");
248    else if(obj==b6)
249        txtinput.setText(txtinput.getText()+"6");
250    else if(obj==b7)
251        txtinput.setText(txtinput.getText()+"7");
252    else if(obj==b8)
253        txtinput.setText(txtinput.getText()+"8");
254    else if(obj==b9)
255        txtinput.setText(txtinput.getText()+"9");
256    else if(obj==b0)
257        txtinput.setText(txtinput.getText()+"0");
258    else if(obj==bdes)
259        txtinput.setText(txtinput.getText()+".");
260    
261    else if(obj==bf){
262        sum=Double.valueOf(txtinput.getText()).doubleValue();
263        boolean bool=true;
264            if( bool){
265            result=sum-2*sum;
266            bool=false;
267            }
268            else{
269                result=sum+2*sum;}
270        txtinput.setText(String.valueOf(result));
271        }
272 
273    else if(obj==sinbtn){
274        sum=Double.valueOf(txtinput.getText()).doubleValue();
275        result=Math.sin(sum);
276        txtinput.setText(String.valueOf(result));
277    }
278    
279    else if(obj==cosbtn){
280        sum=Double.valueOf(txtinput.getText()).doubleValue();
281        result=Math.cos(sum);
282        txtinput.setText(String.valueOf(result));
283    }
284    
285    else if(obj==zhishubtn){
286        sum=Double.valueOf(txtinput.getText()).doubleValue();
287        sign="x^y";
288        txtinput.setText("");
289    }
290    
291    else if(obj==daoshubtn){
292        sum=Double.valueOf(txtinput.getText()).doubleValue();
293        result=1/sum;
294        txtinput.setText(String.valueOf(result));
295        //sign="1/x";
296        //txtinput.setText("");
297    }
298    
299    else if(obj==sqrtbtn){
300        sum=Double.valueOf(txtinput.getText()).doubleValue();
301        result=Math.sqrt(sum);
302        txtinput.setText(String.valueOf(result));
303        //sign="√";
304        //txtinput.setText("");
305    }
306    
307    else if(obj==jiechengbtn){
308        int n;
309        n=(int)Double.valueOf(txtinput.getText()).doubleValue();
310        result=fac(n);
311            txtinput.setText(String.valueOf(result));
312    }
313 
314    else if(obj==jiabtn)
315    {
316        sum=Double.valueOf(txtinput.getText()).doubleValue();
317        sign="+";
318        txtinput.setText("");
319    }
320    
321    else if(obj==jianbtn)
322    {
323        sum=Double.valueOf(txtinput.getText()).doubleValue();
324        sign="-";
325        txtinput.setText("");
326    }
327    else if(obj==chubtn)
328    {
329        sum=Double.valueOf(txtinput.getText()).doubleValue();
330        sign="÷";
331        txtinput.setText("");
332    }
333    
334    else if(obj==chengbtn)
335    {
336        sum=Double.valueOf(txtinput.getText()).doubleValue();
337        sign="*";
338        txtinput.setText("");
339    }
340         
341    else if(sign=="+")
342      {
343      result=sum+Double.valueOf(txtinput.getText()).doubleValue();
344      txtinput.setText(String.valueOf(result));
345      }
346      
347    else if(sign=="-")
348        {
349        result=sum-Double.valueOf(txtinput.getText()).doubleValue();
350        txtinput.setText(String.valueOf(result));
351        }
352      
353    else if(sign=="÷")
354        {
355        double n;
356        String m="除零错误!";
357        n=Double.valueOf(txtinput.getText()).doubleValue();
358        if(n==0){
359           JOptionPane.showMessageDialog(this, m, "error", JOptionPane.ERROR_MESSAGE);
360        }
361        else
362            result=sum/n;
363        txtinput.setText(String.valueOf(result));
364        }
365      
366    else if(sign=="*")
367        {
368        result=sum*Double.valueOf(txtinput.getText()).doubleValue();
369        txtinput.setText(String.valueOf(result));
370        }
371     
372    else if(sign=="x^y"){
373        result=Math.pow(sum,Double.valueOf(txtinput.getText()).doubleValue());
374        txtinput.setText(String.valueOf(result));
375    }
376      
377    else if(sign=="sin")
378    {
379          result=Math.sin(sum);
380        txtinput.setText(String.valueOf(result)); 
381    }
382      
383    else if(sign=="cos")
384    {
385        result=Math.cos(sum);
386        txtinput.setText(String.valueOf(result)); 
387    }
388      
389    else if(sign=="!")
390    {
391        int i=(int)Double.valueOf(txtinput.getText()).doubleValue();
392        for(int j=i;j>0;i--){
393            result=j*(j-1);
394        }
395        txtinput.setText(String.valueOf(result));
396    }
397      
398    else if(obj=="√"){;
399        result=Math.sqrt(sum);
400        txtinput.setText(String.valueOf(result));
401    }
402      
403    else if(obj=="1/x"){
404        result=1/sum;
405        txtinput.setText(String.valueOf(result));
406    }
407 }
408  
409   private long fac(int n){
410       long f;
411       if(n==0)
412           f=1;
413       else f=fac(n-1)*n;
414       return f;
415   }
416  public static void main(String args[]){
417      try{   
418         UIManager.setLookAndFeel ("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
419      } catch(Exception e) {}  
420      new Calculator();//使用windows显示风格
421  }
422 
423 }

三.运行结果:

实现加、减、乘、除、阶乘、正弦、余弦和指数运算:

java模拟进程并发执行 java程序模拟_Math

功能说明:

java模拟进程并发执行 java程序模拟_java模拟进程并发执行_02

四、总结和体会:

不足:1.各种计算功能有待增加,可参考电脑自带计算机

         2.显示界面皮肤单一,可增加多种,以便个人喜好

         3.未设置计算值的复制和黏贴功能,单位换算功能等等

收获:对java的应用,有了一定了解和延伸,对于未来设计相关功能程序有较大帮助。