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

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

问题描述

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)'?

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.

因此:第一行将打印出三个字符串,并在它们之间留有空格(此处还会显示变量中的任何尾随空格).第二个命令打印数组的八个整数元素,其中每个字段的宽度为五.

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天全站免登陆