计算:
package com.comput;
import java.util.*;
class stack{
private ArrayList<String> Q1;
int length;
String top;
public stack() {
Q1=new ArrayList<String>();
length=0;
top=null;
}
public void push(String s)
{
Q1.add(s);
top=s;
length++;
}
public String pop() {
if(length>0)
length--;
String s1 =Q1.remove(length);
top= length == 0 ? null:Q1.get(length-1);
return s1;
}
public void print() {
System.out.println(Q1);
}
}
class make{
//处理字符串
public Double Make(String s) {
return compute(Chu(fun(s)));
}
public ArrayList<String> fun(String s) {
ArrayList<String> list = new ArrayList<String>();
String ls = new String();
int i=0;
char str;
str = s.charAt(i);
while(i<s.length()) {
int c = i;
while(i<s.length()&&((str>='0'&&str<='9')||str=='.'))
{
ls=ls+str;
i++;
if(i>=s.length())
break;
str=s.charAt(i);
}
if(c!=i)
{
list.add(ls);
ls = new String();
c=i;
}
while(i<s.length()&&(str>='a'&&str<='z'))
{
ls=ls+str;
i++;
if(i>=s.length())
break;
str=s.charAt(i);
if(str=='(')
str=s.charAt(++i);
}
if(c!=i)
{
list.add("0");
list.add(ls);
ls = new String();
c=i;
}
if(str=='+'||str=='-'||str=='*'||str=='÷'||str=='%'||str=='('||str==')')
{
ls+=str;
list.add(ls);
ls = new String();
i++;
if(i<s.length())
str=s.charAt(i);
}
}
System.out.println(list);
return list;
}
//等式变为中序序列
public ArrayList<String> Chu(ArrayList<String> list){
stack list0 = new stack();
ArrayList<String> ls = new ArrayList<String>();
for(String str:list)
{
System.out.println(ls);
list0.print();
if(str.matches("[0-9]+")) {
ls.add(str);
}else {
if(str.equals("("))
list0.push(str);
else if(str.equals("sin"))
list0.push("s");
else if(str.equals("cos"))
list0.push("c");
else if(str.equals("tan"))
list0.push("t");
else if(str.equals("log"))
list0.push("l");
else if(str.equals("sqrt"))
list0.push("q");
else if(str.equals("ln"))
list0.push("n");
else if(str.equals(")"))
{
while(list0.length!=0&&!list0.top.equals("s")&&!list0.top.equals("c")&&!list0.top.equals("t")
&&!list0.top.equals("l")&&!list0.top.equals("q")&&!list0.top.equals("n")&&!list0.top.equals("("))
{
ls.add(list0.pop());
}
if(list0.length!=0&&list0.top.equals("("))
list0.pop();
else
ls.add(list0.pop());
System.out.println(" "+ls);
}
else
{
while(list0.length!=00&&get(str)<=get(list0.top))
ls.add(list0.pop());
list0.push(str);
}
}
}
while(list0.length!=0)
ls.add(list0.pop());
System.out.println(ls);
return ls;
}
public double compute(ArrayList<String> lss) {
stack Q = new stack();
for(String str:lss) {
Q.print();
if(str.matches("\\d+")) {
Q.push(str);
}
else {
double a = Double.parseDouble(Q.pop());
double b = Double.parseDouble(Q.pop());
double c;
//System.out.println(a+" "+b);
if(str.equals("+")) {
b=a+b;
}else if(str.equals("-")){
b=b-a;
}else if(str.equals("*")) {
b=a*b;
// System.out.println(b);
}else if(str.equals("÷")) {
b=b/a;
}else if(str.equals("%")) {
b=b%a;
}else if(str.equals("s")) {
b=Math.sin(Math.toRadians(a));
}else if(str.equals("c")) {
b=Math.cos(Math.toRadians(a));
}else if(str.equals("t")) {
b=Math.tan(Math.toRadians(a));
}else if(str.equals("l")) {
b=Math.log10(a);
}else if(str.equals("q")) {
b=Math.sqrt(a);
}else if(str.equals("n")) {
b=Math.log(a);
}
Q.push(""+b);
}
}
String str = Q.pop();
return Double.parseDouble(str);
}
//运算符等级
public int get(String s) {
if(s.equals("+")||s.equals("-"))
return 2;
else if(s.equals("*")||s.equals("÷")||s.equals("%"))
return 3;
else if(s.equals("(")||s.equals(")"))
return 0;
else
return 0;
}
}
public class Main {
public static void main(String[] args) {
make st = new make();
System.out.println(st.compute(st.Chu(st.fun("2*cos(70-5*2)+2"))));
}
}
界面:
package com.comput;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class pan extends JFrame{
JPanel sn = new JPanel(new GridLayout(6,5));
JTextField fn = new JTextField(100);
JTextArea vn = new JTextArea(3,420);
Font cn = new Font("宋体",Font.BOLD,20);
String[] str ={"sin","cos","tan","log","sqrt","x^2","1/x","π","÷","%","ln","7","8","9","+","del","4","5","6","-","CE","1","2","3","*","(",")","0",".","="};
JButton[] button = new JButton[str.length];
public void Grid() {
sn.setFont(cn);
fn.setFont(cn);
fn.setHorizontalAlignment(JTextField.RIGHT);
setLayout(new BorderLayout());
setSize(420,480);
setTitle("计算器");
for(int i=0; i<str.length; i++)
{
button[i] = new JButton(str[i]);
button[i].setFont(cn);
sn.add(button[i]);
String s1 = button[i].getText();
button[i].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String s2 = fn.getText();
if(s1.equals("sin")) fn.setText(s2+"sin(");
else if(s1.equals("cos")) fn.setText(s2+"cos(");
else if(s1.equals("tan")) fn.setText(s2+"tan(");
else if(s1.equals("log")) fn.setText(s2+"log(");
else if(s1.equals("sqrt")) fn.setText(s2+"sqrt(");
else if(s1.equals("tan")) fn.setText(s2+"tan(");
else if(s1.equals("ln")) fn.setText(s2+"ln(");
else if(s1.equals("del")) {
String s3 = s2.substring(0,s2.length()-1);
fn.setText(s3);
}
else if(s1.equals("CE"))
{
fn.setText("");
}
else if(s1.equals("=")) {
double z;
make st = new make();
z=st.compute(st.Chu(st.fun(s2)));
fn.setText(""+z);
}
else {
fn.setText(s2+s1);
}
}
});
}
getContentPane().add(fn,BorderLayout.NORTH);
getContentPane().add(vn);
getContentPane().add(sn,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
}
public class Draw {
public static void main(String[] args) {
pan sn = new pan();
sn.Grid();
}
}
代码还有小错误,会继续追加更改