Fortran001—— 输入输出和声明

1 read_write_print

program main   # main-name

implicit none      #表示关闭默认类型功能,所有变量都要事先声明 

read(*,*) fa,fb   #读入

write(*,*)  "hello world!"  #  写出

print * , "hello world! "   #打印出

stop (可省略) 

end

2 数据类型

program ex0404
    integer a            # 定义整数


    real a               # 定义浮点数
    real :: a 
    a = 3.14159/2.0
    real :: a,b,c 
    c =sin(a)**2 +cos(b)**2

    character a        # 定义字符串
    character first
    character second
    character add
    first = "happy"
    second = "birthday"
    add = first//second   # 两个除号连接字符串

    write(*,*) "a=",a
stop
end
3 格式控制符
program ex0421
    integer a 
    real b
    complex c
    logical d
    character(len=20) e
    a =10 
    b =12.34
    c = (1,2)
    d = .true.
    e = "fortran "
    write(*,"(1X,I5)")  a     # 用I 来格式化整数
    write(*,"(1X,F5.2)")  b   # 用F来格式化浮点数
    write(*,"(1X,F4.1,F4.1)") c # complex要用两个浮点数来输出
    write(*,"(1X,L3)") d   # 用L来输出logical
    write(*,"(1X,A10)") e  # 用A 来输出字符串

4 自定义数

program ex0434
implicit none
# 开始创建person类型
type :: person
    character :: name
    integer :: age
    real :: height
    real :: wight
    character :: address
end type person

type(person) :: a 

write(*,*) "name"
read(*,*)  a%name
write(*,*) "AGE"
read(*,*) a%age
write(*,*) "height"
read(*,*) a%height
write(*,*) "weight"
read(*,*) a%weight
write(*,*) "address"
read(*,*) a%adress

write(*,100) a%name, a%age, a%height, a%weight
100 format(/, "name:", A10/, "age:", I3/, "height:" , I3&
&"address:" , A50)


stop
end

猜你喜欢

转载自blog.csdn.net/xiaofeixiazyh/article/details/78493172