void expr(){ cout << "expr -> term rest5" << endl; term(); rest5(); } void rest5(){ if(word.code == 41){ cout << "rest5 -> + term rest5" <<endl; word = scan(); term(); rest5(); } else if(word.code == 42){ cout << "rest5 -> - term rest5" <<endl; word = scan(); term(); rest5(); } else{ cout << "rest5 -> eplision" <<endl; } } void term(){ cout << "term -> unary rest6" << endl; unary(); rest6(); } void rest6(){ if(word.code == 43){ cout << "rest6 -> * unary rest6" <<endl; word = scan(); unary(); rest6(); } else if(word.code == 44){ cout << "rest6 -> / unary rest6" <<endl; word = scan(); unary(); rest6(); } else{ cout << "rest6 -> eplision" <<endl; } } void unary(){ cout << "unary -> factor" << endl; factor(); } void factor(){ cout << "factor -> num" << endl; word = scan(); }给我改一下,变下变量名,优化一下结构
时间: 2024-04-28 21:25:53 浏览: 103
姓名-软件工程技术B2303-数据结构(C++)实验三.doc
void parseExpression(){
cout << "parseExpression -> parseTerm parseRest5" << endl;
parseTerm();
parseRest5();
}
void parseRest5(){
if(word.code == 41){
cout << "parseRest5 -> + parseTerm parseRest5" <<endl;
word = scan();
parseTerm();
parseRest5();
}
else if(word.code == 42){
cout << "parseRest5 -> - parseTerm parseRest5" <<endl;
word = scan();
parseTerm();
parseRest5();
}
else{
cout << "parseRest5 -> eplision" <<endl;
}
}
void parseTerm(){
cout << "parseTerm -> parseUnary parseRest6" << endl;
parseUnary();
parseRest6();
}
void parseRest6(){
if(word.code == 43){
cout << "parseRest6 -> * parseUnary parseRest6" <<endl;
word = scan();
parseUnary();
parseRest6();
}
else if(word.code == 44){
cout << "parseRest6 -> / parseUnary parseRest6" <<endl;
word = scan();
parseUnary();
parseRest6();
}
else{
cout << "parseRest6 -> eplision" <<endl;
}
}
void parseUnary(){
cout << "parseUnary -> parseFactor" << endl;
parseFactor();
}
void parseFactor(){
cout << "parseFactor -> parseNum" << endl;
word = scan();
}
在优化结构方面,可以考虑使用更加简洁的函数命名,避免过度冗长,同时可以考虑将一些公共代码提取出来,封装成可复用的函数,提高代码的可读性和可维护性。此外,还可以使用更加规范的缩进格式和注释,使代码更加易于理解。
阅读全文