不使用 goto 将变量转移到较早的点

发布于 2024-08-16 19:55:33 字数 1417 浏览 7 评论 0原文

如何在不使用 goto 的情况下编写此内容:

ob_start();
$a = 0;
echo "START of LEFT<br />";
begin:
   if($a > 0) {
      echo "CONTENT LEFT: $a<BR />";
      <VERY DIFFICULT ALGORHITM>
       goto end;
   }
<... ALL THE REST CODE OF LEFT ...>
echo "END of LEFT<br /><br />";
$output1 = ob_get_contents();
ob_end_clean();

ob_start();    
echo "START of CENTER<br />";
$a = 5; goto begin;
end:
   <... ALL THE REST CODE OF CENTER ...>
   echo "END of CENTER<br />";
   $output2 = ob_get_contents();
   ob_end_clean();
   // print it
   echo $output1.$output2;

要获得此 echo:

START of LEFT
CONTENT LEFT: 5
END of LEFT

START of CENTER
END of CENTER

要求:

1. 我不允许更改顺序(CORE( echo $a )和 PLUGIN( $a=5 )):

ob_start();
$a = 0;
<ANY CODE>
echo $a;
$output1 = ob_get_contents();
ob_end_clean();

ob_start();
<ANY CODE>
$a = rand(0,10);
$output2 = ob_get_contents();
ob_end_clean();

2.输出必须通过ob_get_contents()生成;

但我可以在某些地方编写任何代码。

// 解决方案 ob_get_contents(); 仅当想要替换输出 HTML 代码中的几行,但无法更改变量的值时才有帮助,以更改 ALGORHYTM(取决于 var 值) ),它生成随机 HTML 代码。

还, 当我检查我的代码时,我明白,我的代码即使使用 GOTO labels 语句不会更改 $output1 内容?怎么做呢?唯一的方法是从头开始重新缓存 $output1 。或者也许我可以通过其他方式做到这一点?

How to write this without goto:

ob_start();
$a = 0;
echo "START of LEFT<br />";
begin:
   if($a > 0) {
      echo "CONTENT LEFT: $a<BR />";
      <VERY DIFFICULT ALGORHITM>
       goto end;
   }
<... ALL THE REST CODE OF LEFT ...>
echo "END of LEFT<br /><br />";
$output1 = ob_get_contents();
ob_end_clean();

ob_start();    
echo "START of CENTER<br />";
$a = 5; goto begin;
end:
   <... ALL THE REST CODE OF CENTER ...>
   echo "END of CENTER<br />";
   $output2 = ob_get_contents();
   ob_end_clean();
   // print it
   echo $output1.$output2;

To get this echo:

START of LEFT
CONTENT LEFT: 5
END of LEFT

START of CENTER
END of CENTER

Requirements:

1. I'm not allowed to change the order(CORE( echo $a ), and PLUGIN( $a=5 )):

ob_start();
$a = 0;
<ANY CODE>
echo $a;
$output1 = ob_get_contents();
ob_end_clean();

ob_start();
<ANY CODE>
$a = rand(0,10);
$output2 = ob_get_contents();
ob_end_clean();

2. Output must be generated via ob_get_contents();

But I'm allowed to write ANY CODE in places.

// Solvings
ob_get_contents(); Helps only if want to replace few lines in output HTML CODE, but can't change a value of variable, to change the ALGORHYTM(depends of var value), which generates the random HTML code.

Also,
As I checked my code, I understand, that my code, even with GOTO labels statement , DOES NOT going to change the $output1 content ?. How to do that? Is the only way is to recache the $output1 from his beggining. Or maybe I'm able to do this in other ways?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

您熟悉方法/函数的概念吗?如果没有(而且似乎很有可能......),你真的应该首先了解一些有关这些的知识。然后,将单个代码块中的功能拆分为小的、可维护的代码片段就轻而易举了。

You are familiar with the concept of methods/functions? If not ( and it seems that chances are.. ) you should really learn something about those first. It's then a piece of cake to split functionality out of a monolithic block of code to small, maintainable pieces of code.

眼藏柔 2024-08-23 19:55:33

如果我很好地理解你想要做什么,我会使用递归函数而不是 goto。例如,看看我前一段时间用 C 编写的数学表达式解析器:

    #include <cstdio>

const long MAX = 100010;
char S[MAX], *p=S;

long eval();
long termen();
long factor();


int main() {
    FILE *fin=fopen("evaluare.in", "r");
    FILE *fout=fopen("evaluare.out", "w");

    fgets(S, MAX, fin);
    fprintf(fout, "%ld\n", eval());
    return 0;
}

long eval() {
    long r = termen();
    while ( *p=='+' || *p=='-' ) {
        switch ( *p ) {
            case '+':
                ++p;                        // go over "+"
                r += termen();
                break;
            case '-':
                ++p;                        // go over "-"
                r -= termen();
                break;
        }
    }
    return r;
}

long termen() {
    long r = factor();
    while ( *p=='*' || *p=='/' ) {
        switch ( *p ) {
            case '*' :
                ++p;
                r *= factor();
                break;
            case '/':
                ++p;
                r /= factor();
                break;
        }
    }
    return r;
}

long factor() {
    long r=0;
    if ( *p == '(' ) {                      // subexpression
        ++p;                                // go over '('
        r = eval();
        ++p;                                // go over ')'
    } else {
        while ( *p>='0' && *p<='9' )  {     // number
            r = r*10 + *p - '0';
            ++p;
        }
    }
    return r;
}

主要思想是将代码拆分为函数,并且您可以说:

goto apocalypse

您将拥有:

Apocalypse();

这是长话短说。

If i understood well what you want to do I would use recursive functions instead of goto. for example look at this math expression parser i've witten in C some time ago:

    #include <cstdio>

const long MAX = 100010;
char S[MAX], *p=S;

long eval();
long termen();
long factor();


int main() {
    FILE *fin=fopen("evaluare.in", "r");
    FILE *fout=fopen("evaluare.out", "w");

    fgets(S, MAX, fin);
    fprintf(fout, "%ld\n", eval());
    return 0;
}

long eval() {
    long r = termen();
    while ( *p=='+' || *p=='-' ) {
        switch ( *p ) {
            case '+':
                ++p;                        // go over "+"
                r += termen();
                break;
            case '-':
                ++p;                        // go over "-"
                r -= termen();
                break;
        }
    }
    return r;
}

long termen() {
    long r = factor();
    while ( *p=='*' || *p=='/' ) {
        switch ( *p ) {
            case '*' :
                ++p;
                r *= factor();
                break;
            case '/':
                ++p;
                r /= factor();
                break;
        }
    }
    return r;
}

long factor() {
    long r=0;
    if ( *p == '(' ) {                      // subexpression
        ++p;                                // go over '('
        r = eval();
        ++p;                                // go over ')'
    } else {
        while ( *p>='0' && *p<='9' )  {     // number
            r = r*10 + *p - '0';
            ++p;
        }
    }
    return r;
}

The main idea is to split the code in functions and where you have let's say:

goto apocalypse

you'll have:

Apocalypse();

That's the long story said short.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文