'*' 和 '/' 在 read 语句的输入中无法识别 [英] '*' and '/' not recognized on input by a read statement

查看:10
本文介绍了'*' 和 '/' 在 read 语句的输入中无法识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习 Fortran,我正在做一个小案例测试程序,其中用户键入两个实数并选择一个算术运算符(从 + - */).用户选择*"时出现如下错误

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 error appears when the user selects "*"

F6502 : read <con> - positive integer expected in repeat field

如果用户选择/",编译器将执行默认情况,并显示此消息

and if the user selects "/" the compiler executes the default case, and displays this message

invalid operator, thanks
the result is 0.000000E+00

程序如下.

program operateur
implicit none

   CHARACTER(LEN=1) :: oper 
   real::a,b,res
   print*,'Give the first number a :'
   read*,a
   print*,'Give the second number b :'
   read*,b
   print*,'which operation ?'
   read*,oper
   !print*,'donnez a,b,oper :'
  ! read(*,*)a,b,oper

               select case (oper)

                  case ('+')      
                  res=a+b

                  case ('-')       
                  res=a-b

                  case ('*')       
                  res=a*b


                  case ('/')       
                  res=a/b           

                  case default
                  print*, "Invalid Operator, thanks" 

               end select

   print*,'the result is ',res 

end program operateur

推荐答案

FORTRAN输入输出格式规则比较复杂.每个输入和输出语句都有两个具有特殊含义的参数.例如

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

第一个参数是一个文件描述符.这里是 10.第二个参数(2I10)"格式说明符.如果您将星号 (*) 作为格式说明符,则您将打开列表导向格式设置模式.

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.

输入列表被拆分为一个或多个输入记录.每个输入记录的格式为 ck*ck* 其中 c 是文字常量,k 是一个整数文字常量.例如,

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

作为 k*c 方案的一个实例,被解释为数字 1.01

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

   5*

被解释为空输入记录的 5 个副本.

is interpreted as 5 copies of null input record.

符号星号 (*) 在列表导向输入模式中具有特殊含义.一些编译器运行时在列表导向输入中遇到没有整数常量的星号时会报告运行时错误,其他编译器会读取星号.例如 GNU Fortran 编译器以符合标准而闻名,因此它的运行时将接受 *.其他编译器运行时可能会失败.

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.

逗号 (,)、斜杠 (/) 和一个或多个空格的序列 ( ) 被视为 列表导向输入模式中的记录分隔符.

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.

要使运行时接受单斜杠或星号,您可以通过明确指定格式来退出列表导向输入模式:

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.

这篇关于'*' 和 '/' 在 read 语句的输入中无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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