验证HTML 是否是合法的HTML 运行在ubuntu下面eclipse的c++工程

  能判断标签的对称(栈) 以及嵌套关系(树)


HtmlDoc.h


#ifndef HTMLDOC_H_
#define HTMLDOC_H_
#include<iostream>
#include<string>
#include"list.h"
#include"stack.h"
#include<stdio.h>
#include<stdlib.h>
using namespace std;

struct TreeNode{
 string nodeName;//node name
 List<TreeNode*> *childList;//child list node
};

class HtmlDocument {
private:
	TreeNode *treeRoot;//html节点  对象
	Stack<TreeNode*>* stack;
	bool checkAndPushStack(string headNodeName);///遍历树看是否合法如果合法放进栈里
	bool checkStackTop(string tailNodeName);///和栈顶元素比较如果相等就移除栈顶元素,否则返回false
	TreeNode * traverseTree(TreeNode* treeNode, string nodeName);//遍历整个树结构
public:
	HtmlDocument();
	~HtmlDocument();
	void release(TreeNode*);
	bool isLegal(string path);
};

#endif /* HTMLDOC_H_ */
HtmlDoc.cpp


#include "HtmlDoc.h"

HtmlDocument::HtmlDocument(){

	this->stack = new Stack<TreeNode*>();//建一个存放树节点的栈
    //---------以下代码是够造一个树结构 目地--------------------//
	TreeNode *html = new TreeNode();
	this->treeRoot = html;

	html->nodeName = "html";
	List<TreeNode*> *htmlChildList= new List<TreeNode*>();
	html->childList = htmlChildList;

	TreeNode *head = new TreeNode();
	head->nodeName = "head";
	List<TreeNode*> *headChildList= new List<TreeNode*>();
	head->childList = headChildList;
	htmlChildList->add(head);


	TreeNode *body = new TreeNode();
	body->nodeName = "body";
	List<TreeNode*> *bodyChildList= new List<TreeNode*>();
	body->childList = bodyChildList;
	htmlChildList->add(body);


	TreeNode *title = new TreeNode();
	title->nodeName = "title";
	List<TreeNode*> *titleChildList= new List<TreeNode*>();
	title->childList = titleChildList;
	headChildList->add(title);

	TreeNode *h1 = new TreeNode();
	h1->nodeName = "h1";
	h1->childList = NULL;
	bodyChildList->add(h1);

	TreeNode *p = new TreeNode();
	p->nodeName = "p";
	p->childList = NULL;
	bodyChildList->add(p);

	TreeNode *center = new TreeNode();
	center->nodeName = "center";
	List<TreeNode*> *centerChildList= new List<TreeNode*>();
	center->childList = centerChildList;
	bodyChildList->add(center);//left

	TreeNode *left = new TreeNode();
	left->nodeName = "left";
	left->childList = NULL;
	bodyChildList->add(left);

	TreeNode *form = new TreeNode();
	form->nodeName = "form";
	form->childList = new List<TreeNode*>();
	bodyChildList->add(form);//left

	TreeNode *table = new TreeNode();
	table->nodeName = "table";
	List<TreeNode*> *tableChildList= new List<TreeNode*>();
	table->childList = tableChildList;
	bodyChildList->add(table);//left

	TreeNode *tr = new TreeNode();
	tr->nodeName = "tr";
	List<TreeNode*> *trChildList= new List<TreeNode*>();
	tr->childList = trCh
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值