import java.awt.datatransfer.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
class Oper // 對壹個含+ - * /字符串求值
{
public String operator(String s) {
boolean flag = true;
boolean flag2 = false;
int i, n = 0;
String chr = new String(), theta = "";
double number1 = 0, number2 = 0;
Stack oprstack = new Stack();
oprstack.push("=");
Stack opnstack = new Stack();
for (i = 0; i < s.length(); i++) {
chr = String.valueOf(s.charAt(i));
if (!chr.equals("+") && !chr.equals("-") && !chr.equals("*")
&& !chr.equals("/") && !chr.equals("(") && !chr.equals(")")
&& !chr.equals("s") && !chr.equals("=")) {
flag2 = true;
continue;
} else {
if (flag2 == true && chr.equals("s"))
return "您輸入有誤!";// 提示“數字+sqrt”式輸入錯誤
flag2 = false;
if (n <= i - 1) // 解決+(問題
{
opnstack.push(s.substring(n, i));
}
n = i + 1;
flag = true;
while (flag) {
switch (precede(String.valueOf(oprstack.peek()), chr))// (String)oprstack.peek()
{
case 3:
oprstack.push(chr); // opr1<opr2
flag = false;
break;
case 2:
oprstack.pop(); // opr1==opr2()
flag = false;
break;
case 1:
theta = String.valueOf(oprstack.pop()); // opr1>opr2
if (theta.equals("s")) // 解決sqrt()問題
{
number1 = Double.parseDouble((String) (opnstack
.pop()));
opnstack.push(String.valueOf(Math.sqrt(number1)));
} else // 解決壹般混合運算
{
number2 = Double.parseDouble((String) (opnstack
.pop()));
number1 = Double.parseDouble((String) (opnstack
.pop()));
opnstack.push(String.valueOf(opr(number1, number2,
theta)));
}
flag = true;
break;
case 0:
return "您輸入有誤!";
}// end switch
}// endwhile
}// endelse
}// endfor
return (String.valueOf(opnstack.pop()));
}// end operator()
public double opr(double x, double y, String theta) // 返回x與y運算值
{
if (theta.equals("+"))
return x + y;
if (theta.equals("-"))
return x - y;
if (theta.equals("*"))
return x * y;
if (theta.equals("/"))
return x / y;
return 0.;
}
public int precede(String chr, String c) // 判斷運算符優先級
{
// 1 代表">" 2代表"=" 3代表"<"
if (chr.equals("+") || chr.equals("-")) {
if (c.equals("*") || c.equals("/") || c.equals("(")
|| c.equals("s"))
return 3;
else
return 1;
}
if (chr.equals("*") || chr.equals("/") || chr.equals("s")) {
if (c.equals("("))
return 3;
else
return 1;
}
if (chr.equals("(")) {
if (c.equals(")"))
return 2;
else
return 3;
}
if (chr.equals(")")) {
return 1;
}
if (chr.equals("=")) {
if (c.equals("="))
return 2;
else if (c.equals(")"))
return 0;// 提示括號不匹配式輸入錯誤
else
return 3;
}
return 0;
}// end precede
}// end class oper
public class Calculator extends Frame implements ActionListener {
private String s_show;
private String s_go;
Panel p1 = new Panel();
Panel p2 = new Panel();
Panel p3=new Panel();
Oper do1 = new Oper();
TextField textfields = new TextField(27);
Dialog dlog=new Dialog(this,"about us");
Label aboutlab=new Label();
String s1;
String sedit;
Button buttons1 = new Button("Backspace");
int screenWidth,screenHeight;
Clipboard clp=Toolkit.getDefaultToolkit().getSystemClipboard();
Calculator() {
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
MenuBar mbr=new MenuBar();
setMenuBar(mbr);
Menu edit=new Menu(" do ");
MenuItem copy=new MenuItem("Copy Ctrl+C");
MenuItem zhantie=new MenuItem(" Ctrl+V");
MenuItem exit=new MenuItem("Close Alt+F4");
edit.add(copy);
edit.add(zhantie);
mbr.add(edit);
Menu help=new Menu(" help ");
MenuItem help1=new MenuItem("help theme");
MenuItem about=new MenuItem("about us");
dlog.setAlwaysOnTop(true);
dlog.setResizable(false);
//dlog.setEnabled(false);
s1="學習時興趣小組做的";
aboutlab.setSize(200, 150);
aboutlab.setText(s1);
dlog.add(aboutlab);
about.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dlog.setVisible(true);
}
});
dlog.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dlog.setVisible(false);
}
});
help.add(help1);
help.add(about);
mbr.add(help);
p1.setBackground(Color.lightGray);
p1.setForeground(Color.red);
p2.setBackground(Color.lightGray);
p2.setForeground(Color.blue);
p1.setLayout(new FlowLayout(FlowLayout.RIGHT, 1,1));
p1.add(textfields);
p1.add(buttons1);
buttons1.addActionListener(this);
p2.setLayout(new GridLayout(5, 4, 10, 16));
this.add(p1, BorderLayout.CENTER);
this.add(p2, BorderLayout.SOUTH);
this.setTitle("Play the window");
this.setResizable(false);
this.setAlwaysOnTop(true);
/**textfields.addKeyListener(new KeyAdapter(){
public void keyTyped(KeyEvent e){
char c=e.getKeyChar();
String textfieldsstr=textfields.toString();
switch(c){
case 1:textfields.setText(textfieldsstr+1);break;
case 2:textfields.setText(textfields.toString()+2);break;
case 3:textfields.setText(textfields.toString()+3);break;
case 4:textfields.setText(textfields.toString()+4);break;
case 5:textfields.setText(textfields.toString()+5);break;
case 6:textfields.setText(textfields.toString()+6);break;
case 7:textfields.setText(textfields.toString()+7);break;
case 8:textfields.setText(textfields.toString()+8);break;
case 9:textfields.setText(textfields.toString()+9);break;
case 0:textfields.setText(textfields.toString()+0);break;
case '*':textfields.setText(textfields.toString()+"*");break;
}
/**if(e.getKeyChar()==1){
textfields.setText(textfields.toString()+1);
}
}
});*///未實現鍵盤監聽
Toolkit tk=Toolkit.getDefaultToolkit();
Dimension screenSize = tk.getScreenSize();
screenWidth=screenSize.width;
screenHeight=screenSize.height;
this.setBounds(screenWidth/2-150, screenHeight/2-128, 300, 256);
dlog.setBounds(screenWidth/2-100, screenHeight/2-78, 200, 150);
int i, j = 0, k = 9, r1 = 0, r2 = 0, r3 = 0;
for (i = 0; i < 20; i++) {
j++;
if (j == 4) {
j = 0;
r1++;
Button buttons2_i = new Button();
p2.add(buttons2_i);
buttons2_i.setBackground(Color.cyan);
buttons2_i.addActionListener(this);
switch (r1) {
case 1:
buttons2_i.setLabel("+");
break;
case 2:
buttons2_i.setLabel("-");
break;
case 3:
buttons2_i.setLabel("*");
break;
case 4:
buttons2_i.setLabel("/");
break;
case 5:
buttons2_i.setLabel("=");
break;
}
}// endif
else // else1
{
if (i < 3) {
r2++;
Button buttons2_i = new Button(""); //
p2.add(buttons2_i);
buttons2_i.setBackground(Color.cyan);
buttons2_i.addActionListener(this);
switch (r2) {
case 1:
buttons2_i.setLabel("(");
break;
case 2:
buttons2_i.setLabel(")");
break;
case 3:
buttons2_i.setLabel("sqrt");
break;
}
}// endif
else {
if (i > 16 && i < 19) {
r3++;
Button buttons2_i = new Button("");
p2.add(buttons2_i);
buttons2_i.addActionListener(this);
switch (r3) {
case 1:
buttons2_i.setLabel(".");
break;
case 2:
buttons2_i.setLabel("CE");
break;
}
}// endif
else {
Integer name = new Integer(k);
k--;
s_show = name.toString();
Button buttons2_i = new Button(s_show);
p2.add(buttons2_i);
buttons2_i.addActionListener(this);
}
}
}// end else1
}// end for
s_show = "";
s_go = "";
this.setVisible(true);
}// end Calculator()構造方法
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof Button) // if1
{
if (e.getActionCommand() == "CE")// Del
{
s_show = "";
s_go = "";
textfields.setText(s_show);
} else if (e.getActionCommand() == "Backspace") {
if (s_show.equals("")) {
s_go = "";
textfields.setText("");
} else {
if (s_go.length() > 0)
s_go = s_go.substring(0, s_go.length() - 1);
s_show = s_show.substring(0, s_show.length() - 1);
textfields.setText(s_show);
}
} else if (e.getActionCommand() == ".") {
if (textfields.getText().equals(""))
textfields.setText("0.");
else {
s_show += e.getActionCommand();
s_go += e.getActionCommand();
textfields.setText(s_show);
}
} else {
if (e.getActionCommand() != "=") {
if (e.getActionCommand().equals("sqrt")) {
s_show += e.getActionCommand();
s_go += e.getActionCommand();
textfields.setText(s_show);
s_go = s_go.substring(0, s_go.length() - 3);
} else {
s_show += e.getActionCommand();
s_go += e.getActionCommand();
textfields.setText(s_show);
}
} else {
s_show += e.getActionCommand();
s_go += e.getActionCommand();
textfields.setText(do1.operator(s_go)); // ().toString()
}
}// end else
}// end if1
if(e.getSource()=="copy"){
sedit=textfields.getSelectedText();
StringSelection text=new StringSelection(sedit);
clp.setContents(text, null);
}
/**else if(e.getSource()=="zhantie"){
Transferable contents=clp.getContents(this);//返回表示剪貼板當前內容的 transferable 對象。
DataFlavor flavor=DataFlavor.stringFlavor;//
String ss;
if (contents.isDataFlavorSupported(flavor)) {
try
{
int start=textfields.getSelectionStart();
int end=textfields.getSelectionEnd();
//textfields.replaceRange("",start,end);
textfields.setText(ss);
ss = (String)contents.getTransferData(flavor);
int n=textfields.getCaretPosition();
textfields.insert(ss,n);
}catch(Exception ee){
// TODO: handle exception
}
}
}*///未實現剪貼板粘貼功能
// end actionPerformed()
// end class Calculator
}
public static void main(String args[]) {
Calculator cal = new Calculator();
}
}