You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Fortran子程序未返回预期月份缩写值问题排查

Fortran程序中月份数字转缩写为空的问题修复

问题根源

  1. 核心错误:num_to_month子程序仅定义了月份缩写的参数数组,但未将对应位置的值赋值给输出参数month_abb,导致该变量始终处于未初始化状态,输出为空。
  2. 其他逻辑问题:
    • 主程序调用子程序后立即执行stop,程序只能处理一个输入就终止,无法实现“多次输入直到输入99”的需求。
    • 无效月份输入(非1-12且不是99)仅打印错误提示,但未跳过后续处理逻辑,会导致无效值被错误计数和处理。
    • 主程序中定义的numbers等数组未被使用,未完成“构建对应数组”的需求。

修正后的完整代码

program Process_MonthNum2Abbr
     implicit none

     integer :: input_val
     integer :: count
     character(len=3) :: my_abb

     integer, allocatable, dimension(:) :: numbers
     character(len=3), allocatable, dimension(:) :: month_abbrs

     !Initialize
     count = 0
     allocate(numbers(0))
     allocate(month_abbrs(0))

     print *, "Enter a month to process - 1 for Jan, or more than one month, one by one (type '99' to stop):"

     !Loop to continuously prompt and read values
     do
        read(*, *) input_val

        !Check for the sentinel value to exit the loop
        if(input_val == 99) exit

        if(input_val < 1 .or. input_val > 12) then
           print*,"Invalid Entry for Month, Try again"
           cycle  ! 跳过当前循环的后续处理
        endif

        count = count + 1
        ! 扩展数组存储月份数字和对应缩写
        call resize_array(numbers, count)
        call resize_array(month_abbrs, count)
        numbers(count) = input_val

        call num_to_month(input_val, my_abb)
        month_abbrs(count) = my_abb
        print*, "Month ", input_val, " is ", my_abb
     enddo

     ! 打印最终存储的月份数组和缩写数组
     if(count > 0) then
        print*, "--- Stored Month Numbers ---"
        print*, numbers
        print*, "--- Corresponding Month Abbreviations ---"
        print*, month_abbrs
     endif

    contains
     ! 通用数组扩展子程序
     subroutine resize_array(arr, new_size)
        implicit none
        class(*), allocatable, intent(inout) :: arr(:)
        integer, intent(in) :: new_size
        class(*), allocatable :: temp(:)

        allocate(temp(new_size))
        if(allocated(arr)) then
           temp(1:size(arr)) = arr
           deallocate(arr)
        endif
        call move_alloc(temp, arr)
     end subroutine resize_array

    end program Process_MonthNum2Abbr

  subroutine num_to_month(month_num, month_abb)
  implicit none

     !Inputs and Outputs
     INTEGER, INTENT(IN) :: month_num
     CHARACTER(LEN=3), INTENT(OUT) :: month_abb

     ! Lookup table for abbreviated months
     CHARACTER(LEN=3), DIMENSION(12), PARAMETER :: months = &
        [CHARACTER(LEN=3) :: "Jan", "Feb", "Mar", "Apr", "May", "Jun", &
                             "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
     ! 核心修正:将对应月份的缩写赋值给输出参数
     month_abb = months(month_num)
     print*,month_num,"abbr",month_abb
    end subroutine num_to_month

关键修正说明

  1. 子程序赋值:在num_to_month中添加month_abb = months(month_num),将匹配的月份缩写赋值给输出参数,解决为空的问题。
  2. 循环逻辑修复:移除主程序中的stop语句,添加cycle跳过无效输入的后续处理,保证循环正常执行。
  3. 数组功能实现:补充数组扩展的子程序resize_array,将有效月份的数字和缩写存入对应数组,完成“构建对应数组”的需求。
  4. 冗余代码清理:移除未使用的变量(如my_month),优化代码结构。

内容的提问来源于stack exchange,提问作者Zilore Mumba

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.07 18:04:50