C 程序用于使用堆栈检查正确的HTML标签
C++ Program for Checking Correct HTML Tag Using Stack
我一直在尝试在数据结构教科书上遵循示例代码。它是一个程序,可以使用堆栈检查C 中的括号,但无法正常工作。不知何故,我对匹配算法的html标签有问题。
#include <iostream>
#include <stack>
#include <vector>
#include <string>
using namespace std;
vector<string> getHtmlTags() { //receives a string of html tags and divide them by "<, >"
vector<string> tags; //vector of html tags
while (cin) { //reads the whole input
string line;
getline (cin,line);
int pos = 0; //current scanning position
int ts = line.find("<",pos); //scans from the current scanning position
while (ts!=string::npos) { //repeat until end of string
int te = line.find(">", ts+1); //scans for the end of a tag (<)
tags.push_back(line.substr(ts,te-ts+1)); //save tag to the vector
pos = te + 1; //repositioning
ts = line.find("<",pos);
}
}
return tags; //return vector of tags
}
bool isHtmlMatched(const vector<string>& tags) { //checks if the html tags are correctly matched
stack<string> S; //implememted stack from above for opening tags
typedef vector<string>::const_iterator Iter; //iterate through vector
for (Iter p = tags.begin(); p != tags.end(); ++p) {
if (p->at(1) != '/') //is it the opening tag?
S.push(*p); //push to the stack
else{
if (S.empty()) return false; //there is nothing to match
string open = S.top().substr(1); //opening tag excluding '<'
string close = p->substr(2); //closing tag excluding '>'
if (open.compare(close) != 0) return false; //exception for fail to match
else S.pop(); //pop matched element
}
}
if (S.empty()) return true; //everything has matched correctly - Correct
else return false; //some did not match correctly - Incorrect
}
int main() {
int rep; //decides the number of trial
cin >> rep;
for (int i=1; i<=rep; i++) { //loop up to the decided trial
if(isHtmlMatched(getHtmlTags()))
cout << "Correct" << endl;
else cout << "Incorrect" << endl;
}
}
它在没有汇编错误的情况下运行,但是由于某种原因,控制台在我投掷HTML标签列表后不会响应。我想寻求帮助以改进这些代码。任何想法都将受到赞赏。
它在没有编译错误的情况下运行,但是由于某种原因,控制台在我投掷HTML标签列表后不会响应。
它为我运行,对于我尝试的几个测试用例,它显示出正确的输出。我使用的输入是:
1
<html>
<head></head>
<body></body>
</html>
您是否有机会在实际输入HTML标签之前忘记输入INT?如果没有,您可以显示输入吗?
相关文章:
- C 和 C++ 中开关语句的案例标签的常量值,但显示不同的行为
- 如何正确指定 goto 语句的标签?
- 简单的 HTML 验证器
- 如何在 html 页面中插入 cgi 应用程序的输出?
- 使用g++静态初始化带有命名标签的嵌套C++结构
- 通过水平滚动条更改标签
- 从 QFontDatabase 设置 QFont 将所有标签设置为等宽字体?Qt C++
- 如何跳过使用 xmlTextReader 关闭 HTML 标签
- 如何以C++格式输出已保存.html文件中<a href>标签中的所有链接?
- 从std :: String中删除HTML标签
- 如何在不获取所有html的情况下获取QTextEdit的html标签?
- C 程序用于使用堆栈检查正确的HTML标签
- 从文件中读取HTML标签并在终端中正确显示它们
- html 标签在 Qt 中不起作用
- 用于查找标签/属性的正则表达式在HTML代码中
- 使用Boost.从HTML中提取某些标签/属性
- 匹配字符串长度;查找和替换;在c++中删除HTML标签
- 如何在两个HTML标签之间获得特定的文本
- Qt:更改html标签的颜色不起作用
- 如何在QLabel中查看未知的HTML标签