100分求下面问题怎么做?

鲁虾 2006-12-01 08:27:11
文本文件a.htm有如下数据
<OL>
<LI>This is item 1.
<OL>
<LI>Item 1 of second list
<LI>Item 2 of second list
<OL>
<LI>Item 1 of third list
</OL>
<LI>Item 3 of second list
</OL>
Line after
要求用一个类,显示出,用控制台程序即可
The rendered output should be:
Line before
1. This is item 1.
1. Item 1 of second list
2. Item 2 of second list
1. Item 1 of third list
3. Item 3 of second list
Line after
也就是说有三个标识符,<ol></ol><li>其对格式作用,大家看看怎么做
要有有如下方法
1、读入文件
2、统计出从文件中读出的这段文本的个数
3、按照上边的格式把数据显示出来!
...全文
668 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
gongchenwei 2007-03-02
  • 打赏
  • 举报
回复
上面有些多余的东西
gongchenwei 2007-03-02
  • 打赏
  • 举报
回复
#include <iostream>
#include <vector>
#include <fstream>
#include <istream>
#include <string>
#include <cstdlib>
#include <algorithm>

using namespace std;

class IsNum
{
public:
bool operator()(char c)
{
if(c>= 48 && c <= 57)
return false;
return true;
}
};


int main()
{
int count_ol;
int number;
string line;
string prefix;
string suffix;
vector<string> tmp;

ifstream openf("test.txt");
if(openf.fail())cerr << "error in openning file!"<<endl;

cout<<"Line before"<<endl;

while ( getline ( openf, line ) )
{
prefix = line.substr(0,4);
suffix = line.substr(4);
if ( prefix == "<LI>" )
{
count_ol++;
tmp.push_back(line);
line.erase(remove_if(line.begin(),line.end(),IsNum()),line.end());
cout << line << ".";
cout << suffix << endl;
}//if
else if ( prefix == "<OL>" )
{
count_ol++;
continue;
}//else if
else if( prefix == "</OL" )
{
count_ol--;
continue;
}//else
}//while
cout << "Line after!" << endl;
return 0;

}
listart 2006-12-03
  • 打赏
  • 举报
回复
RE:嵌套不能太多呵, 严重影响效率! 建议:

#define MAX 20
该定义 限制 OL 欠套不得超过 20 层 ...

不存在嵌套问题,不需要嵌套,直接按字节写入目标文件即可
listart 2006-12-02
  • 打赏
  • 举报
回复
如确实需要原码再联系我
listart 2006-12-02
  • 打赏
  • 举报
回复
没有这么复杂,还可以简单点,不用限制层次的
思路很简单:
一个变量,假设int nIndent(0);用来记录缩进次数,那么一个流读文件,当读到'<'后欲读后面若干字符,
如果满足<OL>,则nIndent++
如果满足</OL>,则nIndent--
如果满足<LI>,往显示流中写入nIndent个'\t'
其他直接写入显示流
至于中间出现nIndent<0 或者最后nIndent!=0还有其他一些情况则可以判断出原文件格式不正确
shellyyee 2006-12-02
  • 打赏
  • 举报
回复
有分我就顶
sclzmbie 2006-12-01
  • 打赏
  • 举报
回复
当然要建个树了,怎么能随便限制嵌套层数呢????
鲁虾 2006-12-01
  • 打赏
  • 举报
回复
gongchenwei 2006-12-01
  • 打赏
  • 举报
回复
头文件加多一个
#include <fstream>
aniude 2006-12-01
  • 打赏
  • 举报
回复
头文件加多一个
#include <iostream>
鲁虾 2006-12-01
  • 打赏
  • 举报
回复
五个错误,我用visul2005编译的,

错误 1 error C2079: 'openf' uses undefined class 'std::basic_ifstream<_Elem,_Traits>' c:\documents and settings\administrator\my documents\visual studio 2005\projects\aa\ass.cpp 10
错误 2 error C2440: 'initializing' : cannot convert from 'const char [12]' to 'int' c:\documents and settings\administrator\my documents\visual studio 2005\projects\aa\ass.cpp 10
错误 3 error C2228: left of '.fail' must have class/struct/union c:\documents and settings\administrator\my documents\visual studio 2005\projects\aa\ass.cpp 11
错误 4 error C2228: left of '.eof' must have class/struct/union c:\documents and settings\administrator\my documents\visual studio 2005\projects\aa\ass.cpp 16
错误 5 fatal error C1903: unable to recover from previous error(s); stopping compilation c:\documents and settings\administrator\my documents\visual studio 2005\projects\aa\ass.cpp 16
Jim_King_2000 2006-12-01
  • 打赏
  • 举报
回复
数据结构如下:
typedef struct tag_STRING_LIST
{
int nLayer;
std::string strContent;
} , *PSTRING_LIST;
std::vector<STRING_LIST> string_list_vector;

算法如下:
变量x表示层次数,初始值为0。分析时字母不分大小写。
1、读入文件一行,若遇到EOF,则退出程序。
2、取出<xx>中的xx,若格式不为<xx>则显示错误,然后退出程序。
3、若xx为OL,则x++;若xx为/OL,则x--,若x小于0,就显示错误退出程序;若xx为LI,则读出后面的字符串,并与当前的x一起,存入vector;若xx值为其它,则显示错误退出。
4、跳到步骤1。
jixingzhong 2006-12-01
  • 打赏
  • 举报
回复
ifstream openf("test.txt");
==》
ifstream openf("a.htm");

我是把文件内容 贴在 test.txt,
改成 a.htm 就可以了,
这个问题不大。
jixingzhong 2006-12-01
  • 打赏
  • 举报
回复
#define MAX 20
该定义 限制 OL 欠套不得超过 20 层 ...

另外,
程序处理要求:
1、<OL> 和 </OL> 是独立一行的,该标记后面没有 有效字符串
2、<LI> 后面紧跟需要处理的字符串
jixingzhong 2006-12-01
  • 打赏
  • 举报
回复
#include <string>
#include <cstdlib>
#include <iostream>

#define MAX 20
using namespace std;

int main()
{
ifstream openf("test.txt");
if(openf.fail())cerr << "error in openning file!"<<endl;

string line, prefix, suffix;
int ol=0, li[MAX]={0};
cout<<"Line before"<<endl;
while(!openf.eof())
{
getline(openf, line);
prefix = line.substr(0, 4);
if(prefix == "<OL>"){ol++; continue;}
if(prefix == "</OL"){ol--; continue;}
if(prefix == "<LI>")
{
li[ol-1]++;
suffix = line.substr(4);

int i;
for(i=0; i<ol; i++)
cout<<"\t";
cout<<li[ol-1]<<". "<<suffix<<endl;
}
}
cout<<"Line after"<<endl;
openf.close();
system("pause");
return 0;
}
鲁虾 2006-12-01
  • 打赏
  • 举报
回复
逻辑的我来写,偶就是不会读入,其他的慢慢的可以写出来
shawnwan 2006-12-01
  • 打赏
  • 举报
回复
期待高手出现
weiym 2006-12-01
  • 打赏
  • 举报
回复
stack
jixingzhong 2006-12-01
  • 打赏
  • 举报
回复
#include <fstream>

没有贴全 ...

64,651

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧