模拟浏览器操作程序(C语言实现)

 问题描述

标准Web浏览器具有在最近访问的网页间后退和前进的功能。实现这些功能的一个方法是:使用两个栈,追踪可以后退和前进而能够到达的网页。在本题中,要求模拟实现这一功能。

需要支持以下指令:

BACK:将当前页推到“前进栈”的顶部。取出“后退栈”中顶端的页面,使它成为当前页。若“后退栈”是空的,忽略该命令。

FORWARD:将当前页推到“后退栈”的顶部。取出“前进栈”中顶部的页面,使它成为当前页。如果“前进栈”是空的,忽略该命令。

VSIT <url>:将当前页推到“后退栈”的顶部。使URL特指当前页。清空“前进栈”。

QUIT:退出浏览器。

假设浏览器首先加载的网页URL是:htto./ / www. acm. org/

源代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct stack
{
	char s[100][70];
	int top;
}ST;//栈定义
typedef struct queue
{
	char q[100][70];
	int head;
	int tail;
}QU;//队列定义,用于接受输出
void PUSHQ(QU *qu, char cur[])//入队
{
	if (qu->tail == 100)
	{
		printf("队满\n");
	}
	else 
	{
	   qu->tail++;
	   strcpy_s(qu->q[qu->tail], strlen(cur) + 1, cur);
	}
}
void POPQ(QU *qu)//出队
{
	qu->head++;
}
void PUSH(ST *st, char cur[])//入栈
{
	if (st->top == 100)
	{
		printf("栈满\n");
	}
	else
	{
		strcpy_s(st->s[st->top],strlen(cur)+1, cur);
		st->top++;
	}
}
void POP(ST *st)//出栈
{
	if (st->top == 0)
	{
		printf("栈空\n");
	}
	else
	{
		st->top--;
	}
}
int EMPTY(ST *st)//判空
{
	if (st->top == 0)
		return 1;
	else
		return 0;
}
void BACK(ST *stfront,ST *strear,char cur[], QU *qu)//回溯
{
	if (!EMPTY(strear))
	{ 
		PUSH(stfront, cur);
		POP(strear);
		strcpy_s(cur, strlen(strear->s[strear->top]) + 1, strear->s[strear->top]);
		PUSHQ(qu, cur);
	}
	else
	{
		char *a = "Ignored";
		PUSHQ(qu, a);
	}
}
void FORWARD(ST *stfront, ST *strear, char cur[], QU *qu)//前寻
{
	if (!EMPTY(stfront))
	{
		PUSH(strear, cur);
		POP(stfront);
		strcpy_s(cur, strlen(stfront->s[stfront->top]) + 1, stfront->s[stfront->top]);
		PUSHQ(qu, cur);
	}
	else
	{
		char *a = "Ignored";
		PUSHQ(qu,a);
	}
}
void VISIT(ST *stfront,ST *strear, char cur[],QU *qu)//访问
{
	PUSHQ(qu, cur);
	stfront->top = 0;
}

int main()
{
	printf("输入:\n");
	ST stfront;
	ST strear;
	stfront.top = 0;
	strear.top = 0;
	QU qu;
	qu.head = -1;
	qu.tail = -1;
	char cur[70];
	char start[70] = "http://www.acm.org/";//需先对此网站进行如回溯栈的特殊处理
	char in[2][70];
	strcpy_s(cur, strlen(start) + 1, start);
	scanf_s("%s", in[0], 70);
	while (strcmp(in[0], "QUIT") != 0)
	{
		if (strcmp(in[0], "VISIT") == 0)
		{
			PUSH(&strear, cur);
			scanf_s("%s", in[1], 70);
			strcpy_s(cur,strlen(in[1])+1,in[1]);
			VISIT(&stfront, &strear, cur, &qu);
		}
		else if (strcmp(in[0], "QUIT") == 0)
		{
			break;
		}
		else if (strcmp(in[0], "BACK") == 0)
		{
			BACK(&stfront, &strear, cur, &qu);
		}
		else
		{
			FORWARD(&stfront, &strear, cur, &qu);
		}
		scanf_s("%s", in[0], 70);
	}
	printf("输出:\n");//输出
	while (qu.head != qu.tail)
	{
		POPQ(&qu);
		printf("%s\n", qu.q[qu.head]);
	}
	
}

测试结果:

 

  • 13
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值