100分求下面问题怎么做?
文本文件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、按照上边的格式把数据显示出来!
问题点数:100、回复次数:19Top
1 楼weiym(磨刀霍霍向猪羊)回复于 2006-12-01 08:47:42 得分 1
stackTop
2 楼shawnwan()回复于 2006-12-01 09:23:32 得分 1
期待高手出现Top
3 楼qfkfw(空军一号)回复于 2006-12-01 09:31:07 得分 0
逻辑的我来写,偶就是不会读入,其他的慢慢的可以写出来Top
4 楼jixingzhong(瞌睡虫·星辰)回复于 2006-12-01 09:36:48 得分 80
#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;
} Top
5 楼jixingzhong(瞌睡虫·星辰)回复于 2006-12-01 09:38:32 得分 0
#define MAX 20
该定义 限制 OL 欠套不得超过 20 层 ...
另外,
程序处理要求:
1、<OL> 和 </OL> 是独立一行的,该标记后面没有 有效字符串
2、<LI> 后面紧跟需要处理的字符串Top
6 楼jixingzhong(瞌睡虫·星辰)回复于 2006-12-01 09:39:24 得分 0
ifstream openf("test.txt");
==》
ifstream openf("a.htm");
我是把文件内容 贴在 test.txt,
改成 a.htm 就可以了,
这个问题不大。Top
7 楼Jim_King_2000()回复于 2006-12-01 09:48:48 得分 12
数据结构如下:
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。Top
8 楼qfkfw(空军一号)回复于 2006-12-01 10:01:49 得分 0
五个错误,我用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
Top
9 楼aniude(重返荣耀)回复于 2006-12-01 10:42:01 得分 2
头文件加多一个
#include <iostream>Top
10 楼gongchenwei()回复于 2006-12-01 11:14:13 得分 2
头文件加多一个
#include <fstream>
Top
11 楼qfkfw(空军一号)回复于 2006-12-01 11:29:14 得分 0
恩Top
12 楼sclzmbie(忘我)回复于 2006-12-01 12:48:34 得分 2
当然要建个树了,怎么能随便限制嵌套层数呢????Top
13 楼jixingzhong(瞌睡虫·星辰)回复于 2006-12-01 19:28:29 得分 0
#include <fstream>
没有贴全 ...Top
14 楼shellyyee(☆☆☆☆☆-----疏狂一醉,生活大师)回复于 2006-12-02 09:26:33 得分 0
有分我就顶Top
15 楼listart(开始)回复于 2006-12-02 12:57:12 得分 0
没有这么复杂,还可以简单点,不用限制层次的
思路很简单:
一个变量,假设int nIndent(0);用来记录缩进次数,那么一个流读文件,当读到'<'后欲读后面若干字符,
如果满足<OL>,则nIndent++
如果满足</OL>,则nIndent--
如果满足<LI>,往显示流中写入nIndent个'\t'
其他直接写入显示流
至于中间出现nIndent<0 或者最后nIndent!=0还有其他一些情况则可以判断出原文件格式不正确Top
16 楼listart(开始)回复于 2006-12-02 12:58:09 得分 0
如确实需要原码再联系我
Top
17 楼listart(开始)回复于 2006-12-03 22:19:46 得分 0
RE:嵌套不能太多呵, 严重影响效率! 建议:
#define MAX 20
该定义 限制 OL 欠套不得超过 20 层 ...
不存在嵌套问题,不需要嵌套,直接按字节写入目标文件即可
Top
18 楼gongchenwei()回复于 2007-03-02 17:01:02 得分 0
#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;
}
Top
19 楼gongchenwei()回复于 2007-03-02 17:02:12 得分 0
上面有些多余的东西Top





