[英]Java pairs of symbols program
使用堆栈数据结构:如果输入文件不平衡,则将提供不平衡原因和文件内本地化详细信息。 出于灵活性的原因,请从文本文件中读取符号的平衡对。 通过考虑以下符号对测试程序:(),{},[],/ * * /
我在最后一个要求上遇到了麻烦:/ * * /
我似乎也无法掌握如何打印文件内本地化详细信息? 即错误发生在文本文件的哪一行?
文本文件如下所示:
(()(()
{}}}{}{
[[[]][][]
((}})){{]
()
[]
{}
[]{}
()()()[]
*/ /*
(a+b) = c
编码:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class P1 {
private boolean match = true;
// The stack
private java.util.Stack<Character> matchStack = new java.util.Stack<Character>();
// What to do with a match
public boolean ismatch() {
return match && matchStack.isEmpty();
}
// Finding a match
public void add(char c) {
Character k = leftSide(c);
if (k == null)
;
else if (k.charValue() == c)
matchStack.push(k);
else {
if (matchStack.isEmpty() || !matchStack.pop().equals(k))
match = false;
}
}
// Add string values
public void add(String s) {
for (int i = 0; i < s.length(); i++)
add(s.charAt(i));
}
// The various symbol pairs
protected static Character leftSide(char c) {
switch (c) {
case '(':
case ')':
return new Character('(');
case '[':
case ']':
return new Character('[');
case '{':
case '}':
return new Character('{');
default:
return null;
}
}
// Main method. Welcome message, read the test file, build the array, print
// results.
public static void main(String args[]) {
List<String[]> arrays = new ArrayList<String[]>();
// Welcome message
System.out
.println("Project #1\n"
+ "Welcome! The following program ensures both elements of various paired symbols are present.\n"
+ "Test Data will appear below: \n"
+ "-------------------------------");
// Read the file
try {
BufferedReader in = new BufferedReader(new FileReader(
"testfile.txt"));
String str;
// Keep reading while there is still more data
while ((str = in.readLine()) != null) {
// Line by line read & add to array
String arr[] = str.split(" ");
if (arr.length > 0)
arrays.add(arr);
// Let the user know the match status (i.e. print the results)
P1 mp = new P1();
mp.add(str);
System.out.print(mp.ismatch() ? "\nSuccessful Match:\n"
: "\nThis match is not complete:\n");
System.out.println(str);
}
in.close();
// Catch exceptions
} catch (FileNotFoundException e) {
System.out
.println("We're sorry, we are unable to find that file: \n"
+ e.getMessage());
} catch (IOException e) {
System.out
.println("We're sorry, we are unable to read that file: \n"
+ e.getMessage());
}
}
}
一种简单的实现方法是使用诸如Map<String, Stack<Location>>
类的Map<String, Stack<Location>>
,其中Location
是您创建的包含两个int
(一个行号和一个字符号)的类。 那可以是您的位置信息。 此映射的键( String
)将是您对中的左侧(开瓶器)部分。 每次有开瓶器时,您都会在地图中查找适当的Stack
,并在该开瓶器上推一个新的Location
。 每次遇到更近的物件时,您都会查看其开启器,使用开启器在地图中查找正确的Stack
,然后将其弹出一次。 我说使用String
作为密钥的原因是因为并非所有的开瓶器都可以用Character
即/*
开瓶器)表示,所以String
必须要做。 由于您无法为leftSide(char)
(现在leftSide(String)
)功能打开leftSide(String)
,因此必须使用if-else
或使用地图( Map<String, String>
)创建更接近于开启者映射。
当到达文件末尾时,应仅将未保留在Stack
对象中的Location
对象打开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.