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 |
一次AC的代码>用数组简单地模拟两个栈的操作// 简单而有趣的入栈和出栈操作, 权且把这题当作娱乐 // 关键是理解和处理好 command和URL,把URL当成两个栈间的一个中介,其它的只是简单的模拟 // 一次AC #include <iostream> using namespace std; const int N = 100; const int LEN = 80; int main() { char forward[N][LEN]; char backward[N][LEN]; int forTop = -1; // 空栈 int backTop = -1; char command[LEN]; // 命令 char URL[LEN] = "http://www.acm.org/"; // 网址 while (cin>>command) { if( strcmp(command,"VISIT") == 0 ) { strcpy(backward[++backTop], URL); //入栈 forTop = -1; cin>>URL; cout<<URL<<endl; } else if (strcmp(command,"BACK") == 0) { if (backTop >=0) { strcpy(forward[++forTop], URL);// 入栈 strcpy(URL, backward[backTop--]);// 出栈 cout<<URL<<endl; } else { cout<<"Ignored"<<endl; } } else if (strcmp(command,"FORWARD") == 0) { if (forTop >=0) { strcpy(backward[++backTop], URL); // 入栈 strcpy(URL , forward[forTop--]); // 出栈 cout<<URL<<endl; } else { cout<<"Ignored"<<endl; } } else if (strcmp(command,"QUIT") == 0) { break; } } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator