1.界面
2.计算器功能
/**
* 计算器功能:
* 1.能实现带括号的浮点数的简单四则运算
* 2.add const π e
* 3.1/x x^2 x^(1/2)即sqr
*/
3.代码
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Pattern;
/**
* 计算器功能:
* 1.能实现带括号的浮点数的简单四则运算
* 2.add const π e
* 3.1/x x^2 x^(1/2)即sqr
*/
public class Calculator extends JFrame implements ActionListener{
private final String[] KEYS={"7","8","9","+","4","5","6","-","1","2","3","*","0","e","π","÷","(",")",".","%","1/x","√","x^2","=","x^y","∁","✘"};
private final JButton[] keys=new JButton[KEYS.length];
private final JTextField resultText = new JTextField("");//初始可输入的textFiled为空
private String b="",str_x_y="";//初始输入的字符串为空
private int signal=0,signal1=0;//用于计算x^y
public Calculator(){//页面的基础布局
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();
StringBuffer sb1=new StringBuffer();//1/x
StringBuffer sb2=new StringBuffer();//√
StringBuffer sb3=new StringBuffer();//x^2
StringBuffer sb4=new StringBuffer();//x^y
if(signal==1&&b.length()>0){// x^y
sb4.delete(0,sb4.length());
int index=b.indexOf("^("),index1,index2=0;
String str1="",str2="",x,y;
if(label.equals(")")){
signal=0;signal1=1;
str_x_y=b.substring(index+1)+")";//^()中括号里面的数
System.out.println("str_x_y:"+str_x_y);
if(str_x_y!=""){//确定y
y=Result(Cal1(str_x_y));
System.out.println("x^y中的y:"+y);
if("0123456789.".indexOf(b.charAt(index-1))>=0){//不带括号,即前一个数
for(int i = index-1; i >= 0 && "0123456789.".indexOf(b.charAt(i)) >= 0; i--){//将多位浮点数看作一个字符串存入result数组中
sb4.append(b.charAt(i));
}
str1 = sb4.reverse().toString();
System.out.println("不带括号,即前一个数"+str1);
}if(")".indexOf(b.charAt(index-1))>=0){//带括号
int i = index-1;
index1=i;//)
do{
i--;//0123
}while ("(".indexOf(b.charAt(i))<0&&i>=1);
index2=i;//(
str2 =Result(Cal1(b.substring(index2+1,index1)));//
System.out.println(" ( )^里的数:"+b.substring(index2+1,index1));
}if(str1!=""){
x=str1;
b=b.substring(0,index-str1.length())+Cal5(x,y);
System.out.println(b);
resultText.setText(b.substring(0,b.length()-1));
}if(str2!=""){//确定x
x=str2;
b=b.substring(0,index2)+Cal5(x,y);
System.out.println(b);
resultText.setText(b.substring(0,b.length()-1));
}
}
}
//b=b.substring(0,b.length()-1)+")";
//System.out.println("b:"+b);
//resultText.setText(b);
}
if(b.length()>0){
int index=b.indexOf("ERROR");
if(index!=-1){
b=""+b.substring(index+5);
}
}
switch (label) {
case "∁"://清空
signal=0;
b ="";
resultText.setText(b);
break;
case "✘"://清除一个字符
if(b.length()>0) {
b = b.substring(0, b.length() - 1);
resultText.setText(b);
}else {
resultText.setText("");
}
break;
case "="://得出结果
if(b.length()>0) {
int len=b.length();
for(int i=0;i<=len-1;i++){//满足带小数点的运算
if(b.charAt(0)=='.'||b.charAt(0)=='-'||b.charAt(0)=='+'){//满足带负数的运算 eg:-6-9 ----> 0-6-9
b="0"+b;len++;//.9==0.9,-9=0-9
}if(b.charAt(len-1)=='.'){//9.==9.0
b=b+"0";len++;
}if("+-*÷%".indexOf(b.charAt(len-1))>=0){//eg:1+9-=1+9
b=b.substring(0,len-1);len--;//!!!!!
resultText.setText(b);
}if(b.charAt(len-1)==')'){//1+(1+9-)=1+(1+9)
if("+-*÷%".indexOf(b.charAt(len-2))>=0){
b=b.substring(0,len-2)+")";len--;//(1+8*)
resultText.setText(b);
}
}if(b.charAt(0)=='('){
if('+'==b.charAt(1)){//(+1+8)=(0+1+8)
b="(0+"+b.substring(2);len++;
}if('-'==b.charAt(1)){//(-1+8)=(0-1+8)
b="(0-"+b.substring(2);len++;
}if("*÷%".indexOf(b.charAt(1))>=0){
b="("+b.substring(2);len--;//(*9-8)=(9-8)
resultText.setText(b);
}
}if(i>=1&&i<=len-2){
if ('.'==b.charAt(i)){//
System.out.println("i:"+i);
if("0123456789".indexOf(b.charAt(i-1))<0){
b=b.substring(0,i)+"0"+b.substring(i);len++;//1-.9==1-0.9
}else if("0123456789".indexOf(b.charAt(i+1))<0){
b=b.substring(0,i+1)+"0"+b.substring(i+1);len++;// 9.+8==9.0+8
}
}
if('('==b.charAt(i)){//
if("0123456789".indexOf(b.charAt(i-1))>=0){
b=b.substring(0,i)+"*"+b.substring(i);len++;
}if(b.charAt(i+1)=='+'){
b=b.substring(0,i+1)+"0"+b.substring(i+1);len++;
}if(b.charAt(i+1)=='-'){
b=b.substring(0,i+1)+"0"+b.substring(i+1);len++;
}if("*÷%".indexOf(b.charAt(i+1))>=0&&i+2<=len-1){
b=b.substring(0,i+1)+b.substring(i+2);len--;
}
}
if(')'==b.charAt(i)){//
if("0123456789".indexOf(b.charAt(i+1))>=0){
b=b.substring(0,i+1)+"*"+b.substring(i+1);len++;
}if("+-*÷%".indexOf(b.charAt(i-1))>=0){
b=b.substring(0,i-1)+b.substring(i);len--;
}
}
}
}
//处理x^y
if(b.length()>0){
System.out.println(b);
String []temp1 = Cal1(b);//中缀转后缀
String result = Result(temp1);//计算后缀结果
b = result + "";
resultText.setText(b);
}
}
else {
resultText.setText("");
}
break;
case "1/x"://倒数
if(b.length()>0) {
sb1.delete(0,sb1.length());//清空sBuffer
int index1,index2;
String str1,str2,t1,t2;
if ("0123456789.".indexOf(b.charAt(b.length()-1))>=0){//不带括号
for (int i = b.length()-1;i>=0&&"0123456789.".indexOf(b.charAt(i))>=0;i--) {//将多位浮点数看作一个字符串存入result数组中
sb1.append(b.charAt(i));
}
str1=sb1.reverse().toString();
System.out.println("1/x:"+str1);
t1 = Cal4(str1);
if(!t1.equals("ERROR")){
b=b.substring(0,b.length()-str1.length())+t1;
}else b="ERROR";
}if(")".indexOf(b.charAt(b.length()-1))>=0){//带括号
int i = b.length()-1;
index1=i;//)
do{
i--;//0123
}while ("(".indexOf(b.charAt(i))<0&&i>=1);
index2=i;//(
str2=b.substring(index2+1,index1);
System.out.println("1/x ( )里的数:"+str2);
t2 =Cal4(Result(Cal1(str2)));
if(!t2.equals("ERROR")){
b=b.substring(0,b.length()-(index1-index2+1))+"("+t2+")";
}else b="ERROR";
}
resultText.setText(b);
}else {
resultText.setText("");
}
break;
case "√"://开方
if(this.b.length()>0) {
sb2.delete(0,sb2.length());//清空sb2
int index1,index2;
String str1,str2,t1,t2;
if ("0123456789.".indexOf(b.charAt(b.length()-1))>=0){//不带括号
for (int i = b.length()-1;i>=0&&"0123456789.".indexOf(b.charAt(i))>=0;i--) {//将多位浮点数看作一个字符串存入result数组中
sb2.append(b.charAt(i));
}
str1=sb2.reverse().toString();
System.out.println("√:"+str1);
t1 = Cal2(str1);
if(!t1.equals("ERROR")){
b=b.substring(0,b.length()-str1.length())+t1;
}else b="ERROR";
}if(")".indexOf(b.charAt(b.length()-1))>=0){//带括号
int i = b.length()-1;
index1=i;//)
do{
i--;//0123
}while ("(".indexOf(b.charAt(i))<0&&i>=1);
index2=i;//(
str2=b.substring(index2+1,index1);
System.out.println("√ ( )里的数:"+str2);
t2 =Cal2(Result(Cal1(str2)));
if(!t2.equals("ERROR")){
b=b.substring(0,b.length()-(index1-index2+1))+"("+t2+")";
}else b="ERROR";
}
resultText.setText(b);
}else {
resultText.setText("");
}
break;
case "x^2"://平方
if(this.b.length()>0) {
sb3.delete(0,sb3.length());//清空sb2
int index1,index2;
String str1,str2,t1,t2;
if ("0123456789.".indexOf(b.charAt(b.length()-1))>=0){//不带括号
for (int i = b.length()-1;i>=0&&"0123456789.".indexOf(b.charAt(i))>=0;i--) {//将多位浮点数看作一个字符串存入result数组中
sb3.append(b.charAt(i));
}
str1=sb3.reverse().toString();
System.out.println("x^2:"+str1);
t1 = Cal3(str1);
if(!t1.equals("ERROR")){
b=b.substring(0,b.length()-str1.length())+t1;
}else b="ERROR";
}if(")".indexOf(b.charAt(b.length()-1))>=0){//带括号
int i = b.length()-1;
index1=i;//)
do{
i--;//0123
}while ("(".indexOf(b.charAt(i))<0&&i>=1);
index2=i;//(
str2=b.substring(index2+1,index1);
System.out.println("x^2( )里的数:"+str2);
t2 =Cal3(Result(Cal1(str2)));
b=b.substring(0,b.length()-(index1-index2+1))+"("+t2+")";
}
resultText.setText(b);
}else {
resultText.setText("");
}
break;
case "x^y":
if(b.length()>0){
signal=1;
b=b+"^(";
resultText.setText(b);
}else{
resultText.setText("");
}
break;
case "e"://常数e
String temp4 = String.valueOf(2.7182818284590);
b = b + temp4;
resultText.setText(b);
break;
case "π"://常数Π
String temp5 = String.valueOf(3.1415926535897);
b = b + temp5;
resultText.setText(b);
break;
case "."://小数点
if(b.length()==0)
{
b = b + label;
}else {
int index=-1;String s;
for (int i=b.length()-1;i>=0;i--){//找到上一个.的位置
if(".".indexOf(this.b.charAt(i))>=0){
index=i;
break; }
}//System.out.println(index);
if(index!=-1){//找到上一个.
s=b.substring(index);
if(s.contains("+")||s.contains("-")||s.contains("*") ||s.contains("%")||s.contains("÷")){
b = b + label;
}
}else {//没有上一个.
b = b + label;
}
resultText.setText(b);//不允许同一个数中小数点的出现次数超过1
}
break;
case "-":
case "+":
case "*":
case "÷":
case "%":
if(b.length()>0){
if("+-*÷%".indexOf(b.charAt(b.length()-1))>=0){//+-*÷%这5个运算符不能连续出现
b=b.substring(0,b.length()-1)+label;
}else{ b=b+label;}
resultText.setText(b);
}else{
if(label.equals("-")){//首次输入时,允许输入负号 ,代表负数
b=b+label;
resultText.setText(b);
}else resultText.setText("");
}
break;
default://其他情况,自然连接即可
if (signal1 == 0) {
this.b = b + label;
}
resultText.setText(b);
signal1=0;
break;
}
}
StringBuffer sBuffer = new StringBuffer();
private String[] Cal1(String str){//中缀转后缀
char []stack=new char[100];//
int top=-1,j=0;//栈顶指针,控制变量
String []result = new String[100];//存储后缀表达式
for (int i = 0; i<str.length(); i++){
if ("0123456789.".indexOf(str.charAt(i))>=0){//遇到数字直接存入后缀表达式
sBuffer.delete(0,sBuffer.length());//清空sBuffer
for (;i<str.length()&&"0123456789.".indexOf(str.charAt(i))>=0;i++)
{//将多位浮点数看作一个字符串存入result数组中
sBuffer.append(str.charAt(i));
}
i--;
result[j]=sBuffer.toString();
j++;
}else if ("(".indexOf(str.charAt(i))>=0) {//遇到( 存于栈内
top++;
stack[top]=str.charAt(i);
}else if (")".indexOf(str.charAt(i))>=0) {//遇到),将栈顶元素存入后缀表达式直到遇到(为止,此时将(删掉
while (true) {
if(stack[top]!='('){
result[j]=stack[top]+"";
j++;
top--;
}else {
top--;
break;
}
}
}else if ("*÷%".indexOf(str.charAt(i))>=0) {
if (top==-1) {//栈为空 直接加入
top++;
stack[top]=str.charAt(i);
}else{//栈非空
if ("*÷%".indexOf(stack[top])>=0){
result[j]=stack[top]+"";
top--;
j++;
if(top!=-1){
i--;//继续比较
}else{
stack[++top]=str.charAt(i);
}
}else if("(".indexOf(stack[top])>=0){
top++;
stack[top]=str.charAt(i);
}else if ("+-".indexOf(stack[top])>=0){
top++;
stack[top]=str.charAt(i);
}
}
}else if ("+-".indexOf(str.charAt(i))>=0){
if (top==-1){
top++;
stack[top]=str.charAt(i);
}else{
if ("*÷%".indexOf(stack[top])>=0){
result[j]=stack[top]+"";
top--;
j++;
if(top!=-1){
i--;//继续比较
}else{
stack[++top]=str.charAt(i);
}
}else if ("(".indexOf(stack[top])>=0){
top++;
stack[top]=str.charAt(i);
}else if ("+-".indexOf(stack[top])>=0) {
result[j]=stack[top]+"";
top--;
j++;
if(top!=-1){
i--;//继续比较
}else{
stack[++top]=str.charAt(i);
}
}
}
}
}
while (top!=-1) {
result[j]=stack[top]+"";
j++;
top--;
}
// 输出后缀表达式
// for (String s:result) {
// System.out.println(s);
// }
return result;
}
StringBuffer Cal2_result=new StringBuffer();
public String Cal2(String str){//开方√
Cal2_result.delete(0,Cal2_result.length());
double a=Double.parseDouble(str),b;
if(a<0){
return "ERROR";
}else{
b=Math.sqrt(a);
Cal2_result.append(b);
return Cal2_result.toString();
}
}
StringBuffer Cal3_result=new StringBuffer();
public String Cal3(String str) {//平方
Cal3_result.delete(0,Cal3_result.length());
double a=Double.parseDouble(str),b;
b=Math.pow(a,2);
Cal3_result.append(b);
return Cal3_result.toString();
}
StringBuffer Cal4_result=new StringBuffer();
public String Cal4(String str) {//倒数 1/x
Cal4_result.delete(0,Cal4_result.length());
double a=Double.parseDouble(str),b;
if(a==0){
return "ERROR";
}else {
b=1.0/a;
Cal4_result.append(b);
return Cal4_result.toString();
}
}
StringBuffer Cal5_result=new StringBuffer();
public String Cal5(String x,String y) {//倒数 x^y
Cal5_result.delete(0,Cal5_result.length());
double X=Double.parseDouble(x),Y=Double.parseDouble(y),b;
b=Math.pow(X,Y);
Cal5_result.append(b);
return Cal5_result.toString();
}
public String Result(String []str)
{
Pattern reg=Pattern.compile("^\\d+(\\.\\d+)?$");//非负浮点数;^[0-9]+(\\.[0-9]+)?浮点数
//
String []Result=new String[100];
int Top=-1;
for (int i=0;str[i]!=null;i++){
if (reg.matcher(str[i]).matches()){//遇到数字字符进栈
Top++;
Result[Top]=str[i];
}if("+-*%÷".contains(str[i])){//操作符
double x,y,n;
x=Double.parseDouble(Result[Top]);
Top--;
y=Double.parseDouble(Result[Top]);
Top--;
if ("-".contains(str[i])){//减
n=y-x;
Top++;
Result[Top]=String.valueOf(n);
}if("+".contains(str[i])){//加
n=y+x;
Top++;
Result[Top]=String.valueOf(n);
}if("*".contains(str[i])){//乘
n=y*x;
Top++;
Result[Top]=String.valueOf(n);
}if("÷".contains(str[i])){//除
if (x==0) {
return "ERROR";
}else{
n=y/x;
Top++;
Result[Top]=String.valueOf(n);
}
}if("%".contains(str[i])){//求余数
if (x==0) {
return "ERROR";
}else{
n=y%x;
Top++;
Result[Top]=String.valueOf(n);
}
}
}
}
return Result[Top];
}
public static void main(String []args){
Calculator a=new Calculator();
}
}