欢迎来到天天文库
浏览记录
ID:50826804
大小:45.45 KB
页数:5页
时间:2020-03-15
《JAVA利用堆栈实现括号匹配算法.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、importjava.util.*;publicclassMain{/***@paramargs*/publicstaticvoidmain(String[]args){//class1obj=newclass1();//class2obj1=newclass2();//obj.Print(300);//System.out.println();//obj1.Print1();Scannerscan=newScanner(System.in);Stringstr;System.out.println("请输入括号:");st
2、r=scan.next();intl=str.length();class3obj2=newclass3();obj2.Judge(str,l);}publicclassclass1{publicbooleansushu(intn){intnum=(int)Math.sqrt((double)n);for(inti=2;i<=num;i++){if(n%i==0)returnfalse;}returntrue;}publicvoidPrint(intn){intcount=0;for(inti=2;i<=n;i++)if(s
3、ushu(i)){if(count%6==0)System.out.println();System.out.print(i+"");count++;}}}publicclassclass2{publicvoidPrint1(){System.out.print(1+"+");intcounter=0;doublesum=0;for(doublei=1;1/(2*i-1)>0.00001;i++){if(counter%10==0)System.out.println();System.out.print("1/"+(2*i
4、-1)+"+");counter++;sum+=1/(2*i-1);}System.out.println();System.out.print("结果是:"+sum);}}publicclassclass3{privatecharStack[]=newchar[30];privateinttop;publicclass3(){this.top=-1;}publicbooleanIsEmpty(){returntop==-1?true:false;}publicbooleanIsFull(){returntop==19?tr
5、ue:false;}publiccharGetTop(){returnStack[top];}publiccharPop(){returnStack[top--];}publicvoidPush(charch){Stack[++top]=ch;}publicbooleanPipei(charch1,charch2){if((ch1=='('&&ch2==')')
6、
7、(ch1=='['&&ch2==']')
8、
9、(ch1=='{'&&ch2=='}'))returntrue;returnfalse;}publicvoidJudg
10、e(Stringstr,intlength){for(inti=0;i11、))System.out.println("括号匹配!");elseSystem.out.println("左右括号不匹配!");}}
11、))System.out.println("括号匹配!");elseSystem.out.println("左右括号不匹配!");}}
此文档下载收益归作者所有