关于 Pascal 输出的一些茫然

发布于 2024-12-09 17:26:48 字数 3328 浏览 1 评论 0原文

平台:Linux 2.6.18 x86_64

编译器:Free Pascal Compiler version 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!

数字是我的输入。我用谷歌搜索了一下,认为这是因为缓冲区的原因。所以我尝试在写入之后添加刷新(stdout)或刷新(输出)。但它仍然效果不佳。

我希望它的作用是:

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 仍然在第一次 Write 之前执行。

对了,我也尝试过:

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上的keiy!

Platform: Linux 2.6.18 x86_64

Compiler: Free Pascal Compiler version 2.2.2 [2008/11/05] for x86_64

The source code is:

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.

I compiled it and run it. But the ouput is something like:

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!

The numbers are my input. I googled it, and thought it is because of the buffer. So I tried to add the flush(stdout) or flush(output) after the Write. But it still does not work well.

I hope it works as:

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!

Thank you! And sorry for my fool~


Addition:
I tried as (Thank you, 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.

The result is still not well as:

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!

The Readln is still executed before the first Write.

By the way, I also tries:

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.

In this case, the first loop is good, but the others are still wrong.

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!

Thank you!


Final Solution:
http://www.amath.unc.edu/sysadmin/DOC4.0/pascal/lang_ref/ref_io.doc.html#592
It's because, the eof actually corresponds to an implicit read.
The current code should be:

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.

Lastly, thank you, Aloush here and keiy on CSDN!

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

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

发布评论

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

评论(1

瀞厅☆埖开 2024-12-16 17:26:48

你可以尝试以下方法吗?

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.

Can you try the following?

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