Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Современный Фортран_учебник

.pdf
Скачиваний:
31
Добавлен:
23.03.2015
Размер:
1.61 Mб
Скачать

72

3.% ! .

4.' , .

5.& # .

6.6 ( ),

+ Agrapher.

7.2-# .

8.# # .

9.! Out.txt.

' * ! ! .

! "

1.' + , 5.

2.Container – . -

, +* 3 , -

.

3.ProModule – . ! -

, +* 3 ! .

4.!

+ ! .

5.! +

Interface.

#

!: in.txt;

out.txt ! + .-

.

!: ! ! -

, ! in.txt. ' ! # 3 .

Container

6 Container – ! -

. Container -

Subroutine OneArray. ' Container -

# OneArray. ! ! -

dArr, +* . $ +

OneArray.

' ARR, NumARR ! OneArray. (-

ARR OneArray. 1 -

dARR # . / allocate deallocate – *-

# #.

 

73

 

 

Program Container

!

Implicit none

 

Integer dARR

! ARR

Open(1,File='In.txt') ! %

Open(2,File='Out.txt')! %

Read(1,*)dARR; Call OneArray('B') ! dARR - 'B' Read(1,*)dARR; Call OneArray('C') ! dARR - 'C'

contains

Subroutine OneArray(ArrName) !

Implicit none

character,intent(in)::ArrName ! $ “ ” Real,dimension(1:dARR)::ARR !

Integer::NumARR ! max

Read(1,*)ARR; Write(2,2)ArrName,ARR ! / ARR

NumARR=sum(maxloc(Abs(ARR)))

Write(2,3) ARR(NumARR), NumARR, ArrName 2 format(/' Массив ', a/ ( 6F8.1) )

3 format(' max по модулю элемент ', F8.1, & ' его номер', i3,' в массиве ', a)

End Subroutine OneArray !

End Program Container

Container

Массив В

 

 

 

 

 

1.0

-4.0

6.0

20.0

45.0

-71.0

4.0

-17.0

.0

.0

.0

11.0

max по модулю элемент

-71.0 его номер

6 в массиве B

Массив С

 

 

 

 

 

-122.0

.0

45.0

71.0

4.0

-17.0

.0

.0

.0

11.0

 

 

max по модулю элемент

-122.0 его номер

1 в массиве C

ProModule – # $ ".

. ! – !

Mo ForModule, :

1.! ! ! !,

! ! ! ;

2.! , ! !

.

" ! Mo ! + ProArray, - * . ' ProArray – $ .

! ! .

74

3, ARR, * . 6 -

forModule, ! +* !, 3 ArrN, dARR (public) ! (private) NumARR -

! ProArray forModule. " ARR ProArray, -

! * .

Module Mo ! 5. 9$ 7 & , &

Implicit none

Integer,public::ArrN,dARR ! & ,

Integer,private::NumARR ! : max , Contains !

Subroutine ProArray

Implicit none

Real,dimension(1:dARR)::ARR ! .

Read(1,*)ARR; Write(2,2)ArrN,ARR ! ARR

2 format(/’ Массив №’, i5/ ( 6F8.1) ) NumARR=sum(maxloc(Abs(ARR))) Write(2,3) ARR(NumARR), NumARR, ArrN

3 format(’ max + . ’,F8.1, & ’ ’, i3, ’ 7’,i5)

End Subroutine proArray end Module Mo

Program forModule

use Mo ! Mo, : ArrN, dARR , proArray

Open(1,file='in.txt');

Open(2,file='Out.txt')!%: ,

do ArrN=1,10000

! 10000

 

Read(1,*, end=10) dARR;

Call ProArray

 

enddo

 

 

 

 

 

 

10 Write(2,*)

’ ’

 

 

End Program forModule

 

 

 

 

forModule

 

 

Массив №

1

 

 

 

 

 

1.0

-4.0

6.0

20.0

45.0

-71.0

 

4.0

-17.0

.0

.0

.0

11.0

 

max по модулю элемент

-71.0 его номер

6 в массиве №

1

Массив №

2

 

 

 

 

 

22.0

.0

45.0

71.0

4.0

-17.0

 

.0

.0

.0

11.0

 

 

 

max по модулю элемент

71.0 его номер

4 в массиве №

2

Все данные прочитаны

 

 

 

 

$ ! %

1. 0 .

75

2.0 .

3.0 .

4.- ?

5.- ?

6./ # ! ! ? / ! ?

7." 3 !

?

8.- ! ?

9./ # # ? '.

10.& # 3 , .

11./ 3 , ! +* ?

12./ , , ?

13.0 interface ! ?

14./ ! ?

# $& !

! :

1.+ function;

2.subroutine;

3.– + ;

4.– + .

$' !

! # . 2.6.

3. 0 ! &

3.1. 1 &

' + (plain) :

«!» – ;

* + (

+ # );

, ,

, , #

+* + ( «&»);

+ : *.f90, *.f03 – $ % # , *.for - % % (! 77).

76

 

 

 

% 15.

 

'

 

 

 

 

1

 

Program FreeForm ! :

Program - Начало программы

2

 

! 5. & & &$

строка-комментарий

3

 

Real R; integer i ! комментарий

Два простых оператора в строке

4

 

Do i=1,10 ! 10

Do - Начало конструкции

5

 

Write(*,*)’ R=?’; Read(*,*) R

начало блока

6

 

Write(*,*)’R =’, R, & ! & -

с переносом по &

7

 

’ S =’, 3.14* R**2

строка-продолжение

8

 

End do

End+Do = конец конструкции Do

9

 

End Program FreeForm

End+Program = конец программы

 

 

 

 

' *

# – <Enter> «;» :

! «;», ( 3, 5)

+ ;

<Enter> ( 1-9),

( 2-8), ( 4-8) do ( 5-7);

! # .

# ' ! –

, !

«;», . %, #

' !, + + + -

.

' %.15 !, ! R

Real R, Real,dimension(1:10) :: R. .

. R,

, # # – ! :

Program R10 ! : 10

Real,dimension(1:10):: R

Write(*,*)’ R=?’; Read(*,*) R ! 10

Write(*,*)’ R =’,R; Write(*,*)’ S =’,3.14*R**2

End Program R10

3.2.! ,

/. : , -

, , , .

"

+ , , , ,

#, , , , +, . (-

+* #:

77

# , 0÷9, «_», «$»;

! ! «$»;

+;

, , ,

! # ;

31 .

& 3 , # # -

(s=v*t), ! : Space=Velocity*Time. 3 ( – 8.5) – . , !-

, , # , -

, # . ' , :

.true. + + ;

1:99999 ! + ( format).

+ # # $ : -

, . / + ! ! #

, ! . ' -

! ! + – . ' + max

Real :: a=2, b=3, c, max ! max –

max=a; C = max(a,b) ! max()- %

.$ : inconsistent usage of MAX - # * ! max.

'

3 ! ! ,

! 3.14 ! , Pi. '-

– . 3, !.

! # :

-: integer, * real, complex;

-logical;

-(!) character.

% 16. ' #

1

# -

, !

#

'

!

 

 

 

 

 

 

 

integer

–2147

 

integer*1 *2 *4 ±1010

%

 

-1.76

1e7 1.2e7

real* 4 10±38

7

0 -

Real

1.3D+123

real* 8 10±308

17

, -

 

1.2Q-1234

real*16 10± 4932 33

 

 

 

 

 

Complex

(-1.76 , 1)

' * #

0

 

 

 

 

 

Logical

.true. .false.

Logical*1 *2 *4

 

1

 

 

 

 

 

character

‘ =’

“O’K”

character*1 *32767

/ -

 

 

ANSI

 

 

 

 

 

 

 

 

 

 

78

/ ! # , -

# '/, 3, , '/. 8 parameter, #

: real,parameter :: Pi =3.141593.

– ,

. -

; 3 +, -

,

*. # , -

+ ! ! , .

# !:

, , . ;

, ;

#, , cos( -0.2) ;

, , ;

! , , x+y+z ;

( ), # # .

3, # * # , +:

% ( ) !

+ , * -

, ! ;

!: -

+ , -

– ;

! +

, s//p

%

& % ( ) ! -, * .

% 17. +

 

#

#

#

 

1

sin(X)

( )

 

 

 

 

!

2

X**2

! –

 

 

 

 

 

3

–X

( )

 

 

 

 

(,

4

X/Y, X*Y

! –

 

 

 

 

,

5

X+Y, X-Y

! –

 

 

 

 

5 # - !, , -

, + .

, ! ! . - -

79

! . 1 , -

+ ; + ! . -

+ .

!+ + # –

# * # . (

+ , - *!+ * # .

, ! . 1 -

, . !. (

# !

. " + :

– *1 *2 *4 ;

– * - *4 *8 *16 ;

– - *4 *8 *16 .

& , # # -

* :

* # ! – * -

, , 2./3.=0.666667;

# ! – ,

, , 8/3=2 2/3=0;

# ! – *

, , 2./3=>2./3.=0.6666667 2/3.=>2./3.=0.666667 .

& ! an ax :

integer n a**n n- -

a*a*a* ..*a, . a**n=0 n<0 integer a, , 3**(-2)=1/3**2=1/9=0;

real x a**x a x = eln a x = e x ln a , -

!, ! a > 0 ;

* a,b

5 ! a=r+θi -

. ! , Ln – .

, ! , ! -

.

' ! &(5, -

! # !:

! x/0;

! 0/0, 0.0/0.0, 0/0.0, 0.0/0;

+ ! + -

! 0**0 0**(-2);

! *- + !, (-8)**(1./3), -(8**(1./3)) 3.

80

&

& ! .

! –

( ). «//», s=s1//s2,

s + 1,2,…len(s), len (s1//s2)=len(s1)+len(s2). ' - !

. ': ! ab = ab(14:26)//ab(1:13)

, + :

Character*26 :: ab=’abcdefghijklmnopqrstuvwxyz’

: ab=’nopqrstuvwxyzabcdefghijklm

& , s1=s1//s2 s1

“=” ! . 0, -

len(s1). + +

, , , .

s1=trim(s1)//s2 , s1 # # ,

s2. trim (s1) # .

'

« » «!», :

# #;

# ;

, + # -

, ! # .

! – .true.« »,

.false.– «!».

% 18. !

 

90,

77,

 

"%

 

 

 

 

! A<b

a<b

a .lt. b

 

 

 

! Ab

a<=b

a .le. b

 

 

 

A=b

k==m

a .eq. b

 

 

 

Ab

k/=m

a .ne. b

 

 

 

! Ab

a>=b

a .ge. b

 

 

 

! a>b

a>b

a .gt. b

 

 

 

& + # . 0,

A=2, B=0 A+3 >B :

,+3 5; 5 !,

0; ! 5>0 – « ».

+

% 19.

81

! #

 

!

 

#

 

 

 

 

& 01

.not.

1

.not.a

 

 

 

 

/ 3+

.and.

2

a.and.b

 

 

 

 

( 3+

.or.

3

a.or.b

 

 

 

 

$!

.eqv.

4

a.eqv.b

 

 

 

 

0 . !

.neqv.

4

a.neqv.b

& .not.a , (–x), ( ) -

, ! # +

. ( ! -

, ! -

. 0, a.and. .not.b.

& .not. – ! « », -

«!», ! «!», « -».

: T – « » F – «!».

% 20.

% #

 

.AND.

F

T

 

 

F

F

F

 

 

T

F

T

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

.EQV.

 

F

T

 

 

F

 

T

F

 

 

T

 

F

T

 

 

 

 

 

 

 

 

 

.NOT.

 

F

T

 

 

 

 

 

 

 

 

 

 

 

 

 

T

F

 

 

 

 

 

 

 

 

 

 

.OR.

F

T

 

F

F

T

 

T

T

T

 

 

 

 

 

 

 

 

 

 

.NEQV.

F

T

 

F

F

T

 

T

T

F

 

,

! #

' : a>3 .and. a<5. ,

, ! (3,5).

+ +* : -

; ; .

3.3.

& : =

+:

, ! , . +

, # !

, , . / ! -