【发布时间】:2014-05-20 20:43:14
【问题描述】:
这个程序应该转换如下:
\hyp76{a,1+a-b}b{xy}
到:
\HyperpFq{7}{6}@@{a,1+a-b}{b}{xy}.
如您所见,如果组的内容是一个字符,则没有括号。但是,当带括号的组之一位于行尾时,它会跳过该组并将其视为单个字符(不带括号)的情况。我怎样才能避免这种情况?谢谢。这是我的代码:
static int checkNestedBrackFront(String line, int pos){
int count=0;
for(int i=pos;i<line.length();i++ ){
if(line.charAt(i)=='{')
count++;
if(line.charAt(i)=='}'&&count==0)
return i;
if(line.charAt(i)=='{')
count--;
}
return 0;
}
line = new Scanner(new File("KLSadd.tex")).useDelimiter("\\Z").next();
PrintWriter writer = new PrintWriter("Converted.tex");
while(line.contains("\\hyp76")){
int posHyp = line.indexOf("\\hyp76");
String beforeHyp = line.substring(0,posHyp);
int start = posHyp+7;
String firstArgs = line.substring(start, line.indexOf("}", start));
if(line.charAt((line.indexOf("}", start)+1))!='{'&&!(line.substring(start, start+4).contains("\n"))){ //this is to check for single characters
secArgs = line.substring(line.indexOf("}", start) + 1,line.indexOf("}", start) + 2 );
posSec=line.indexOf("}", start)+1;
}
else {
int posBrack = line.indexOf("{", line.indexOf("}", start));
posSec = checkNestedBrackFront(line, posBrack+1);
secArgs = line.substring(posBrack+1, posSec);
}
if((line.charAt(posSec+1)!='{')&&!(line.substring(posSec,posSec+4).contains("\n"))){ //this is to check for single characters
System.out.println(line.charAt(posSec+1)+"hello");
thirdArgs= line.substring(posSec+1,posSec+2);
afterHyp=line.substring(posSec+2);
}
else{
int posThirdBrack = line.indexOf("{", posSec);
int posThird = checkNestedBrackFront(line, posThirdBrack+1);
thirdArgs = line.substring(posThirdBrack+1,posThird);
afterHyp = line.substring(posThird+1);
}
line=beforeHyp+"\\HyperpFq{7}{6}@@{"+firstArgs+"}{"+secArgs+"}{"+thirdArgs+"}"+afterHyp;
}
writer.print(line);
writer.close();
【问题讨论】:
-
非常混乱。试着澄清你的问题。什么是单字符大小写?什么是括号案例?这个返回 0 的无用函数 'checkNestedBrackFront' 是什么?