二代计算器完成除了同层括号问题之外其他问题都已解决
可以简单的实现四则运算及括号优先运算(实现失败)
package gouzao;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import java.text.DecimalFormat;
import javax.swing.*;
import org.omg.CORBA.TCKind;
public class computer extends JFrame implements ActionListener{
String[] str= {"7","8","9","DEL","Replay","4","5","6","×","÷","1","2","3","+","-","0","(",")","=","end"};
JPanel p = new JPanel();
DecimalFormat df = new DecimalFormat("0");
int round=0,round1=0;
char un=' ';
int x=1,o=0,s=0,y=0,r=0,m=0,w=0,l=0,d=0,kl=0;//开关:x表示字符长度,o判断用户在输入运算符之前是否按了0键(o为0时其他数字键可以替代0)
//s,y,m,w判断用户是否输入了+,-,×,÷键(控制用户防止用户输入两次同样的运算符及其他非法操作),r判断用户是否输入加减乘除;
double sum=0,befor=0,k=0;
char c[]= {'/','/','/','/','/','/','/','/','/','/','/','/','/','/','/'};
JTextField textA;
String t="0 ";
public computer() {
setLayout(new BorderLayout());
setFont(new Font("Arial",Font.PLAIN,14));
textA = new JTextField("",5);
textA.setFont(new Font("Arial",Font.PLAIN,30));
textA.disable();
getContentPane().add("North",textA);
p.setLayout(new GridLayout(4,5));
for(int i=0;i<20;i++) {
JButton b = new JButton(str[i]);
b.addActionListener(this);
b.setActionCommand(str[i]);
p.add(b);
}
getContentPane().add("Center",p);
}
public static void main(String[] agrs) {
computer c = new computer();
c.setTitle("计算器");
c.pack();
c.setVisible(true);
c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
c.setLocationRelativeTo(null);
while(true) {
c.textA.setText(c.t);
}
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("7")) {
if(x==15||d==1) {
return;
}else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0) {
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'7');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;m=0;w=0;l=0;;
}else if(e.getActionCommand().equals("8")) {
if(x==15||d==1) {
return;
}else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'8');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;m=0;w=0;l=0;;
}else if(e.getActionCommand().equals("9")) {
if(x==15||d==1) {
return;
}else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'9');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;m=0;w=0;l=0;;
}else if(e.getActionCommand().equals("4")) {
if(x==15||d==1) {
return;
}else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'4');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;m=0;w=0;l=0;;
}else if(e.getActionCommand().equals("5")) {
if(x==15||d==1) {
return;
}else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'5');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;m=0;w=0;l=0;;
}else if(e.getActionCommand().equals("6")) {
if(x==15||d==1) {
return;
}else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'6');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;m=0;w=0;l=0;;
}else if(e.getActionCommand().equals("1")) {
if(x==15||d==1) {
return;
}else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'1');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;m=0;w=0;l=0;;
}else if(e.getActionCommand().equals("2")) {
if(x==15||d==1) {
return;
}else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格//为0时退格
x=x-1;
}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'2');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;m=0;w=0;l=0;
}else if(e.getActionCommand().equals("3")) {
if(x==15||d==1) {
return;
}else if(t.charAt(0)=='0'&&s==0&&y==0&&m==0&&r==0){//为0时退格
x=x-1;}
if(o==1) {
x=x-1;
o=0;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'3');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
s=0;y=0;m=0;w=0;l=0;
}else if(e.getActionCommand().equals("0")) {
if(x==15||d==1) {
return;
}else if(t.charAt(0)=='0'&&r==0){
return;
}else if(o==1){
return;
}else
{
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'0');
strBuilder.toString();
t=strBuilder.toString();
if(s==1||y==1||m==1) {
o=1;
}
x=x+1;
s=0;y=0;m=0;w=0;l=0;
}
}else if(e.getActionCommand().equals("+")) {
if((x==15||x==14)&&(y==0&&m==0&&w==0)) {
return;
}else if(s==1||l==1) {
return;
}else {
if(y==1||m==1||w==1){
x=x-1;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'+');
strBuilder.toString();
t=strBuilder.toString();
s=1;
y=0;
w=0;
x=x+1;
d=0;
o=0;
r=1;
}
}else if(e.getActionCommand().equals("-")) {
if((x==15||x==14)&&(s==0&&m==0&&w==0)) {
return;
}else if(y==1||l==1) {
return;
}else
{
if(s==1||m==1||w==1){
x=x-1;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'-');
strBuilder.toString();
t=strBuilder.toString();
y=1;
s=0;
m=0;
d=0;
w=0;
x=x+1;
o=0;
r=1;
}
}else if(e.getActionCommand().equals("=")) {
int od=0;
int gl=0;
int kl=0;
for(;round>0;round--) {
if(t.charAt(x-1)=='+'||t.charAt(x-1)=='-'||t.charAt(x-1)=='×'||t.charAt(x-1)=='÷')
x=x-1;
if(t.charAt(x-1)=='(') {
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,' ');
strBuilder.toString();
t=strBuilder.toString();
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,')');
strBuilder.toString();
t=strBuilder.toString();
x=x+1;
}
do {
if(t.charAt(gl)=='(') {
od++;
}
gl++;
}while(t.charAt(gl)!=')'&&t.charAt(gl)!=' ');
int j=0;
double []a=new double[15];
int b=0;
String[] t2=new String[21];
while(t.charAt(j)!=' ') {
String t1="0 ";
for(int i=j;i<20;i++) {
if(t.charAt(i)>='0'&&t.charAt(i)<='9') {
StringBuilder strBuilder1 = new StringBuilder(t1);
strBuilder1.setCharAt(i-j,t.charAt(i));
strBuilder1.toString();
t1=strBuilder1.toString();
}else {
c[b]=t.charAt(i);
j=i+1;
break;
}
}
double all=0;
for(int g=0;g<t1.replace(" ","").length();g++) {
double u=(int)Math.pow(10,t1.replace(" ","").length()-g-1);
all=all+u*Integer.parseInt(t1.charAt(g)+"");
}
t2[b]=t1;
a[b]=all;
b=b+1;
}
if(sum==0||a[0]==befor) {
sum=a[0];
}else {
sum=0;
}
for(int l=0;l<=100;l++) {
System.out.println(round1+" "+c[l]+" "+c[l+1]+" "+l+" "+sum+" "+kl);
if(l==0)
sum=a[0];
if(l!=0) {
if(c[l-1]=='(')
sum=a[l];
}
if(c[l]=='+'&&c[l+1]=='(') {
sum=sum+k;
}else if(c[l]=='-'&&c[l+1]=='(') {
sum=sum-k;
}else if(c[l]=='÷'&&c[l+1]=='(') {
sum=sum/k;
}else
if((c[l]=='('&&od==0)||c[l]=='/') {
break;
}else if(od!=0&&kl==0) {//括号跳转
if(round1!=od) {
round1=round1+1;
while(c[l]!='('){
l++;
}
if(round1==od)
kl=1;
continue;
}
}else if(kl==1&&(c[l]=='('||c[l]==')')) {//括号跳转
if(od==1) {
l=-1;
k=sum;
od--;
round1--;
kl=1;
}else {
round1--;
od--;
kl=1;
l--;
while(c[l]!='('&&l!=0){
l--;
}
l--;
while(c[l]!='('&&l!=0){
l--;
}
k=sum;
continue;
}
}else if(c[l]=='+'&&kl==1) { //加法
if(l!=0) {
if(c[l-1]=='(')
sum=a[l];
}
double u=a[l+1];
while(c[l+1]=='÷'||c[l+1]=='×') {
if(c[l+1]=='×'&&c[l+2]!='(') {
u=u*a[l+2];
}else if(c[l+1]=='÷'&&c[l+2]!='('){
u=u/a[l+2];
}else if(c[l+1]=='×'&&c[l+2]=='(') {
u=u*k;
}else if(c[l+1]=='÷'&&c[l+2]=='(') {
u=u/k;
}
l=l+1;
}
sum=sum+u;
}else if(c[l]=='-'&&kl==1) { //减法
if(c[l-1]=='(')
sum=a[l];
double u=a[l+1];
while(c[l+1]=='÷'||c[l+1]=='×') {
if(c[l+1]=='×'&&c[l+2]!='(') {
u=u*a[l+2];
}else if(c[l+1]=='÷'&&c[l+2]!='('){
u=u/a[l+2];
}else if(c[l+1]=='×'&&c[l+2]=='(') {
u=u*k;
}else if(c[l+1]=='÷'&&c[l+2]=='(') {
u=u/k;
}
l=l+1;
}
sum=sum-u;
}
else if(c[l]=='×'&&kl==1) { //乘法
System.out.println(" "+sum);
if(c[l-1]=='(')
sum=a[l];
double u=a[l+1];
if(c[l+1]=='(')
u=k;
while(c[l+1]=='÷'||c[l+1]=='×') {
if(c[l+1]=='×'&&c[l+2]!='(') {
u=u*a[l+2];
}else if(c[l+1]=='÷'&&c[l+2]!='('){
u=u/a[l+2];
}else if(c[l+1]=='×'&&c[l+2]=='(') {
u=u*k;
}else if(c[l+1]=='÷'&&c[l+2]=='(') {
u=u/k;
}
l=l+1;
}
sum=sum*u;
}else if(c[l]=='÷'&&kl==1) { ///除法
if(c[l-1]=='(')
sum=a[l];
double u=a[l+1];
while(c[l+1]=='÷'||c[l+1]=='×') {
if(c[l+1]=='×'&&c[l+2]!='(') {
u=u*a[l+2];
}else if(c[l+1]=='÷'&&c[l+2]!='('){
u=u/a[l+2];
}else if(c[l+1]=='×'&&c[l+2]=='(') {
u=u*k;
}else if(c[l+1]=='÷'&&c[l+2]=='(') {
u=u/k;
}
l=l+1;
}
sum=sum/u;
}
}
int f=0;
for(int m=0;m<b;m++) {
if(t2[m].replace(" ","").length()>10) {
t="error";
sum=0;
f=1;
}else if(sum==befor&&befor!=0) {
f=1;
}
}
if(f==0) {
t=String.valueOf(df.format(sum));
}else {
t=String.valueOf(df.format(befor));
}
x=t.replace(" ", "").length();
for(int h=0;h<16-(x-1);h++) {
t=t+" ";
}
t=t.replace("+","");
t=t.replace("-","");
t=t.replace("×","");
befor=sum;
double wr=0;
for(int g=0;g<t.replace(" ","").length();g++) {
double u=(int)Math.pow(10,t.replace(" ","").length()-g-1);
wr=wr+u*Integer.parseInt(t.charAt(g)+"");
}
sum=wr;
s=0;y=0;m=0;w=0;l=0;r=0;
}else if(e.getActionCommand().equals("Replay")) { //重置
t="0 ";
x=1;
m=0;
un=' ';
y=0;
s=0;
round=0;
w=0;
k=0;
sum=0;
r=0;
befor=0;
d=0;
l=0;
}else if(e.getActionCommand().equals("×")) {
if((x==15||x==14)&&(s==0&&y==0&&w==0)) {
return;
}else if(m==1||l==1) {
return;
}else
{
if(s==1||y==1||w==1){
x=x-1;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'×');
strBuilder.toString();
t=strBuilder.toString();
m=1;
y=0;
w=0;
s=0;
x=x+1;
o=0;
r=1;
d=0;
}
}else if(e.getActionCommand().equals("÷")) {
if((x==15||x==14)&&(s==0&&y==0&&m==0)) {
return;
}else if(w==1||l==1) {
return;
}else
{
if(s==1||y==1||m==1){
x=x-1;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'÷');
strBuilder.toString();
t=strBuilder.toString();
w=1;
m=0;
y=0;
s=0;
x=x+1;
o=0;
d=0;
r=1;
}
}else if(e.getActionCommand().equals("(")) {
if(x==15||x==14) {
return;
}else if(l==1||(s==0&&y==0&&m==0&&w==0)) {
return;
}else{
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'(');
strBuilder.toString();
t=strBuilder.toString();
round=round+1;
s=0;
l=1;
y=0;
w=0;
x=x+1;
o=0;
r=1;
}
}
else if(e.getActionCommand().equals(")")){
if(x==15){
return;
}else if(round==0||t.charAt(x-1)=='(') {
return;
}else if(s==1||y==1||m==1){
x=x-1;
}
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,')');
strBuilder.toString();
t=strBuilder.toString();
s=0;
d=1;
round=round-1;
l=0;
y=0;
w=0;
x=x+1;
o=0;
r=1;
}
else if(e.getActionCommand().equals("DEL")) {
if(x!=0&&x!=1) {
x=x-1;
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,' ');
strBuilder.toString();
t=strBuilder.toString();
}else if(x==1&&t.charAt(1)!=0) {
x=x-1;
StringBuilder strBuilder = new StringBuilder(t);
strBuilder.setCharAt(x,'0');
strBuilder.toString();
t=strBuilder.toString();
x++;
}
System.out.println(x);
}
else if(e.getActionCommand().equals("end")){
System.exit(0);
}
}
}