数组和派生类型 [英] Arrays and derived types

查看:111
本文介绍了数组和派生类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关我的新项目,我必须使用一个数组,而不是一个临时文件来存储用户信息。要做到这一点,我需要创建派生类型了。

不过,我不明白一个数组是什么,派生类型是什么,如何使用它们,他们可以做什么,以及其他一些基本的想法。
谁能给我有关数组和派生类型的一些信息?

我写了code他们,但我不知道它是正确写入。
如果任何人都可以检查这对我来说,我会AP preciate它。

下面是我的数组和派生类型:

 !派生类型
TYPE银行
  INTEGER :: acNumber,acChecks
  REAL :: acBlance,acRate
  CHARACTER :: acType * 1,acLName * 15,* acFName 15
END TYPE!排列
INTEGER,参数::的MaxRow,MaxColum = 7
INTEGER,DIMENSION(的MaxRow:MaxColum):: AccountData


解决方案

如果你是你有可能看到一个子程序接受10/15参数的FORTRAN编程。如果你想想看,这是疯了(他们是太多了,你跑交换他们的风险),你很快就会意识到,一些参数总是一起旅行。
这将是有意义的收拾他们携带周围的一切作为一个整体,不作为独立的实体一个单一的实体下。这将大大减少参数的个数,让您只负担找到适当的关联。这个单一的实体的类型是

在您的code,你说银行是这些信息的集合。现在,您可以声明该类型,这将重新present并提供访问的单一变量acNumber,acChecks等的具体变量。为了做到这一点,你必须使用%符号。所以,如果您的银行变量称为B,你可以说,例如

  B%acNumber = 5

您可以想像B,为衣柜,含有不同的货架上。您将关闭,所有的货架和他们的内容一起移动。

数组是一组相同类型的实体(比如,​​整数或字符(LEN = 1024),或银行),他们一个接一个,所以你可以用一个数字索引来访问它们。请记住,除非在1特别规定,在FORTRAN启动数组索引(在所有其他主要语言,第一个指标是零代替)

至于你的code,我建议你:


  •   INTEGER,DIMENSION(的MaxRow:MaxColum):: AccountData

      INTEGER :: AccountData(的MaxRow,MaxColum)

    是一样的,但是你少写。另请注意,使用之间的区别:和。如果要定义一个矩阵(你的情况),这是一个两维数组,你必须使用逗号。你写的是错的。


  • 有关的琴弦,它的更好,如果你写

     字符:: acType * 1,acLName * 15,* acFName 15

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

    在这种情况下,你写多了,但你的语法是pcated(我可能是错的,虽然)德$ P $
    此外,请记住它的更好,如果你写的类型,每行一个成员变量。这是一个品味的问题,但我preFER通过在每个成员变量一行看到一个类型的全尺寸。


  • 有关MAXROWS和MaxColumns,我想它们写成MAX_ROWS和MAX_COLUMNS。参数之类的东西,通过传统是非常恒定的标识与所有的资本,在任何主要语言下划线分隔的名称。



修改:回答您的评论,这里采用的是一个数组的一个例子

  $更多foo.f90
程序测试
    整数:: myArray的(10)    myArray的= 0!相当于归零由一个单一的元件中的一个
    myArray的(2)= 5
    myArray的(7)= 10    打印*,myarray中程序结束
$ G95 foo.f90 -o富
$ ./foo
 0 5 0 0 0 0 10 0 0 0

一个数组就像具有相同名称的多个变量,通过索引标识。非常有用的前preSS向量,或矩阵。
当然,你可以做你定义一个聚合类型的数组,而不是predefined类型(例如,整数)。

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

解决方案

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

这篇关于数组和派生类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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