1. 程式人生 > >java 寫一個簡單的計算器

java 寫一個簡單的計算器

請點選好的,看完介紹,咱們就根據這個杭電oj上1237 這道題,改寫一個用GUI寫出來的 小計算器,類似於這樣:


然後 通過輸入 顯示結果,比如說:


可以看得出來,咱們得到的結果是正確的:

程式碼:

package Computer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Count extends JApplet implements ActionListener
{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField textField = new JTextField("請輸入");
	String operator = "";//操作
	String input = "";//輸入的 式子
	boolean flag =  true;
//	boolean flag1 = true;
//	boolean flag2 = true;
	public void init()//覆寫Applet裡邊的init方法
	{
		Container C = getContentPane();
		JButton b[] = new JButton[16];
		JPanel panel = new JPanel();
		C.add(textField, BorderLayout.NORTH);
		C.add(panel,BorderLayout.CENTER);
		panel.setLayout(new GridLayout(4, 4,5,5));
		String name[]={"7","8","9","+","4","5","6","-","1","2","3","*","0","C","=","/"};//設定 按鈕
		for(int i=0;i<16;i++)//新增按鈕
		{
			b[i] = new JButton(name[i]);
			b[i].setBackground(new Color(192,192,192));
			b[i].setForeground(Color.BLUE);//數字鍵 設定為 藍顏色
			if(i%4==3)
				b[i].setForeground(Color.RED);
			b[i].setFont(new Font("宋體",Font.PLAIN,16));//設定字型格式
			panel.add(b[i]);
			b[i].addActionListener(this);
		}
		b[13].setForeground(Color.RED);//非數字鍵,即運算鍵設定為紅顏色
		b[13].setForeground(Color.RED);
	}
	public void actionPerformed(ActionEvent e) 
	{
		int cnt = 0;
		String actionCommand = e.getActionCommand();
		if(actionCommand.equals("+")||actionCommand.equals("-")||actionCommand.equals("*") ||actionCommand.equals("/"))
			input +=" "+actionCommand+" ";//設定輸入,把輸入的樣式改成 需要的樣子
		else if(actionCommand.equals("C"))
			input = "";
		else if(actionCommand.equals("="))//當監聽到等號時,則處理 input
		{
			input+= "="+compute(input);
			textField.setText(input);
			input="";
			cnt = 1;
		}
		else
			input += actionCommand;//數字為了避免多位數的輸入 不需要加空格
		if(cnt==0)
		textField.setText(input);
	}
	private String compute(String input)//即1237 的 樣例
	{
		String str[];
		str = input.split(" ");
		Stack<Double> s = new Stack<Double>();
		double m = Double.parseDouble(str[0]);
		s.push(m);
		for(int i=1;i<str.length;i++)
		{
			if(i%2==1)  
            {  
                if(str[i].compareTo("+")==0)  
                {  
                    double help = Double.parseDouble(str[i+1]);  
                    s.push(help);  
                }  
                  
                if(str[i].compareTo("-")==0)  
                {  
                    double help = Double.parseDouble(str[i+1]);  
                    s.push(-help);  
                }  
                  
                if(str[i].compareTo("*")==0)  
                {  
                    double help = Double.parseDouble(str[i+1]);  
                    double ans = s.peek();//取出棧頂元素  
                    s.pop();//消棧  
                    ans*=help;  
                    s.push(ans);  
                }  
                  
                if(str[i].compareTo("/")==0)  
                {  
                    double help = Double.parseDouble(str[i+1]);  
                    double ans = s.peek();  
                    s.pop();  
                    ans/=help;  
                    s.push(ans);  
                }  
            }  
        }  
        double ans = 0d;  
        while(!s.isEmpty())  
        {  
            ans+=s.peek();  
            s.pop();  
        }  
        String result = String.valueOf(ans);
        return result;
	}
	public static void main(String args[])
	{
		JFrame frame = new JFrame("Count");
		Count applet = new Count();
		frame.getContentPane().add(applet, BorderLayout.CENTER);
		applet.init();//applet的init方法
		applet.start();//執行緒開始
		frame.setSize(350, 400);//設定視窗大小
		frame.setVisible(true);//設定視窗可見
	}

}