Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:用java自带Stack实现的代码,请大家多多指教In Reply To:用java自带Stack实现的代码,请大家多多指教 Posted by:lg8t4bysq8 at 2006-11-11 20:03:32 我发现我和你的差不多,就是在处理输入输出上不一样,今天弄了一天的RE了 package com.whutjava; import java.io.BufferedInputStream; import java.util.Scanner; import java.util.Stack; /** * 浏览器对象类 * @author whut * */ public class Main { /** * @param args */ public static void main(String[] args){ String homePage = "http://www.acm.org/"; Main browse = new Main(homePage); Scanner cin = new Scanner(new BufferedInputStream(System.in)); while(true){ String command = ""; command = cin.next(); if(Main.COMMAND_VISIT.equals(command)){ command = command + " " + cin.next(); } browse.browse(command); if(browse.isClose()){ break; }else if(browse.isErrorCommand()){ System.out.println(Main.IGNORED); }else if(browse.isIgnored()){ System.out.println(Main.IGNORED); }else { System.out.println(browse.currentPage); } } } private String currentPage; public static final String COMMAND_BACK = "BACK"; public static final String COMMAND_FORWARD = "FORWARD"; public static final String COMMAND_VISIT = "VISIT"; public static final String COMMAND_QUIT = "QUIT"; public static final String IGNORED = "Ignored"; private boolean close = false; private boolean errorCommand = false; private boolean ignored = false; public boolean isIgnored() { return ignored; } public void setIgnored(boolean ignored) { this.ignored = ignored; } public boolean isErrorCommand() { return errorCommand; } public void setErrorCommand(boolean errorCommand) { this.errorCommand = errorCommand; } public boolean isClose() { return close; } public void setClose(boolean close) { this.close = close; } /** * URL 存放堆栈, for forward */ private Stack<String> forwardStack; /** * 浏览历史堆栈, for back */ private Stack<String> backStack; /** * 使用主页初始化的浏览器对象 * @param homeURL */ public Main(String homeURL) { super(); forwardStack = new Stack<String>(); backStack = new Stack<String>(); currentPage = homeURL; } /** * 浏览行为,输入命令,返回浏览器动作 * @param command:浏览命令 * @return */ public void browse(String command){ this.errorCommand = false; this.ignored = false; if(COMMAND_BACK.equals(command)){ //1,处理返回操作,查找浏览历史堆栈 if(this.backStack.empty()){ ignored = true; }else{ //历史堆栈出栈, 当前页进栈 this.forwardStack.push(this.currentPage); this.currentPage = this.backStack.pop(); } }else if(COMMAND_FORWARD.equals(command)){ //1,处理返回操作,查找浏览urlStack if(this.forwardStack.empty()){ ignored = true; }else{ //urlStack堆栈出栈 this.backStack.push(this.currentPage); this.currentPage = this.forwardStack.pop(); } }else if(COMMAND_QUIT.equals(command)){ this.close = true; return; }else{ //处理VISIT 命令和 非法命令 if(command != null && command.startsWith(COMMAND_VISIT+" http://")) { String url = command.substring(COMMAND_VISIT.length()+1); if(url.length() > 70 || url.indexOf(" ") >= 0) { //按规定url地址过长,被忽略,或者包含有空格 errorCommand = true; return; } //当前页面进历史栈,前进栈清空 this.backStack.push(this.currentPage); this.currentPage = url; this.forwardStack.clear(); return; } //非法命令 errorCommand = true; return; } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator