'*' and '/' not recognized on input by a read statement

前端 未结 2 2048
Happy的楠姐
Happy的楠姐 2020-11-28 17:06

I start learning Fortran and I\'m doing a little case test program where the user types two real numbers and selects an arithmetic operators (from + - * /). The following e

2条回答
  •  伪装坚强ぢ
    2020-11-28 17:31

    FORTRAN input and output formatting rules are rather involved. Each input and ouptut statement has two arguments that have special meaning. For example

      READ (10,"(2I10)") m,n
    

    The first argument is a file descriptor. Here it is 10. The second argument "(2I10)" is the format specifier. If you give an asterisk (*) as a format specifier you switch on the list-directed formatting mode.

    List directed input as the name suggests is controlled by the argument list of the input operator.

    1. Why asterisk (*) is special in list-directed input mode?

    The input list is split into one or more input records. Each input record is of the form c, k*c or k* where c is a literal constant, and k is an integer literal constant. For example,

      5*1.01
    

    as an instance of k*c scheme is interpreted as 5 copies of number 1.01

       5*
    

    is interpreted as 5 copies of null input record.

    The symbol asterisk (*) has a special meaning in list-directed input mode. Some compiler runtimes would report a runtime error when they encounter asterisk without an integer constant in list-directed input, other compilers would read an asterisk. For instance GNU Fortran compiler is known for standards compliance, so its runtime would accept *. Other compiler runtimes might fail.

    2. What's up with slash (/)?

    A comma (,), a slash (/) and a sequence of one or more blanks ( ) are considered record separators in list-directed input mode.

    There is no simple way to input a slash on its own in this mode.

    3. Possible solution: specify format explicitly

    What you can do to make the runtime accept a single slash or an asterisk is to leave the list-directed input mode by specifying the format explicitly:

    read (*,"(A1)") oper
    

    should let you input any single character.

提交回复
热议问题