读取语句输入时不能识别'*'和'/' [英] '*' and '/' not recognized on input by a read statement

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

问题描述

我开始学习Fortran,我正在做一个小小例子测试程序,用户输入两个实数并选择一个算术运算符(从+ - * /)。当用户选择*时出现以下错误:

  F6502:读取< con> - 重复字段中预期的正整数

如果用户选择/,编译器将执行默认大小写,并显示此消息

 无效的运算符,谢谢
结果是0.000000E + 00

code>

该程序如下。

  b 








$ b $第一个数字a:'
read *,a
print *,'给第二个数字b:'
read *,b
print *,'哪个操作?'
读*,oper
!print *,'donnez a,b,oper:'
!阅读(*,*)a,b,oper

选择案例(操作)

案例('+')
资源= a + b $ b $ (' - ')
res = ab

案例('*')
res = a * b


案例('/')
res = a / b

案例默认
print *,无效的操作员,谢谢

结束选择

print *,'结果是',res

end program operateur


解决方案

FORTRAN输入和输出格式化规则相当复杂。每个输入和ouptut语句都有两个具有特殊含义的参数。例如

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

第一个参数是一个文件描述符。这是 10 。第二个参数(2I10)格式说明符。如果您给星号( * )作为格式说明符,则切换到列表引导的格式化模式。



顾名思义,列表定向输入由输入运算符的参数列表控制。

1。为什么星号( * )在列表控制的输入模式中是特殊的?



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

  5 * 1.01 

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

  5 * 

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

符号星号( * )在列表导向输入模式下有特殊意义。有些编译器运行时会在列表导向输入中遇到星号不带整数常量的情况下报告运行时错误,其他编译器会读取星号。例如GNU Fortran编译器以标准兼容而闻名,所以它的运行时将接受 * 。其他编译器运行时可能会失败。



2。怎么了斜线( / )?



逗号(),一个斜杠( / )和一个或多个空白序列( )被认为是以列表控制输入模式记录分隔符



在这种模式下,没有简单的方法输入斜线。



3。可能的解决方案:显式指定格式



你可以做什么来使运行时接受单斜线或星号是通过明确指定格式来离开列表引导输入模式:

  read(*,(A1))oper 

应该让你输入任何单个字符。


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

The program is as follows.

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 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.

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

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