⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 新建 文本文档.txt

📁 这个程序用于判断平衡符号
💻 TXT
字号:

package s;
import java.io.*;

public class test
{
static int a, b;//a计算左括号的总个数b 计算右括号的总个数
static int yuansupoped;//计算弹出的元素的个数。
boolean yesnopan=true;//判断是不调用pan这个方法
int linenum = 0;//计算文章总的行数
stack s = new stack();//生成栈的实例
public static void main(String[] arg) {
test tt = new test();

File f = new File("F:\\io.txt");
tt.ispingheng(f);

}


//方法 pipei 判断是否披配
public boolean pipei(char j, char ss) {

if (j == '(' && ss == ')') {
return true;
}
else if (j == '{' && ss == '}') {
return true;
}

else if (j == '[' && ss == ']') {
return true;
}
return false;
}
//方法: pan 在读完文件后判断栈是否为空,如果为空,且a=b,说明文件成功,如果a<b,调用findkuohao从栈中找出曾经弹出的最晚的‘{‘,
并打印出在该’{‘在下一行需要’{’。如果栈不空,说明在括号多,同样调用findkuohao从栈中找出曾经弹出的最晚的‘{‘,说在该’{‘
下一行需要’}‘。

public void pan() {
boolean yesnoempty;//标识是否栈空
yesnoempty = s.isEmpty();//判断是否栈空
if (yesnoempty == false)
{
int p=s.findkuohao('{');//p代表‘{’所在的下一行
System.out.println("'}' expected in column : " +p);
}
if (yesnoempty == true) {
if (a == b) {
System.out.println("your program is successuful");
}
if (a < b) {
int q = s.findkuohao('{');//q代表‘{’所在的下一行
System.out.println("'{' expected in column : " + q);
}
}
}


public void ispingheng(File f)
{
boolean yesnopipei = true;//标识是否披配
int m = 0;
int n = 0;
test tt = new test();
try {
RandomAccessFile file = new RandomAccessFile(f, "r");
long filepointer = 0;

long length = file.length();
while (filepointer < length)
{
linenum++;//每读一行linenum加一。
String sg = file.readLine();//取得每行的字符串
filepointer = file.getFilePointer();//取得读文件时的当前指针
int g = sg.length();//取得该行的字符串长度
for (int x = 0; x < g; x++) {
char ch = sg.charAt(x);//逐个判断该行的每一个字符是否为左括号
if (ch == '{' || ch == '(' || ch == '[') {
a++;//a计算左括号的总个数
Node ne = new Node(ch,linenum);//生成Node 实例
s.push(ne);//放入栈中
}
if (ch == ')' || ch == '}' || ch == ']') {
boolean yesnoempty = s.isEmpty();
b++;//b 计算右括号的总个数
if (yesnoempty == false) {
Node cr = s.topAndPop();//取出栈中的顶层元素
yesnopipei = tt.pipei(cr.ch, ch);//判断是否披配
if (yesnopipei == true) {
s.pop();//弹出栈中顶层元素
yuansupoped++;//计算弹出的元素的个数。

}
else if (yesnopipei == false) {
if (cr.ch == '[' || cr.ch == '(' || ch == ']' || ch == ')') {
switch (ch) {
case ')': {
System.out.println("')'excepted in column : " + linenum);
yesnopan=false;//产生该情况后不在调用pan
break;//产生该情况后跳出switch 循环
}
case ']': {
System.out.println("']'excepted in column : " + linenum);
yesnopan=false;//产生该情况后不在调用pan
break;//产生该情况后跳出switch 循环
}

}
switch (cr.ch) {
case '(': {

System.out.println("')'excepted in column : " + cr.x);
yesnopan=false;//产生该情况后不在调用pan
break;//产生该情况后跳出switch 循环
}
case '[': {
System.out.println("']'excepted in column : " + cr.x);
yesnopan=false;//产生该情况后不在调用pan
break;//产生该情况后跳出switch 循环
}
}
break;//如果跳出switch 循环,则跳出for循环,不在读文件。
}

}
}
}
}
}
if(yesnopan==true)

{
pan();//只有在switch中的所有情况都不发生的情况下,才会调用pan
}
}
catch (Exception en) {
en.printStackTrace();
}
}
}
class stack
{
int b;
int guohaolinenum;//标识找到的‘{’所在的行数。
public stack() {
this(defaultcatacity);//生成容量为100的栈。
}

public stack(int capcity) {
thearray = new Node[capcity];//生成Node 数组(栈);
topofstack = -1;
}
class Underflow//定义异常类,当栈空要取元素时产生该异常
extends Exception
{
public Underflow()
{ 
topofstack = -1;
}
}

class Overflow//定义异常类,当栈满要向其中加元素时产生该异常
extends Exception {
public Overflow() {
topofstack = -1;
}
}
public boolean isEmpty() {
boolean bn = true;
if (topofstack == -1) {
bn = true;
}
if (topofstack != -1) {
bn = false;
}
return bn;
}

public boolean isFull() {
return topofstack == thearray.length - 1;//如果该式成立,则返回true,否则返回false;
}

public void makeempty() {
topofstack = -1;
}

public void push(Node x) throws Overflow {
if (isFull())
throw new Overflow();
topofstack++;
thearray[topofstack] = x;
}

public Node top() throws Underflow {
Node cc = new Node('q', 0);//自定义一个Node
if (isEmpty())
return cc;//如果栈空返回cc
return thearray[topofstack];//否则返回顶层元素。
}

public int findkuohao(char m) {//m代表要找的括号
for (int i = 0; i <test.yuansupoped; i++) {//test.yuansupoped代表从栈中曾弹出过的元素的个数,如超出这个范围找m,
会产生空指针异常,实际上栈中元素并没有删除,我们在pop方法中只是移动指针来改变当前topofstack所指向的元素,找m 时
当然从弹出的元素中找,所以要计算弹出的元素个数。
Node node = topAndPop();
if (node.ch == m) {
int n = node.x;//如果找到与m 相等的node.ch,表示出该node.ch所在的行数。
guohaolinenum = node.x+1;//guohaolinenum 表示node.ch所在的行的下一行。
break;//跳出for循环
}
topofstack++;//如果找到的不披配,指针topofstack加一,继续找,一直到i <test.yuansupoped不成立为止(其实是找遍曾弹出的元素)。
}
return guohaolinenum;//返回表示node.ch所在的行的下一行。
}


public void pop() throws Underflow {

if (isEmpty())
{

}
if(!isEmpty())
{
topofstack--;
}
}

public Node topAndPop() {
Node cc = new Node('m', 0);
Node ot = new Node('p', 0);//自定义ot
if (isEmpty())
return cc;
try {
Node topitem = top();//取得顶层元素。
ot = topitem;//把值传给ot
}
catch (Exception en) {
en.printStackTrace();
}

return ot;//返回顶层元素。
}

private Node[] thearray;
public static int topofstack;//topofstack作为一个指针,指向thearray数组中的元素
static int defaultcatacity = 100;//定义数组长度
}
class Node//定义Node 类
{
char ch;//存放‘[‘,’{‘,’(‘
int x;//存放’[‘,’{‘,’(‘在文件中所在的行数。
Node() {

}

Node(char ch, int x) {
this.ch = ch;//初始化ch
this.x = x;//初始化x
}
} 
备注:
<1> 由于,没有java语法分析,所以在那一个行需要‘{’,对行数定们不准确,由于没有在栈中保存在括号在文件中所在的行数,所以对于
在那一行需要‘}’定位得就更不准,对于‘(’‘)’‘[’‘]‘,如果出现不披配我视为在同行需要括号,因为()或[]的两部分
平常都在同一行,而且如果’[]或()不披配,将不在判断‘{’或‘}’。可以再定义一个类让它继承stack,让这个新类存放右括号及其
所在的行数,这样在那一行需要‘}’定位得会准一些,我不想做了。由于要存放Node 所以必须自定义栈。
<2>可以把stack类独立的放在别一个包中,把stack封装起来,不过方法findkuohao要做如下定义:
public int findkuohao(char m,int numyuansupoped) {//m代表要找的括号,numyuansupoped代表弹出的元素个数。
for (int i = 0; i <numyuansupoped; i++) {//test.yuansupoped代表从栈中曾弹出过的元素的个数,如超出这个范围找m,
会产生空指针异常,实际上栈中元素并没有删除,我们在pop方法中只是移动指针来改变当前topofstack所指向的元素,找m 时
当然从弹出的元素中找,所以要计算弹出的元素个数。
Node node = topAndPop();
if (node.ch == m) {
int n = node.x;//如果找到与m 相等的node.ch,表示出该node.ch所在的行数。
guohaolinenum = node.x+1;//guohaolinenum 表示node.ch所在的行的下一行。
break;//跳出for循环
}
topofstack++;//如果找到的不披配,指针topofstack加一,继续找,一直到i <test.yuansupoped不成立为止(其实是找遍曾弹出的元素)。
}
return guohaolinenum;//返回表示node.ch所在的行的下一行。
}
//test类调用该方法时,需要把弹出的元素个数也传给它。


















⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -