高分再提问.....如何提高效率!!~~!~!急!~急!~急@!!~~!

Mrkang 2006-09-08 05:17:37
procedure TRichEdit2.SetInputText(const Value: string);
var WS1, WS2: string;
i, L, P, Count: Integer;
begin
FInputText := Value;
Self.SelStart := 0;
Self.SelLength := Length(Self.Text);
Self.SelAttributes.Color:=Font.Color;
WS1 := Value; // StringToOleStr(Value);
WS2 := Self.Text; //StringToOleStr(Self.Text);
L := Length(WS2);
if Length(WS1)<=0 then Exit;
Count := 0;
P := 0;
for i := 1 to L do
begin
if ws1[i] = ws2[i] then
inc(Count)
else
begin
if (Count mod 2)=1 then
begin
if Pos(ws2[i],Checkstr)<=0 then
dec(Count);
end;
Self.SelStart := P;
Self.SelLength := Count;

Self.SelAttributes.Color := clRed;
P := i;
Count := 0;
end;
if (i>Length(ws1)) then
begin
Count:=0;
end;
end;
if Count <> 0 then
begin
Self.SelStart := P;
Self.SelLength := Count;
Self.SelAttributes.Color := clRed;
end;
end;
//我要写一个考试打字 的程序,上面的这段代码是把录入文本和要求的文本进行对比,如果正确,那么字就变成红色,否则不变色....程序执行都没问题了,但是现在的效率很地下,如果给定文本再400字以上,打字速度和颜色显示的速度就很慢....如何提高这个效率呢?程序里是再Memo1里的Onchange事件里把来判断打的字是否正确的,Value实际上就是Memo1.Lines.Text..但是每次都要去Memo1.Text作为字符串好像效率太低了..如何提高..求各位高人指点...拜托..分不够再加
...全文
220 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Mrkang 2006-09-09
  • 打赏
  • 举报
回复
up
我是RichEdit显示考试内容,用Memo输入文字,
wudi_1982 2006-09-09
  • 打赏
  • 举报
回复
按照你“我是RichEdit显示考试内容,用Memo输入文字”的意思,我临时写了一个,不完善,但基本可以反映一个思路。

我的做法是这样的,限制memo上每行的汉字个数,我这里是限制为10个,当超过10的时候自动将多余内容拷贝到下一行(当然,richedit的每行也是只有10个汉字),然后当一行输入完毕后,根据行数对这一行的内容进行比较,因为每次传递的对比对象只是一行,也就是10个汉字,所以不会出现随着内容增多比较变慢的情况。


代码如下:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;

const
MaxLength=20;

type
TForm1 = class(TForm)
RichEdit1: TRichEdit;
Memo1: TMemo;
procedure Memo1Change(Sender: TObject);
private
public
procedure db(b: WideString;linecount :integer);
end;

var
Form1: TForm1;

implementation

uses StrUtils;

{$R *.dfm}

procedure TForm1.Memo1Change(Sender: TObject);
var
tm : string;
dbstring : WideString;
begin
if length(trim(Memo1.Lines[Memo1.Lines.count-1])) >= MaxLength then
begin
tm := copy(Memo1.Lines[Memo1.Lines.count-1],MaxLength+1,length(trim(Memo1.Lines[Memo1.Lines.count-1]))-MaxLength);
dbstring := widestring(Memo1.Lines[Memo1.Lines.count-1]);
db(dbstring,Memo1.Lines.Count-1);
Memo1.Lines.Add(tm);
end;

end;

procedure TForm1.db(b: WideString;linecount :integer);
var
i ,richLine: integer;
tm : WideString;
begin
richLine := linecount div 2;
tm := RichEdit1.Lines[richline];
for i := 1 to MaxLength div 2 do
begin
if tm[i]<>b[i] then
begin
RichEdit1.SelStart := (richLine * MaxLength) +i*2-1;
RichEdit1.SelLength := 2;
RichEdit1.SelAttributes.Color := clRed;
end;
end;
end;

end.
Mrkang 2006-09-08
  • 打赏
  • 举报
回复
还有更好的方法的吗?最好能在我的原来的代码的基础上帮忙改下..谢谢了1!!!!!
这个问题肯定400分.
Elysium 2006-09-08
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var s:string;
iLine,iCol,j,k:integer;
begin
iCol:=memo1.CaretPos.x; {取得光标所在列}
iLine:=memo1.CaretPos.y; {取得光标所在行}
s:=memo1.Lines.Strings[iLine];
if s <> '' then
begin
if s[iCol] <> ch
...
end;
end;
Mrkang 2006-09-08
  • 打赏
  • 举报
回复
是汉字,
如何判断光标附近的呢???请指点,学Delphi很短又丢了很久,最好能有代码200分送上,另开贴给200分.
Elysium 2006-09-08
  • 打赏
  • 举报
回复
按字比比较快吧,把当前输入的字与对应该位置的内存或字符串中的索引字一比较就行了吧,不一致就变色
wudi_1982 2006-09-08
  • 打赏
  • 举报
回复
但是每次都要去Memo1.Text作为字符串好像效率太低了

如果是每次传递你所谓的那400个以上的字符,效率肯定低啊。可以按照楼上说的,只和光标附近的比较,楼主可以研究一下tstringlist类,除了和光标附近的比较,还可以根据行数,来比较,而你可以把每一行的字数限制死,这样,就会比较快了。
postren 2006-09-08
  • 打赏
  • 举报
回复
每次比较的时候不用对整个Memo.Lines.Text进行操作吧,只比较光标附近的文本(也就是刚刚输入或修改过的)应该效率会高很多
wudi_1982 2006-09-08
  • 打赏
  • 举报
回复
说说你的具体需求,判断打字的程序是判断英文还是中文,是打字完成之后再进行判断还是再打字的时候就判断?
wudi_1982 2006-09-08
  • 打赏
  • 举报
回复
做个记号晚上看

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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