【问题标题】:How to determine data type entered by user?如何确定用户输入的数据类型?
【发布时间】:2016-07-09 22:53:27
【问题描述】:

我正在编写一个模拟 ATM 机工作的程序。 基本上,如果输入了无效字符,我想显示一条错误消息。例如:

'Please enter your name...'

[here user enters a random **digit**, which is not a character]

[here I want the program to **determine** whether the input type is
*character* or *integer* and then decide what to do next : 
show an error or continue running]

我只是想知道是否有可能这样做?

【问题讨论】:

    标签: types fortran user-input gfortran fortran95


    【解决方案1】:

    有两件事。更容易查看是否有人输入了数字。 Read 会尝试将输入的值放入提供的变量中,如果无法做到,则会将iostat 参数设置为正数:

    program determine
        implicit none
        integer :: iNumber
        integer :: io_stat
    
        do
            print *, "Please enter a number"
            read(*, *, iostat=io_stat) iNumber
            if (io_stat == 0) exit
            print *, "This isn't a number, try again!"
        end do
        print *, "You entered a number ", iNumber
    end program determine
    

    当然,另一种方式并不那么容易。 "Hello" 绝不是整数,但 "12" 肯定是字符串。因此,在这种情况下,您必须直接验证字符串。一个简单、快速而肮脏的解决方案是这样的:

    program determine
        implicit none
        character(len=50) :: cName
    
        do
            print *, "Please enter a name: (A-Za-z)"
            read(*, '(A50)') cName
            if (valid_input(cName)) exit
            print *, "No valid input! Try again!"
        end do
    
        print *, "You entered the name " // trim(cName)
    
        contains
    
            function valid_input(cName)
                implicit none
                character(len=*), intent(in) :: cName
                logical :: valid_input
                integer :: i
    
                valid_input = .false.
    
                if (len_trim(cName) == 0) return
    
                do i = 1, len_trim(cName)
                    select case(ichar(cName(i:i)))
                        case(ichar('A'):ichar('Z'))
                            continue
                        case(ichar('a'):ichar('z'))
                            continue
                        case default
                            return
                    end select
                end do
                valid_input = .true.
            end function valid_input
    end program determine
    

    更新:正如@francescalus 在对此答案的评论中指出的那样,您还可以使用VERIFY 关键字来检查字符串中的不合格字符。当然这意味着您必须将每个符合条件的字母输入到SET 字符串中,但它仍然比我的valid_input 方法短:

    function valid_input(cName)
        implicit none
        character(len=*), intent(in) :: cName
        logical :: valid_input
    
        valid_input =                                   &
            (  ( len_trim(cName) > 0 ) .and.            &
               ( verify(trim(cName),                    &
                        'ABCDEFGHIJKLMNOPQRSTUVWXYZ' // &
                        'abcdefghijklmnopqrstuvwxyz'    &
                       ) == 0                           &
               )                                        &
            )
    
    end function valid_input
    

    当然,这不会验证包含空格或其他非标准字母的名称。如果您需要更复杂的东西,您几乎可以实现正则表达式。示例见here

    【讨论】:

    • 看来verify可能是这里的朋友。
    • 这就是我喜欢 Stackexchage 的原因。我每天都在学习新事物。你为什么不写你的答案?我会赞成的。
    • 我今天使用了这个答案并验证效果很好。如果您想接受特殊字符,请尝试使用 0123456789 进行验证。在某些情况下,iostat 不是最佳解决方案,因为如果您正在读取文件,它用于处理文件结尾。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多