这段代码怎么优化

Dlwxn 2003-09-29 12:40:57
var
A,B,C:Boolean;
Begin
if A and B and C then 语句1
else if A and (not B) and (not C) then 语句2
else if (not A) and B and (not C) then 语句3
else if (not A) and (not B) and C then 语句4
else if A and B and (not C) then 语句5
else if A and (not B) and C then 语句6
else if (not A) and B and C then 语句7
else then 语句8
End

看到这段代码我就烦,有没有好的方法?
...全文
35 36 打赏 收藏 转发到动态 举报
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
kylincn 2003-09-29
  • 打赏
  • 举报
回复
效率高不高不敢说,不过为了易读易维护,给你点建议:
if a then
begin
if b then
begin
if c then
begin
语句1
end
else
begin
语句5
end;
end
else
begin
if c then
begin
end
else
begin
end;
end
end
else
begin
if b then
begin
if c then
begin

end
else
begin

end
end
else
语句4
end;
Dlwxn 2003-09-29
  • 打赏
  • 举报
回复
效率太低
WeiXuBin 2003-09-29
  • 打赏
  • 举报
回复
数据结构
belllab 2003-09-29
  • 打赏
  • 举报
回复
mengxiang5160 2003-09-29
  • 打赏
  • 举报
回复
var
A,B,C:Boolean;
Begin
if A then
begin
if B and C then 语句1
else if (not B) and (not C) then 语句2
else if A and B and (not C) then 语句5
else if A and (not B) and C then 语句6
end
else if (not A) then
begin
if B and (not C) then 语句3
else if (not B) and C then 语句4
else if B and C then 语句7
end
else 语句8
End
用这个办法还可以继续改进!


gdfd 2003-09-29
  • 打赏
  • 举报
回复
这八种情况没有交集……只能按哪种出现得最多就放前面来提高效率吧……用CASE是一样的……
Dlwxn 2003-09-29
  • 打赏
  • 举报
回复
如果用这个
var
Total: integer;
A, B, C: Boolean;
begin
A:=False;
B:=False;
C:=False;

Total := Integer(A) * 100 + Integer(B) * 10 + Integer(C);

case Total of
111: edit1.Text := '111';
110: edit1.Text := '110';
101: edit1.Text := '101';
100: edit1.Text := '100';

011: edit1.Text := '011';
010: edit1.Text := '010';
001: edit1.Text := '001';
000: edit1.Text := '000';
end;


end;
和上面的一个效率是不用说了!
Dlwxn 2003-09-29
  • 打赏
  • 举报
回复
如果改成
var
A,B,C:Boolean;
Begin
A:=False;
B:=False;
C:=False;
if A and B and C then 语句1
else if A and (not B) and (not C) then 语句2
else if (not A) and B and (not C) then 语句3
else if (not A) and (not B) and C then 语句4
else if A and B and (not C) then 语句5
else if A and (not B) and C then 语句6
else if (not A) and B and C then 语句7
else then 语句8
End

每次都执行前面的7个判断,然后才能执行 语句8 这个不是任何视觉误差吧?还是效率的问题。
cash2000 2003-09-29
  • 打赏
  • 举报
回复
同意yoic。
crossbow 2003-09-29
  • 打赏
  • 举报
回复
http://crossbow.myrice.com/formatx/
crossbow 2003-09-29
  • 打赏
  • 举报
回复
只是要减少视觉误差,干脆用我的SoruceFormatX格一下吧,
IORILI 2003-09-29
  • 打赏
  • 举报
回复
暂时没什么好的方法,
Dlwxn 2003-09-29
  • 打赏
  • 举报
回复
我认为在很多判断的情况下,还是yoic(我是一棵菠菜,菜,菜,菜,菜,菜,菜) 和 FrameSniper(§绕瀑游龙§) 的那个方法比我的问题效率高。
lty 2003-09-29
  • 打赏
  • 举报
回复
可以考虑用集合变量,那样代码清楚一些。
Dlwxn 2003-09-29
  • 打赏
  • 举报
回复
这样写效率很低!但恐怕没有其他的方法了
/////////////////////////////////////
如果你不知道哪条执行次数最多,把他放在最下面,而且有很多这样的判断,你说效率能高吗?

因为我在调试程序,用了很多if....else....,一步一步的就能感觉到。
FrameSniper 2003-09-29
  • 打赏
  • 举报
回复
这个楼主根本不是要优化,他只是要减少视觉误差!
dickeybird888 2003-09-29
  • 打赏
  • 举报
回复
这样写效率很低!但恐怕没有其他的方法了
FrameSniper 2003-09-29
  • 打赏
  • 举报
回复
其实很简单,使用if....else....结构表面上看起来复杂,实际就是对一个集合进行划分,划分后的若干子集合是补的关系,没有任何交的关系....
耙子 2003-09-29
  • 打赏
  • 举报
回复
你这个关系条件还不复杂。

这类题目通常的解法是建立真值表,这是很多表达就能合并了,只然后根真值表用case 或者 查表法写代码。
FrameSniper 2003-09-29
  • 打赏
  • 举报
回复
这不叫优化,这叫减少视觉误差!
加载更多回复(16)

5,386

社区成员

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

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