Java calculator (with infinite parentheses) +string and double convert each other

Source: Internet
Author: User
Tags gettext

The Java teacher arranges the homework, wants us to compile a calculator, and must have the parenthesis function ... The disgusting, more than 600 lines of code, debugging to debug to finally be able to use, but estimated there are bugs.

There is a bug I know, but it does not affect normal use: there can be a leading zero, that is, there can be 0087 of such numbers, the calculator will think this is 87,.

Here are two, the left is the input, the right is the output.


The core algorithm of this calculator is to handle the string on the left, and get the result on the right side, which takes into account the problem of bracket matching, minus sign, multiplication priority and so on, this algorithm writes 200 lines.

Because the string is relatively short, up to dozens of, so my algorithm is more violent, without considering the complexity of time and space, a lot of string traversal (does not conform to my such a acmer rigorous scholarly spirit ah ...) It's a little lazy, actually.

In addition, the calculator can also automatically recognize the legitimacy of the input content, if not legal, you are not entered into some symbols

For example: If you have entered the 8+9, and then you press ') ' or ' (', the calculator will not respond, you only have to write a ' + ' or remove ' 9 ') to enter ' ('. This feature I was in the input when the things in each position recorded in a f[][] two-dimensional array, each time you enter a key to judge.

Another example of your current formula does not constitute a complete expression, press Enter or ' = ' is invalid, you can press ' Backspace ' back a little bit of modification, but do not support jumping change, can only change the last (my ability and energy is limited, if plus jump change, but also to add the left and right arrows, too troublesome).

This calculator supports mouse point button and keyboard input, press ' = ' or ' enter ' to produce results, press ' C ' to clear zero.

There's another thing that makes me crazy!!!! Because it is a beginner, not how to Java, at first do not know the string and double the conversion between the algorithm system has been done, I also made the change_to_double () and change_to_string () two functions, wrote hundreds of lines of code, Debugging these two functions have not been fixed for a long time, and then I know can use the system ready-made functions, it is simply to crash ...

The following is how the double and string are converted to each other.

String to double:

Double A;

String ls= "1.23"

A=double.parsedouble (LS);

Then a becomes a double type of 1.23.


Double to String: Two methods (do not know what the difference is, can be used)

Method 1: +a directly behind S (I think this is easy to write)

String s= "";

Double a=1.23

S=s+a;

Method 2: Use String.valueof (double);

String s= "";

Double a=1.23;

S==s.valueof (a);


Here is the code I implemented. Some places have comments.


Import java.awt.*;import java.awt.event.*;import javax.swing.*;p ublic class Calculator extends JFrame {static char a[]={ ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '};//static double k[]={0.1,0.001,0.0001,0.00001,0.000001,0.0000001};// What broken Java software, this thing put to other places all error JTextField textfield1;private JButton B0,b1,b2,b3,b4,b5,b6,b7,b8,b9,back,c,jia,jian , Cheng,chu,deng,dian,zuok,youk;public Calculator () {Super ("calculator"); Container Container=getcontentpane ();//container.setlayout (new FlowLayout ()); container.setlayout (null);// text box textfield1=new JTextField (""); textfield1=new JTextField (+); Textfield1.setbounds (+, 290, +); Container.add ( TEXTFIELD1);//various buttons back=new JButton ("Backspace"); Back.setbounds (a), Container.add (back); C=new JButton ("C"); C.setbounds, Container.add (C), Zuok=new JButton ("("); Zuok.setbounds (Max, Max, Max); Container.add ( Zuok), Youk=new JButton (")"), Youk.setbounds (260, Max, Max), Container.add (Youk); Jia=new JButton ("+"); Jia.setbounds ( (contain);Er.add (Jia), jian=new JButton ("-"), Jian.setbounds (260, N, N), Container.add (Jian); Cheng=new JButton ("*"); Cheng.setbounds, Container.add (Cheng), chu=new JButton ("/"); Chu.setbounds (260, 160, 50, 40); Container.add (CHU);d ian=new JButton ("."); Dian.setbounds (260, 110, 200), Container.add (Dian);d eng=new JButton ("=");d eng.setbounds (210, 90); Container.add (Deng), b0=new JButton ("0"), B0.setbounds (260), Container.add (B0), B1=new JButton ("1");     B1.setbounds (20, 210, 50, 40); Container.add (B1), B2=new JButton ("2"), B2.setbounds (n, Max, Max), Container.add (B2); B3=new JButton ("3"); B3.setbounds, Container.add (b3), B4=new JButton ("4"), B4.setbounds (A., () Container.add (B4); B5=new JButton ("5"); B5.setbounds (+ 140, 160, 50, 40) Container.add (B5); B6=new JButton ("6"); ; Container.add (B6); B7=new JButton ("7"); B7.setbounds (+, 8, +); Container.add (b7); B8=new JButton B8.setbounds (container, +, +);Add (B8); B9=new JButton ("9"); B9.setbounds ((+), Container.add (B9); Buttonjudge judge=new Buttonjudge (); B1.addactionlistener (judge); B2.addactionlistener (judge); B3.addactionlistener (judge); B4.addactionlistener (judge ); B5.addactionlistener (judge); B6.addactionlistener (judge); B7.addactionlistener (judge); B8.addactionlistener ( Judge); B9.addactionlistener (judge); B0.addactionlistener (judge); Jia.addactionlistener (judge); Jian.addactionlistener (judge); Cheng.addactionlistener (judge); Chu.addactionlistener (judge); Zuok.addactionlistener (judge), Youk.addactionlistener (judge);d Ian.addactionlistener (judge); Back.addactionlistener (judge); C.addactionlistener (judge);d Eng.addactionlistener (judge);//keyboard keyboard key=new keyboard (); B0.addkeylistener (KEY); B1.addkeylistener (key); B2.addkeylistener (key); B3.addkeylistener (key); B4.addkeylistener (key); B5.addkeylistener ( key); B6.addkeylistener (key); B7.addkeylistener (key); B8.addkeylistener (key); B9.addkeylistener (key); B0.addkeylistener (key); Jia.addkeylistener (key); JiAn.addkeylistener (key); Cheng.addkeylistener (key); Chu.addkeylistener (key); Zuok.addkeylistener (key); Youk.addkeylistener (Key);d Ian.addkeylistener (key); Back.addkeylistener (KEY); C.addkeylistener (Key);d Eng.addkeylistener (key); SetSize (340,350); setvisible (true);} public static void Main (string[] args) {Calculator application=new Calculator (); Application.setdefaultcloseoperation ( Jframe.exit_on_close);} int ct=0;int f[][]=new int[100][10];p ublic void memsetf () {/*f[i][0] means there is no, f[i][1] indicates the number of closing parentheses currently required to match, f[i][2] Indicates whether there is currently a decimal point, 1 for have, 0 for no, f[i][3] for multiplication 1, ordinary number 2, opening parenthesis 3, right parenthesis 4, decimal point 5, minus 6, inaction 0*/for (int i=0;i<100;i++) {j=0;j<10;j++ i][j]=0;}}} public class Keyboard implements keylistener{//keyboard event handling public void keytyped (KeyEvent e) {String s=textfield1.gettext (); if (E.getkeychar () = = ' * ' | | E.getkeychar () = = '/' | | E.getkeychar () = = ' + ') {if (ct>0&&f[ct-1][0]==1&&f[ct-1][3]!=1&&f[ct-1][3]!=5&& f[ct-1][3]!=3) {if (E.getkeychar () = = ' + ') textfield1.settext (s+ ' + '); else if (e.getkeychar () = = ' * ') teXtfield1.settext (s+ ' * '), else if (e.getkeychar () = = '/') Textfield1.settext (s+ '/'); F[ct][0]=1; F[CT][1]=F[CT-1][1]; f[ct][3]=1;ct++;}} else if (e.getkeychar () = = ' = ') {if (ct>0&&f[ct-1][0]==1&&f[ct-1][1]==0&&f[ct-1][3]!=1 &&f[ct-1][3]!=3&&f[ct-1][3]!=5&&f[ct-1][3]!=6) {Textfield1.settext (CCLT (S));}} else {if (E.getkeychar () = = '-') {if (ct>0&&f[ct-1][0]==1&&f[ct-1][3]!=1&&f[ct-1][3]!=5) { Textfield1.settext (s+ '-'); F[ct][0]=1; F[CT][1]=F[CT-1][1]; f[ct][3]=6;ct++;} if (ct==0) {textfield1.settext (s+ '-'); F[ct][0]=1; f[ct][3]=6;ct++;}} else if (e.getkeychar () = = ' (') {if ((ct>0&&f[ct-1][3]!=5&&f[ct-1][3]!=4&&f[ct-1][3]!=2) | | | ct==0) {textfield1.settext (' s+ '); F[ct][0]=1;if (ct==0) {f[ct][1]=1;} if (ct>0) {f[ct][1]=f[ct-1][1]+1;} f[ct][3]=3;ct++;}} else if (e.getkeychar () = = ') ') {if (ct>0&& (f[ct-1][3]==2| | f[ct-1][3]==4) &&f[ct-1][1]>0) {Textfield1.settext (s+ ') '); F[ct][0]=1; F[ct][1]=f[ct-1][1]-1; f[ct][3]=4;ct++;}}else if (e.getkeychar () = = '. ') {if (ct>0&&f[ct-1][2]==0&&f[ct-1][3]==2) {textfield1.settext (s+ '. '); F[ct][0]=1; F[CT][1]=F[CT-1][1]; F[ct][2]=1; f[ct][3]=5;ct++;}} else if (E.getkeychar () >= ' 0 ' &&e.getkeychar () <= ' 9 ') {if (ct==0) {Textfield1.settext (S+e.getkeychar ()); F[ct][0]=1; f[ct][3]=2;ct++;} else if (f[ct-1][3]!=4) {Textfield1.settext (S+e.getkeychar ()); F[ct][0]=1; F[CT][1]=F[CT-1][1]; F[CT][2]=F[CT-1][2]; f[ct][3]=2;ct++;}}}} public void keypressed (KeyEvent e) {String s=textfield1.gettext (); if (E.getkeycode () ==keyevent.vk_c) { Textfield1.settext (""); Ct=0;memsetf ();} else if (E.getkeycode () ==keyevent.vk_back_space) {if (ct>0) {Ct--;textfield1.settext (s.substring (0,S.length ()-1) ); f[ct][0]=0; f[ct][1]=0; f[ct][2]=0; f[ct][3]=0;}} else if (E.getkeycode () ==keyevent.vk_enter) {if (ct>0&&f[ct-1][0]==1&&f[ct-1][1]==0&&f[ ct-1][3]!=1&&f[ct-1][3]!=3&&f[ct-1][3]!=5&&f[ct-1][3]!=6) {TextField1.setText (CCLT (S));}}} public void keyreleased (KeyEvent E{}}private class Buttonjudge implements actionlistener{//button event handling public void actionperformed (ActionEvent event) {String S=textfield1.gettext (); String Curs=event.getactioncommand (), if (curs== "C") {Textfield1.settext (""); Ct=0;memsetf ();} else if (curs== "Backspace") {if (ct>0) {Ct--;textfield1.settext (s.substring (0,s.length ()-1)); f[ct][0]=0; f[ct][1]=0; f[ct][2]=0; f[ct][3]=0;}} else if (curs== "=") {if (ct>0&&f[ct-1][0]==1&&f[ct-1][1]==0&&f[ct-1][3]!=1&&f[ ct-1][3]!=3&&f[ct-1][3]!=5&&f[ct-1][3]!=6) {Textfield1.settext (CCLT (S));}} else {if (curs== "+" | | curs== "*" | | curs== "/") {if (ct>0&&f[ct-1][0]==1&&f[ct-1][3]!=1&&f[ct-1][3]!=5&&f[ct-1][3] !=3) {textfield1.settext (s+curs); F[ct][0]=1; F[CT][1]=F[CT-1][1]; f[ct][3]=1;ct++;}} else if (curs== "-") {if (ct>0&&f[ct-1][0]==1&&f[ct-1][3]!=1&&f[ct-1][3]!=5) { Textfield1.settext (s+curs); F[ct][0]=1; F[CT][1]=F[CT-1][1]; f[ct][3]=6;ct++;} if (ct==0) {Textfield1.settext (s+cURS); F[ct][0]=1; f[ct][3]=6;ct++;}} else if (curs== ") {if ((ct>0&&f[ct-1][3]!=5&&f[ct-1][3]!=4&&f[ct-1][3]!=2) | | | ct==0) {textfield1.settext (s+curs); F[ct][0]=1;if (ct==0) {f[ct][1]=1;} if (ct>0) {f[ct][1]=f[ct-1][1]+1;} f[ct][3]=3;ct++;}} else if (curs== ")") {if (ct>0&& (f[ct-1][3]==2| | f[ct-1][3]==4) &&f[ct-1][1]>0) {textfield1.settext (s+curs); F[ct][0]=1; F[ct][1]=f[ct-1][1]-1; f[ct][3]=4;ct++;}} else if (curs== ".") {if (ct>0&&f[ct-1][2]==0&&f[ct-1][3]==2) {textfield1.settext (s+curs); F[ct][0]=1; F[CT][1]=F[CT-1][1]; F[ct][2]=1; f[ct][3]=5;ct++;}} else {if (ct==0) {textfield1.settext (s+curs); F[ct][0]=1; f[ct][3]=2;ct++;} else if (f[ct-1][3]!=4) {textfield1.settext (s+curs); F[ct][0]=1; F[CT][1]=F[CT-1][1]; F[CT][2]=F[CT-1][2]; f[ct][3]=2;ct++;}}}} public static double change_to_double (String s) {////int isfushu=0;//if (s.charat (0) = = ' # ' | | S.charat (0) = = '-') {//isfushu=1;//s=s.substring (1, s.length ());//}//double ans=0;//int point=0,findedp=0;//for (int i =0;i<S.length (); i++) {//if (S.charat (i) = = '. ') {//findedp=1;//point=i;//break;//}//}//if (findedp==0) {//double fk=1;//for (int i=0;i<s.length (); i++) {//ans+= ( S.charat (S.length ()-i-1)-' 0 ') *fk;//fk*=10;//}//}//else if (findedp==1) {//int fk=1;//for (int i=0;i<point;i++) {// ans+= (S.charat (point-i-1)-' 0 ') *fk;//fk*=10;//}//fk=1;////for (int i=point+1;i<s.length (); i++) {//ans+= ( S.charat (i)-' 0 ') *k[fk];//fk++;//if (fk>6) break;//}//}//if (isfushu==0) return Ans;//else if (isfushu==1) return ans * ( -1);//return 0;//}////public static String change_to_string (double A) {////int isfushu=0;//if (a<0) isfushu=1;// String s= "";//int havep=0;//if (A-(int) (a)!=0) havep=1;//if (havep==0) {//system.out.println (a);//while (1==1) {//int b = (int) (A%10);//s=a[b-0]+s;//a/=10;//if (a==0) Break;//}//}//else if (havep==1) {//int c= (int) (a);// System.out.println (c);//while (1==1) {//int b= (int) (C%10);//s=a[b]+s;//system.out.println (s);//c/=10;//if (c==0) break;//}//s+= '. '; /system.out.println (s);//double d=a-(int) (a);//while (1==1) {//int x= (iNT) (D*10);//s=s+a[x];//system.out.println (s);//d*=10;//if (d==0| | S.length () >6) break;//}//}//if (isfushu==0) return s;//else return ' # ' +s;////}public string cclt (string s) {//Put "--" in exchange for " -0+" int qq=-1;for (int i=0;i<s.length () i++) {if (S.charat (i) = = '-' &&s.charat (i+1) = = '-') {qq=i;}} if (qq!=-1) {return cclt (s.substring (0, QQ) + " -0+" +s.substring (qq+2, S.length ()));} int flag=0;for (int i=0;i<s.length (); i++) {//judgment there is no parenthesis if (s.charat (i) = = ' (') {flag=1;break;}} int L=0,r=0,findedl=0,findedr=0;if (flag==1) {//parenthesized int kuohaonum[]=new int[100];for (int i=0;i<100;i++) kuohaonum[i ]=0;if (S.charat (0) = = ' (') {kuohaonum[0]=1;findedl=1;l=0;} for (int i=1;i<s.length (); i++) {if (findedl==1&&findedr==1) break;if (S.charat (i) = = ' (') {kuohaonum[i]= Kuohaonum[i-1]+1;if (kuohaonum[i]==1&&findedl==0) {l=i;findedl=1;}} else if (S.charat (i) = = ') ') {kuohaonum[i]=kuohaonum[i-1]-1;if (kuohaonum[i]==0&&findedr==0) {r=i;findedr=1;}} else kuohaonum[i]=kuohaonum[i-1];} String final=s.substring (0,l) +cclt (s.substring (l+1,R)); if (R+1>=s.length ()) {return cclt (Final);} else return CCLT (final+s.substring (R+1,s.length ()));} else if (flag==0) {//no parentheses int p=0,priority=0;for (int i=0;i<s.length (); i++) {if (S.charat (i) = = ' * ' | | S.charat (i) = = '/') {P=i;priority=1;break;}} if (priority==1) {//has multiplication when string n= ""; String ls= ""; String rs= ""; int havejian=0;//go left encounter minus int havenum=0;//right walk encounter number for (int i=p-1;i>=0;i--) {if (S.charat (i) = = '-' | | S.charat (i) = = '. ' | | (S.charat (i) >= ' 0 ' &&s.charat (i) <= ' 9 ')) {if (S.charat (i) = = '-') havejian=1;if (havejian==1&& ((S.charat (i) >= ' 0 ' &&s.charat (i) <= ' 9 '))) { break;} Ls=s.charat (i) +ls;} else break;} if (S.charat (p+1) = = '-') rs+= '-';//#代表相反数if (S.charat (p+1) >= ' 0 ' &&s.charat (p+1) <= ' 9 ') {Rs+=s.charat (p+ 1);} for (int i=p+2;i<s.length (); i++) {if ((S.charat (i) >= ' 0 ' &&s.charat (i) <= ' 9 ') | | S.charat (i) = = '. ') {Rs+=s.charat (i);} else break;} int len_of_l=ls.length (); int len_of_r=rs.length (); LS=CCLT (LS); RS=CCLT (RS);d ouble a,b;a=double.parsedouble (LS); b= Double.parsedouble (RS), if (S.CharAt (p) = = ' * ') {n=n+ (a*b);} else if (S.charat (p) = = '/') {n=n+ (A/b);} String final= ""; Final=s.substring (0, p-len_of_l) +n;if (P+len_of_r+1>s.length ()) {return cclt (Final);} Else{return cclt (s.substring (0, p-len_of_l) +n+s.substring (p+len_of_r+1, S.length ()));}} else if (priority==0) {//does not have multiplication priority p=0;if (S.charat (0) = = '-') {for (int i=1;i<s.length (); i++) {if (S.charat (i) = = '-' | | S.charat (i) = = ' + ') {P=i;break;}}} else if (s.charat (0) = '-') {for (int i=0;i<s.length (); i++) {if (S.charat (i) = = '-' | | S.charat (i) = = ' + ') {P=i;break;}}} if (p==0) {//out answer string finalans= ""; return finalans+double.parsedouble (s);} Calculates the plus and minus string n= ""; String ls= ""; String rs= ""; int havejian=0;//go left encounter minus int havenum=0;//right walk encounter number for (int i=p-1;i>=0;i--) {if (S.charat (i) = = '-' | | S.charat (i) = = '. ' | | (S.charat (i) >= ' 0 ' &&s.charat (i) <= ' 9 ')) {if (S.charat (i) = = '-') havejian=1;if (havejian==1&& ((S.charat (i) >= ' 0 ' &&s.charat (i) <= ' 9 '))) { break;} Ls=s.charat (i) +ls;} else break;} if (S.charat (p+1) = = '-') rs+= '-';//#代表相反数if (S.charat (p+1) >= ' 0 ' &&Amp;s.charat (p+1) <= ' 9 ') {Rs+=s.charat (p+1);} for (int i=p+2;i<s.length (); i++) {if ((S.charat (i) >= ' 0 ' &&s.charat (i) <= ' 9 ') | | S.charat (i) = = '. ') {Rs+=s.charat (i);} else break;} int len_of_l=ls.length (); int len_of_r=rs.length (); LS=CCLT (LS); RS=CCLT (RS);d ouble a,b;a=double.parsedouble (LS); b= Double.parsedouble (RS); if (S.charat (p) = = ' + ') {n=n+ (a+b);} else if (S.charat (p) = = '-') {n=n+ (A-B);} String final= ""; Final=s.substring (0, p-len_of_l) +n;if (P+len_of_r+1>s.length ()) {return cclt (Final);} Else{return cclt (s.substring (0, p-len_of_l) +n+s.substring (p+len_of_r+1, S.length ()));}}} return null;}}


Java calculator (with infinite parentheses) +string and double convert each other

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.