我正在为hw写一个程序。我们应该平衡这些符号
l { }'s, ( )'s, [ ]'s, " "'s, and /* */'s
并忽略字符串文字和注释块,但我不知道该怎么做。我的代码部分有效,但是当涉及{ }
它无法说明时。处理/* */
时也存在问题。我被困住了,不知道该走哪条路。
例如,给定:
public class Test {
public static final void main(String[ ) args) {
System.out.println("Hello.");
}
}
它打印了两个}
不匹配,因为{
不在}
之前。我们需要将符号推入堆栈并弹出它们进行比较。我们还需要编写自己的名为MyStack.java的堆栈方法
我在这里提供了主要内容:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class SymbolBalance {
public static void main(String[] args){
if(args.length>0){
try{
Scanner input = new Scanner(new File (args[0]));
MyStack<Character> ms = new MyStack<>();
String str;
char ch;
while(input.hasNext()){
str = input.next();
for(int i=0;i<str.length();i++){
ch = str.charAt(i);
if(ch == '{'||ch =='(' || ch=='[' ||ch=='"'||
(ch == '/'&&i<str.length() -1&&str.charAt(i+1)=='*'))
{
ms.push(ch);
}
else if (ch==')'){
if(ms.isEmpty()||ms.pop()!= '('){
System.out.println(") is mismatched");
}
}
else if(ch == ']'){
if(ms.isEmpty() || ms.pop() != '['){
System.out.println("] is mismatched");
}
}
else if(ch == '}'){
if(ms.isEmpty() || ms.pop() != '{'){
System.out.println("} is mismatched");
}
}
else if(ch == '*' && i<str.length()-1&&str.charAt(i+1) == '/'){
if(ms.isEmpty() || ms.pop() != '*'){
System.out.println("*/ is mismatched");
}
}
else if(ch == '"'){
if(ms.isEmpty() || ms.pop() != '"'){
System.out.println(" \"\" is mismateched");
}
}
}
}
input.close();
}
catch(FileNotFoundException e){
System.out.println("Cannot find file");
e.printStackTrace();
}
}
else{
System.out.println("No command line argument");
}
}
}
答案 0 :(得分:0)
您的计划正确率为99%。唯一存在逻辑谬误的部分是双引号的特殊情况。
双引号是一种特殊符号,在某种意义上它是表示报价开头和结尾的相同符号。列表中的其他符号具有不同的开始和结束符号。例如。一个支架。开始括号用&#39; [&#39;并且结束一个用&#39;]&#39;表示。但是对于双引号,"
可以表示引用的开头或结尾。
由于双引号是特殊符号,因此您需要以特殊方式处理它。当您将双引号符号推送或弹出到MyStack
堆栈时,您需要跟踪它是开始还是结束。可以有很多方法来跟踪它。我建议一个表示相同的布尔标志。每次推送或弹出双引号到堆栈时,都需要翻转标志。
以下是我的修订版程序的工作实例:
public static void main(String[] args){
if(args.length>0){
try{
Scanner input = new Scanner(new File (args[0]));
MyStack<Character> ms = new MyStack<>();
String str;
char ch;
boolean quoteStart = true; //Flag to indicate quote start or end
while(input.hasNext()){
str = input.next();
for(int i=0;i<str.length();i++){
ch = str.charAt(i);
//Remove the " from the following condition. Handle it later.
if(ch == '{'||ch =='(' || ch=='[' ||
(ch == '/'&&i<str.length() -1&&str.charAt(i+1)=='*'))
{
ms.push(ch);
}
else if (ch==')'){
if(ms.isEmpty()||ms.pop()!= '('){
System.out.println(") is mismatched");
}
}
else if(ch == ']'){
if(ms.isEmpty() || ms.pop() != '['){
System.out.println("] is mismatched");
}
}
else if(ch == '}'){
if(ms.isEmpty() || ms.pop() != '{'){
System.out.println("} is mismatched");
}
}
else if(ch == '*' && i<str.length()-1&&str.charAt(i+1) == '/'){
if(ms.isEmpty() || ms.pop() != '*'){
System.out.println("*/ is mismatched");
}
}
//Handle the quote here
else if(ch == '"'){
if(quoteStart == true) {
ms.push(ch);
quoteStart = false;
}
else {
if(ms.isEmpty() || ms.pop() != '"'){
System.out.println(" \"\" is mismateched");
}
quoteStart = true;
}
}
}
}
input.close();
}
catch(FileNotFoundException e){
System.out.println("Cannot find file");
e.printStackTrace();
}
}
else{
System.out.println("No command line argument");
}
}