gpt4 book ai didi

character-encoding - Fortran 字符串数组

转载 作者:行者123 更新时间:2023-12-04 16:49:25 32 4
gpt4 key购买 nike

<分区>

我在填充字符串数组时遇到问题。使用 [] 声明分配元素时出现问题在一个数组中。当我为数组元素分配我的元素时,一切很好。

这是输出

Expected Output: redgreenyellowblue
Actual output: redgreyelblue

这是代码

Character (len=*), Intent(in), Optional :: a2,     &
a3, a4, a5, a6, a7, a8

Character (len=65), Allocatable :: str(:)
Character (len=65) :: b(512)

a1 = "red"
a2 = "green"
a3 = "yellow"
a4 = "blue"
a5 = "magenta"
a6 = "cyan"
a7 = "white"

Allocate (str(7))

str = [a1, a2, a3, a4, a5, a6, a7]

str(4) = a4
str(5) = a5
str(6) = a6
str(7) = a7

Write (*,*) "Expected output: ", Trim(a1), Trim(a2), Trim(a3), Trim(a4)
Write (*,*) "Actual output: ", Trim(str(1)), Trim(str(2)), Trim(str(3)), Trim(str(4))

我确认以下工作按预期进行

Character (len=65), Allocatable :: str(:)
Character (len=65) :: a1, a2, a3, a4, a5, a6, a7

a1 = "red" ; a2 = "green"; a3 = "yellow"; a4 = "blue"
a5 = "magenta"; a6 = "cyan"; a7 ="white"

Allocate (str(7))
str = [a1,a2,a3,a4,a5,a6,a7]
Write (*,*) Trim(a1), Trim(a2), Trim(a3), Trim(a4), Trim(a5)
Write (*,*) Trim(str(1)), Trim(str(2)), Trim(str(3)), Trim(str(4)), Trim(str(5))
Deallocate (str)

解决方法是在构造函数中包含字符长度参数

str = [Character(len=65) :: a1,a2,a3,a4,a5,a6,a7]

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com