打印语句中引号中的字符 [英] Characters in quotes in a print statement

查看:29
本文介绍了打印语句中引号中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

fortran-95 中 datetime 子例程的文档提供了有关数据和时间使用的示例.代码如下:

The documentation for the datetime subroutine in fortran-95, gives an example on the usage of data and time. Here's the code:

program test_time_and_date
    character(8)  :: date
    character(10) :: time
    character(5)  :: zone
    integer,dimension(8) :: values
    ! using keyword arguments
    call date_and_time(date,time,zone,values)
    call date_and_time(DATE=date,ZONE=zone)
    call date_and_time(TIME=time)
    call date_and_time(VALUES=values)
    print '(a,2x,a,2x,a)', date, time, zone
    print '(8i5)', values
end program test_time_and_date

在上面的代码片段中,单引号内的字符是什么,例如 '(a,2x,a,2x,a, 8i5)',在 print 语句中?

In the above snippet what are the characters inside single quotes, for example '(a,2x,a,2x,a, 8i5)', in the print statements?

推荐答案

输出语句采用格式说明符,它控制显示项目的外观.输入语句也使用一种格式来控制输入的解释.

Output statements take a format specifier which controls the look of the displayed items. Input statements also use a format to control the interpretation of input.

print 版本的输出(与 write 比较)中,格式说明符是(未加引号的)逗号之前的部分.在所有形式的 I/O 中,可以通过以下三种方式之一指定格式:

In the print version of output (compare with write) the format specifier is the part before the (unquoted) comma. In all forms of I/O the format may be specified by one of three ways:

  • 使用 *(所谓的list-directed I/O);
  • format 语句使用标签;
  • 使用字符表达式(变量或常量).
  • using a * (so-called list-directed I/O);
  • using a label for a format statement;
  • using a character expression (variable or constant).

问题中的形式是第三个.

The form in the question is this third.

格式规范的字符表达式由多个部分组成.这里的引号只是字符串的分隔符.

A character expression for a format specification is made up of a number of parts. The quotes here are simply a delimiter for the string.

首先是一对强制性的括号,导致最短的格式规范'()'(不打印任何内容).

First is the mandatory pair of parentheses, leading to the shortest format specification '()' (which prints nothing).

接下来是格式项目,可能是:

Next, are format items which may be:

  • 字符文字常量;
  • 编辑描述符;
  • 控制描述符;
  • 更多格式项的集合.

格式项目前面可能有一个重复计数.

Format items may have a repeat count ahead of them.

要回答所提出的问题,我将仅考虑引用的格式规范的编辑和控制描述符.其他描述符的更广泛的细节(以及这里的更多细节)和格式可以在其他地方找到关于搜索词的新知识.

To answer the question as asked I'll consider just the edit and control descriptors of the format specifications quoted. Wider details of other descriptors (and more detail on the ones here) and formatting may be found elsewhere with the newly gained knowledge on search terms.

这里有两个编辑描述符:ai.有一个控制描述符x.

There are two edit descriptors here: a and i. There is one control descriptor x.

a 指定字符串的输出,i 指定整数.i5 表示输出宽度为五(如果整数的输出少于五位,则填充空白);a 导致宽度为输出字符表达式的长度.

a specifies output of a string and i an integer. i5 says the output will be width five (blank padded if the integer has fewer than five digits on output); a leads to width the length of the character expression for output.

控制描述符x插入一个空白.2x 是重复计数为 2 的空白:输出两个空格.

The control descriptor x inserts a blank. 2x is a blank with repeat count 2: giving two spaces output.

所以:第一行打印出三个字符串,它们之间有空格(这里也会出现变量中的任何尾随空格).第二个打印数组的八个整数元素,其中每个字段的宽度为 5.

So: the first line prints out three strings with spaces between them (here any trailing blanks from the variables will also appear). The second prints the eight integer elements of the array where each field has a width of five.

每个 print 语句还给出一个换行符.

Each print statement also gives a newline.

这篇关于打印语句中引号中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆