试题库开发中非文本数据的处理(转)

一个设计优良的试题库系统,除了支持文本数据以外,还应该支持公式、图片、表格等非文本数据,这样才能很好地支持已有的试题形式。笔者前一阵子使用过一个“XXX考核题库系统”,该软件使用Paradox作为数据库,只能支持文本题目,无法满足现实试题的要求,为此对其进行了更改。[@more@] 对于非文本数据的支持可以采用OLE嵌入Word文档的方法,但这种技术实现起来比较复杂,本文给出一种简单而实用的方法:非文本数据用rtf文档导入到存储,编辑器采用RichTextBox。RichTextBox是微软提供的一个ActiveX,导入到Delphi中之后,使用起来就同其他组件一样方便。RichTextBox可以很好地支持文本、公式、图片、表格等,而且它有一个TextRTF属性可供访问,可以把这些文档元素自动转为rtf字符串。比如把RichTextBox1的内容赋给RichTextBox2时,只需要RichTextBox2.TextRTF := RichTextBox1.TextRTF就可以了。
另外非文本数据往往数据量比较大,因此可以在输入到数据库时可以进行数据压缩,而在导出时进行还原,从而减少存放数据量,而又不影响使用。
结合这两点,可以这样实现非文本数据的处理:试题内容用Word或者写字板编辑后加载到RichTextBox中,在RichTextBox中可以进行必要的编辑。存储到数据库时先得到RichTextBox的TextRTF字符串,经过压缩后存储到数据库。而生成试卷时从数据库中读出字符串,然后解压得到原来的TextRTF字符串,加载到RichTextBox中,而后就可以用来进一步的处理得到图文并茂的试卷。
压缩时可以使用zlib来压缩(要引用zlib单元),然后使用下面两个函数。
压缩函数
function MyCompress(str: string): string;
var
nSize: integer; // ***此处必须与DeCompress中的类型一致
strmCompression: TCompressionStream;
strmMemory1, strmMemory2: TMemoryStream;
begin
nSize := Length(str);
strmMemory1 := TMemoryStream.Create;
try
strmCompression := TCompressionStream.Create(cldefault, strmMemory1);
try
strmCompression.Write(str[1], nSize);
finally
FreeAndNil(strmCompression);
end;
strmMemory1.Position := 0;
strmMemory2 := TMemoryStream.Create;
try
strmMemory2.WriteBuffer(nSize, sizeof(nSize));
strmMemory1.SaveToStream(strmMemory2);
strmMemory2.Position := 0;
SetLength(result, strmMemory2.Size);
strmMemory2.Read(result[1], strmMemory2.Size);
finally
FreeAndNil(strmMemory2);
end;
finally
FreeAndNil(strmMemory1);
end;
end;
解压缩函数
function MyDecompress(str: string): string;
var
nSize: integer;
strmDecompression: TDecompressionStream;
strmMemory: TMemoryStream;
begin
strmMemory := TMemoryStream.Create;
try
strmMemory.Write(str[1], Length(str));
strmMemory.Position := 0;
strmMemory.ReadBuffer(nSize, sizeof(nSize));
strmDecompression := TDecompressionStream.Create(strmMemory);
try
SetLength(result, nSize);
strmDecompression.ReadBuffer(result[1], nSize);
finally
FreeAndNil(strmDecompression);
end;
finally
FreeAndNil(strmMemory);
end;
end;
经过测试发现,大多数的RTF文档都可以得到很好的压缩率:即使是全部是文本信息的RTF文档也可以得到倍左右的压缩率,而如果是以图像为主的RTF可以得到10倍甚至高达几十倍的压缩率。

下面是测试压缩、解压缩、压缩率的代码:
procedure TForm1.Test;
var
i, j: integer;
begin
RichTextBox2.TextRTF := MyDecompress(MyCompress(RichTextBox1.TextRTF));
i := Length(RichTextBox1.TextRTF);
j := Length(MyCompress(RichTextBox1.TextRTF));
ShowMessage('Length Before Compression: ' + IntToStr(i)
+ '; Length After Compression: ' + IntToStr(j)
+ '; Compression Ratio: ' + IntToStr(Round(i/j)));
end;
(函数测试环境:Windows 2000 Profressional + Delphi 6)

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8683736/viewspace-910617/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8683736/viewspace-910617/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值