有些人对Pascal输出感到惊讶

时间:2011-10-13 04:02:47

标签: linux pascal

平台:Linux 2.6.18 x86_64

编译器:免费的Pascal编译器版本2.2.2 [2008/11/05] for x86_64

源代码是:

Program main;
var invalue:string;
Begin
        (*Until the EOF, this loop continue to work*)
        while not eof do
        begin
                Write('Please input the express: ');
                Flush(StdOut);
                Readln(invalue);
                Writeln('The expression is: ',invalue);
        end;
        Writeln('');
        Writeln('Exit the program. Bye!');
End.

我编译并运行它。但是输出就像是:

123
Please input the express: The expression is: 123
234
Please input the express: The expression is: 234
345
Please input the express: The expression is: 345

Exit the program. Bye!

这些数字是我的输入。我用Google搜索,并认为这是因为缓冲区。所以我尝试在Write之后添加flush(stdout)或flush(输出)。但它仍然不能很好地运作。

我希望它的工作原理如下:

Please input the express: 123
The expression is: 123
Please input the express: 234
The expression is: 234
Please input the express: 345
The expression is: 345

Exit the program. Bye!

谢谢!对不起我的傻瓜〜


增加: 我试过(谢谢,Aloush!)

Program main;
var invalue:string;
Begin
        (*Until the EOF, this loop continue to work*)
        while not eof do
        begin
                Write('Please input the express: ');
                Flush(StdOut);
                Readln(invalue);
                Writeln('')
                Writeln('The expression is: ',invalue);
        end;
        Writeln('');
        Writeln('Exit the program. Bye!');
End.

结果仍不如:

1
Please input the express: 
The expression is: 1
2
Please input the express: 
The expression is: 2
3
Please input the express: 
The expression is: 3
4
Please input the express: 
The expression is: 4
5
Please input the express: 
The expression is: 5

Exit the program. Bye!

Readln仍然在第一次写入之前执行。

顺便说一下,我也尝试了:

Program main;
var invalue:string;
Begin
        (*Until the EOF, this loop continue to work
        while not eof do
        begin*)
        Repeat
                Write('Please input the express: ');
                Flush(StdOut);
                Readln(invalue);
                Writeln('The expression is: ',invalue);
        Until eof;
        Writeln('');
        Writeln('Exit the program. Bye!');
End.

在这种情况下,第一个循环是好的,但其他循环仍然是错误的。

Please input the express: 123
The expression is: 123
234
Please input the express: The expression is: 234
345
Please input the express: The expression is: 345

Exit the program. Bye!

谢谢!


最终解决方案: http://www.amath.unc.edu/sysadmin/DOC4.0/pascal/lang_ref/ref_io.doc.html#592 这是因为,eof实际上对应于隐式读取。 当前代码应为:

Program main;
var invalue:string;
Begin
        (*Until the EOF, this loop continue to work*)
        Write('Please input the express: ');
        while not Eof  do
        begin
                Readln(invalue);
                Writeln('The expression is: ',invalue);
                Write('Please input the express: ');
        end;
        Writeln('');
        Writeln('Exit the program. Bye!');
End.

最后,谢谢你,Aloush在这里,关于CSDN!

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法吗?

Program main;
var invalue:string;
Begin
        (*Until the EOF, this loop continue to work*)
        while not eof do
        begin
                Write('Please input the express: ');
                Flush(StdOut);
                Readln(invalue);
                Writeln('')
                Writeln('The expression is: ',invalue);
        end;
        Writeln('');
        Writeln('Exit the program. Bye!');
End.