package org.xzr.chapter3.book;
public class Exam3_2 {
public static void expIsCorrect(String[] exp,int n)throws Exception{
SeqStack mySeqStack=new SeqStack();
for(int i=0;i<n;i++){
if((exp[i].equals(new String("(")))||(exp[i].equals(new String("[")))||(exp[i].equals(new String("{")))){
mySeqStack.push(exp[i]);
}else if ((exp[i].equals(new String(")")))&&mySeqStack.notEmpty()&&mySeqStack.getTop().equals(new String("("))) {
mySeqStack.pop();
}else if ((exp[i].equals(new String(")")))&&mySeqStack.notEmpty()&&!mySeqStack.getTop().equals(new String("("))) {
System.out.println("左右括号匹配次序不正确!");
}else if ((exp[i].equals(new String("]")))&&mySeqStack.notEmpty()&&mySeqStack.getTop().equals(new String("["))) {
mySeqStack.pop();
}else if ((exp[i].equals(new String("]")))&&mySeqStack.notEmpty()&&!mySeqStack.getTop().equals(new String("["))) {
System.out.println("左右括号匹配次序不正确!");
}else if ((exp[i].equals(new String("}")))&&mySeqStack.notEmpty()&&mySeqStack.getTop().equals(new String("{"))) {
mySeqStack.pop();
}else if ((exp[i].equals(new String("}")))&&mySeqStack.notEmpty()&&!mySeqStack.getTop().equals(new String("{"))) {
System.out.println("左右括号匹配次序不正确!");
}else if((exp[i].equals(new String(")")))||(exp[i].equals(new String("]")))||(exp[i].equals(new String("}")))&&!mySeqStack.notEmpty()){
System.out.println("右括号多余左括号!");
return;
}
}
if(mySeqStack.notEmpty()){
System.out.println("左括号多于右括号!");
}else {
System.out.println("括号匹配正确!");
}
}
public static String[] strToSring(String str){
int n=str.length();
String []a;
a=new String[n];
for(int i=0;i<n;i++){
a[i]=str.substring(i,i+1);
}
return a;
}
public static void main(String [] args){
String string;
int n;
try {
string="(())abc{[}";
n=string.length();
String[] a;
a=strToSring(string);
expIsCorrect(a, n);
string="(()))abc{[]}";
n=string.length();
String[] b;
b=strToSring(string);
expIsCorrect(b, n);
string="(()()abc{[]}";
n=string.length();
String[] c;
c=strToSring(string);
expIsCorrect(c, n);
string="(())abc{[]}";
n=string.length();
String[] d;
d=strToSring(string);
expIsCorrect(d, n);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
}
}
数据结构-堆栈及其应用-Java代码实现
4星 · 超过85%的资源 需积分: 16 32 浏览量
2011-03-20
20:55:26
上传
评论
收藏 22KB RAR 举报
心安即是归处1
- 粉丝: 4
- 资源: 12
最新资源
- React项目基于JavaScript实现的全球新闻发布管理系统源码.zip
- 基于keras+fasterRCNN,在VOC格式的口罩数据集上训练,检测人群中有无戴口罩python源码+模型
- 基于opencv+qt5机器视觉的传统缺陷检测, 即采用标准图片和待测图片进行pixel to pixel的XOR操作源码+文档
- 管道内检测缺陷数据库管理系统源码+文档说明+sln
- 毕业设计-低功耗STM32F411开发板(原理图+PCB源文件+官方例程+驱动等)源码+文档说明+截图
- 基于yolov5-tensorRT检测+发动机缸体内壁缺陷检测系统源码+文档说明
- 基于C++实现的锂电池缺陷检测源码+文档说明
- push_version
- 软件自制图像批量压缩工具
- 经典缺陷检测算法源码整理包含PaDiM(2020ICPR)、PatchCore(2022CVPR)、SimpleNet+文档说明
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈