谁帮我简化一下这段程序(50分)

Y

Yves

Unregistered / Unconfirmed
GUEST, unregistred user!
谁帮我简化一下这段程序
pocedure x();
begin
res:=a();
if res<> r_OK then begin result:=res
exit
end;
res:=b(c);
if res<> r_OK then begin result:=res
exit
end;
res:=d(e,f);
if res<> r_OK then begin result:=res
exit
end;
res:=g();
if res<> r_OK then begin result:=res
exit
end;
res:=h();
if res<> r_OK then begin result:=res
exit
end;
res:=i();
if res<> r_OK then begin result:=res
exit
end;
res:=j();
if res<> r_OK then begin result:=res
exit
end;
end;
我这段程序的意思是:
调用一系列不同的函数,每个函数都有一个返回值,如果返回值不是 r_ok 就 把x()的返回值置为res(即先前调用的函数返回值),并立刻退出 x()函数
我现在每个函数调用都要写一条if 语句,非常繁杂。请大家帮忙想想办法简化一些,谢谢了!
 
你的函数返回值有问题
如a()既然只有两种返回,不如
函数都返回Boolean
 
不是,不是
各种函数(a(),d()...)都有很多返回值的情况
 
用Goto吧
 
我觉得也不行
 
label PPP;
res:=a();
if res<> r_OK then Goto PPPP;
res:=b(c);
if res<> r_OK then Goto PPP;
res:=d(e,f);
...
PPP:
Result:=res;
 
能不能 再简化一些 况且我的exit 怎么办,得立即退出
其实我不是单纯的想少打几个字
我是想 彻底的在结构上简化,比如能不能换成循环结构?(我想了好久都不行,大家快帮忙)
 
这个过程有问题,返回的值类型不同呀
 
PPP后就是
Result:=res
后面就是
end了
 
怎么不同
都一样 那些a() b() d().... 返回值都一样,都是整型(具体什么型也无所谓,总之是一样)
 
你的每个函数参数也不一样
统一调用似乎不可能
 
我也觉得,等等,明天人多的时候,看看有没有 戏
先谢过 ycxy和 dfw1001
 
要是参数个数都一样还好办,要不然,这里简单了,其他地方(做准备的)又复杂了[:D]
比如,你要两个列表,一个函数指针列表,一个参数指针列表。
 
Result=a();
if Result=r_ok then exit;
 
只能到这份上了[:(]
begin
res := a();
if res = r_OK then
begin
res := b(c);
if res = r_OK then
begin
res := d(e, f);
if res = r_OK then
begin
res := g();
if res = r_OK
begin
res := h();
if res = r_OK then
begin
res := i();
if res = r_OK then
begin
res := j();
if res = r_OK then
exit;
end;
end;
end;
end;
end;
end;
result := res;
end;
 
你这个比我的还复杂
 
什么乱七八糟的程序

有返回值的程序段 不是procedure (也不是你写的 pocedure ),而是 function,
Do you Know?

你要告诉我返回值是什么类型,我才可以帮你改写这个程序呀
 
这个我当然知道
我 那个x() 并不是代表 a() c()
而是另一个函数
 
我这个程序( x() ) 是 连续的调用一系列的 过程
每个过程都有一个 返回值,无论哪个过程 的返回值不为r_ok 就立即 退出x()
 
function A: Integer;
begin
Result := 0;
end;

function B: Integer;
begin
Result := 0;
end;

function C(X: Integer): Integer;
begin
Result := 0;
end;

function D(X, Y: Integer): Integer;
begin
Result := 0;
end;

function Some: Integer;
type
TFunc1 = function: Integer;
TFunc2 = function(X: Integer): Integer;
TFunc3 = function(X, Y: Integer): Integer;
TFuncMethod = packed record
dwType: Word;
Proc: Pointer;
end;
const
R_OK = 0;
FuncArr: array [0..3] of TFuncMethod = (
(dwType: 1
Proc: @A),
(dwType: 1
Proc: @B),
(dwType: 2
Proc: @C),
(dwType: 3
Proc: @D));
var
I: Integer;
Func: TFuncMethod;
begin
for I := 0 to 3 do
begin
Func := FuncArr;
case Func.dwType of
1: Result := TFunc1(Func.Proc);
2: Result := TFunc2(Func.Proc)(0);
3: Result := TFunc3(Func.Proc)(0, 0);
else
Result := R_OK;
end;
if Result <> R_OK then break;
end;
end;
 
顶部