Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一次AC的代码>用数组简单地模拟两个栈的操作

Posted by gfedcba at 2009-01-14 12:02:17 on Problem 1028
// 简单而有趣的入栈和出栈操作, 权且把这题当作娱乐
// 关键是理解和处理好 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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator