数组和派生类型

发布于 2024-07-17 11:34:57 字数 489 浏览 2 评论 0原文

对于我的新项目,我必须使用数组而不是临时文件来存储用户的信息。 为此,我还需要创建派生类型。

但是,我还没有理解什么是数组,什么是派生类型,如何使用它们,它们可以做什么,以及其他一些基本概念。 谁能给我一些有关数组和派生类型的信息?

我为他们编写了代码,但我不知道它是否正确编写。 如果有人可以帮我检查一下,我将不胜感激。

这是我的数组和派生类型:

! derived type
TYPE Bank
  INTEGER :: acNumber, acChecks
  REAL :: acBlance, acRate
  CHARACTER :: acType*1, acLName*15, acFName*15
END TYPE

! array
INTEGER, PARAMETER :: MaxRow, MaxColum = 7
INTEGER, DIMENSION(MaxRow:MaxColum) :: AccountData

For my new project, I have to use an array instead of a scratch file to store information from users. To do this, I need to create derived types, too.

However, I haven't understood what an array is and what a derived type is, how to use them, what they can do, and some other basic ideas.
Can anyone give me some information about array and derived types?

I wrote code for them, but I don't know it is written correctly.
If anyone can check this for me, I would appreciate it.

Here are my array and derived types:

! derived type
TYPE Bank
  INTEGER :: acNumber, acChecks
  REAL :: acBlance, acRate
  CHARACTER :: acType*1, acLName*15, acFName*15
END TYPE

! array
INTEGER, PARAMETER :: MaxRow, MaxColum = 7
INTEGER, DIMENSION(MaxRow:MaxColum) :: AccountData

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

幻梦 2024-07-24 11:34:57

如果您是一名 Fortran 程序员,您可能见过接受 10/15 参数的子例程。 如果你仔细想想,这太疯狂了(它们太多了,你冒着交换它们的风险),你很快就会意识到有些争论总是在一起进行。
将它们打包在一个单一实体下是有意义的,该实体将所有内容作为一个整体而不是作为独立实体进行携带。 这将大大减少参数的数量,只给你寻找适当关联的负担。 这个单一实体就是类型。

在您的代码中,您说银行是这些信息的集合。 您现在可以声明该类型的具体变量,它将表示并提供对单个变量 acNumber、acChecks 等的访问。 为此,您必须使用% 符号。 因此,如果您的银行变量称为 b,您可以说,例如

b%acNumber = 5

您可以将 b 想象成一个壁橱,包含不同的架子。 当你移动关闭的东西时,所有的架子和里面的东西都会一起移动。

数组是一组相同类型的实体(例如整数、字符(len=1024)或银行),它们是一个接一个的,因此您可以使用数字索引访问它们中的每一个。 请记住,除非另有指定,否则 Fortran 中的数组索引从 1 开始(在所有其他主要语言中,第一个索引为零)

至于您的代码,我建议您:

  • write

     INTEGER, DIMENSION(MaxRow:MaxColum) :: AccountData  
      

    作为

     INTEGER :: AccountData(MaxRow,MaxColum) 
      

    都是一样的,只是你写的少了。 另请注意,使用 : 和 , 之间存在差异。 如果你想定义一个矩阵(你的情况),它是一个二维数组,你必须使用逗号。 你写的不对。

  • 对于字符串,最好写成

    <预><代码> 字符 :: acType*1、acLName*15、acFName*15

    作为

     CHARACTER(LEN=1) :: acType 
       字符(LEN=15)::acLName 
       字符(LEN=15)::acFName 
      

    在这种情况下,你写了更多,但你的语法已被弃用(不过我可能是错的)
    另外,请记住,最好在类型中每一行编写一个成员变量。 这是一个品味问题,但我更喜欢通过每个成员变量一行来查看类型的完整大小。

  • 对于 MaxRows 和 MaxColumns,我会将它们写为 MAX_ROWS 和 MAX_COLUMNS。 传统上高度恒定的参数和内容在任何主要语言中都用全大写、下划线分隔的名称来标识。


编辑:为了回答您的评论,这里是一个使用数组的示例,

$ more foo.f90 
program test
    integer :: myarray(10)

    myarray = 0   ! equivalent to zeroing the single elements one by one
    myarray(2) = 5
    myarray(7) = 10

    print *, myarray

end program
$ g95 foo.f90 -o foo
$ ./foo
 0 5 0 0 0 0 10 0 0 0

数组就像多个具有相同名称的变量,由索引标识。 对于表达向量或矩阵非常有用。
您当然可以创建您定义的聚合类型的数组,而不是预定义类型(例如整数)。

If you are a fortran programmer you have probably seen a subroutine accepting 10/15 arguments. If you think about it, it's insane (they are too many, you run the risk of swapping them) and you quickly realize that some arguments always travel together.
It would make sense to pack them under a single entity that carries everything around as a whole, non as independent entities. This would reduce the number of arguments considerably, giving you only the burden to find proper association. This single entity is the type.

In your code, you say that a Bank is an aggregate of those informations. You can now declare a concrete variable of that type, which will represent and provide access to the single variables acNumber, acChecks and so on. In order to do so, you have to use the % symbol. so if your bank variable is called b, you can say for example

b%acNumber = 5

You can imagine b as a closet, containing different shelves. You move the closed, all the shelves and their content move together.

An array is a group of entities of the same type (say, integer, or Character(len=1024), or Bank) and they are one after another so you can access each of them with a numeric index. Remember that, unless specified differently, arrays indexes in fortran start at 1 (in all the other major languages, the first index is zero instead)

As for your code, I suggest you to:

  • write

     INTEGER, DIMENSION(MaxRow:MaxColum) :: AccountData 
    

    as

     INTEGER :: AccountData(MaxRow,MaxColum)
    

    it is the same, but you write less. Please also note that there is a difference between using the : and the ,. If you want to define a matrix (your case), which is a two-dimension array, you have to use the comma. What you wrote is wrong.

  • for the strings, it's better if you write

     CHARACTER :: acType*1, acLName*15, acFName*15
    

    as

     CHARACTER(LEN=1) :: acType
     CHARACTER(LEN=15) :: acLName
     CHARACTER(LEN=15) :: acFName
    

    in this case, you write more, but your syntax is deprecated (I could be wrong, though)
    Also, remember that it's better if you write one member variable per line in the types. It's a matter of taste, but I prefer to see the full size of a type by having one line per member variable.

  • For MaxRows and MaxColumns, I would write them as MAX_ROWS and MAX_COLUMNS. Parameters and stuff that is highly constant by tradition is identified with an all capital, underscore separated name in any major language.


Edit: to answer your comment, here is an example of the use of an array

$ more foo.f90 
program test
    integer :: myarray(10)

    myarray = 0   ! equivalent to zeroing the single elements one by one
    myarray(2) = 5
    myarray(7) = 10

    print *, myarray

end program
$ g95 foo.f90 -o foo
$ ./foo
 0 5 0 0 0 0 10 0 0 0

an array is just like multiple variables with the same name, identified by an index. Very useful to express vectors, or matrices.
You can of course do an array of an aggregated type you define, instead of a predefined type (eg. integer).

你的往事 2024-07-24 11:34:57

数组是变量的有序列表,所有变量都具有相同的类型,并按整数索引。 请参阅 Wikipedia 中的数组 请注意,在 Fortran 中数组索引更加灵活与大多数其他低级语言相比,您可以拥有一个由下限、上限和跨度组成的索引三元组,而不是每个维度只有一个索引。 在这种情况下,表达式的左值是一个子数组,而不是数组类型的单个元素。

派生类型是用户定义的复合类型,它由多个不同类型的组件组成。 在其他一些语言中,这些被称为结构、结构类型或记录类型。 请参阅维基百科中的记录

您还可以创建一个数组派生类型,或者您可以拥有一个派生类型,其中一个或多个组件本身就是数组,或者就此而言,其他派生类型。 由你决定!

检查代码的最简单方法是尝试编译它。 通过编译器当然不能保证程序按预期工作,但这肯定是必需的步骤。

An array is an ordered list of variables, all of the same type, indexed by integers. See Array in Wikipedia Note that in Fortran array indexing is more flexible than most other low level languages, in that instead of a single index per dimension, you can have an index triplet consisting of lower bound, upper bound, and stride. In that case the lvalue of the expression is a subarray rather than a single element of the array type.

A derived type is a composite type defined by the users, which is made up of multiple components which can be of different types. In some other languages these are knows as structs, structure types, or record types. See Record in Wikipedia

You can also make an array of a derived type, or you can have a derived type where one or more components are themselves arrays, or for that matter, other derived types. It's up to you!

The easiest way to check your code is to try to compile it. Making it past the compiler is of course no guarantee that the program works as expected, but it certainly is a required step.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文