计算数学精解【13】-fortran计算精解(2)

发布于:2024-08-14 ⋅ 阅读:(120) ⋅ 点赞:(0)

read,write

读写文件

program learn
    implicit none
    character(10)::student_name
    character(20)::file_name
    integer::id
    integer::io_status
    character(100)::err_msg
    real::math_score,english_score,politics_score

    write(*,*) "请输入文件名:"
    read (*,*)  file_name
    write(*,*) "请输入数据(学号,姓名,数学成绩,英语成绩,政治成绩):"
    read (*,*) id,student_name,math_score,english_score,politics_score

    open (unit=11,file=file_name,status='replace',action='write',iostat=io_status,iomsg=err_msg)
    if (io_status/=0)  then
        write(*,*) "写入文件",file_name,"异常"
    else
        write (11,100) id,student_name,math_score,english_score,politics_score
        close(unit=11)
    endif


    open (unit=10,file=file_name,status='old',action='read',iostat=io_status,iomsg=err_msg)
    if (io_status/=0)  then
        write(*,*) "读取文件",file_name,"异常"
    else
        read (10,100) id,student_name,math_score,english_score,politics_score
        write (*,100) id,student_name,math_score,english_score,politics_score
        close(unit=10)
    endif
    100 format(I5,A10,3F6.2)


end program learn

录入与读取数据

 请输入文件名:
test.dat
 请输入数据(学号,姓名,数学成绩,英语成绩,政治成绩):
1,ads,dsf,adsf,adf,adsf
 请输入数据(学号,姓名,数学成绩,英语成绩,政治成绩):
2,asfd,asdf,adsf,345
 请输入数据(学号,姓名,数学成绩,英语成绩,政治成绩):
3,张三,98.32,68.88,91.34
 录入成功!
 继续录入吗?(y/n)
n
    3张三       98.32 68.88 91.34

Process returned 0 (0x0)   execution time : 35.250 s
Press any key to continue.
program learn
    implicit none
    character(10)::student_name
    character(20)::file_name
    character(1)::is_continue_input,ans
    integer::id
    integer::io_status
    integer::read_status
    integer::write_status
    character(100)::err_msg
    real::math_score,english_score,politics_score

    write(*,*) "请输入文件名:"
    read (*,*)  file_name

    open (unit=11,file=file_name,status='replace',action='write',iostat=io_status,iomsg=err_msg)
    if (io_status/=0)  then
        write(*,*) "写入文件",file_name,"异常:",err_msg
    else
        write_file:do
            write(*,*) "请输入数据(学号,姓名,数学成绩,英语成绩,政治成绩):"
            read (*,*,iostat=read_status) id,student_name,math_score,english_score,politics_score
            if (read_status/=0) cycle
            write(11,100,iostat=write_status) id,student_name,math_score,english_score,politics_score
            if (write_status==0) write (*,*) "录入成功!"
            write (*,*) "继续录入吗?(y/n)"
            read(*,*) is_continue_input
            if (is_continue_input=='n' .or. is_continue_input=='N') exit
        end do write_file
        close(unit=11)
    endif


    open (unit=10,file=file_name,status='old',action='read',iostat=io_status,iomsg=err_msg)
    if (io_status/=0)  then
        write(*,*) "读取文件",file_name,"异常:",err_msg
    else
        read_file:do
            read (10,100,iostat=read_status) id,student_name,math_score,english_score,politics_score
            if (read_status/=0) exit
            write (*,100) id,student_name,math_score,english_score,politics_score
        end do read_file
        close(unit=10)
    endif
    100 format(I5,A10,3F6.2)


end program learn

文件定位

  • rewind 从文件头开始
 请输入文件名:
test.dat
 请输入数据(学号,姓名,数学成绩,英语成绩,政治成绩):
1,张三,87.34,69.49,73.29
 录入成功!
 继续录入吗?(y/n)
y
 请输入数据(学号,姓名,数学成绩,英语成绩,政治成绩):
2,李四,88.32,76.33,93.16
 录入成功!
 继续录入吗?(y/n)
n
 请输入学号:
1
    1张三       87.34 69.49 73.29
 请输入学号:
2
    2李四       88.32 76.33 93.16
 请输入学号:
1
    1张三       87.34 69.49 73.29
 请输入学号:
1
    1张三       87.34 69.49 73.29
 请输入学号:
2
    2李四       88.32 76.33 93.16
 请输入学号:
2
    2李四       88.32 76.33 93.16
 请输入学号:
3
 找不到该学生的记录
 请输入学号:
1
    1张三       87.34 69.49 73.29
 请输入学号:
2
    2李四       88.32 76.33 93.16
 请输入学号:
3
 找不到该学生的记录
 请输入学号:
99
 找不到该学生的记录
 请输入学号:
-1
program learn
    implicit none
    character(10)::student_name
    character(20)::file_name
    character(1)::is_continue_input,ans
    logical::data_is_finded
    integer::id
    integer::io_status
    integer::read_status
    integer::write_status
    integer::search_id
    character(100)::err_msg
    real::math_score,english_score,politics_score

    write(*,*) "请输入文件名:"
    read (*,*)  file_name

    open (unit=11,file=file_name,status='replace',action='write',iostat=io_status,iomsg=err_msg)
    if (io_status/=0)  then
        write(*,*) "写入文件",file_name,"异常:",err_msg
    else
        write_file:do
            write(*,*) "请输入数据(学号,姓名,数学成绩,英语成绩,政治成绩):"
            read (*,*,iostat=read_status) id,student_name,math_score,english_score,politics_score
            if (read_status/=0) cycle
            write(11,100,iostat=write_status) id,student_name,math_score,english_score,politics_score
            if (write_status==0) write (*,*) "录入成功!"
            write (*,*) "继续录入吗?(y/n)"
            read(*,*) is_continue_input
            if (is_continue_input=='n' .or. is_continue_input=='N') exit
        end do write_file
        close(unit=11)
    endif


    open (unit=10,file=file_name,status='old',action='read',iostat=io_status,iomsg=err_msg)
    if (io_status/=0)  then
        write(*,*) "读取文件",file_name,"异常:",err_msg
    else
        search_data:do
            data_is_finded=.false.
            write(*,*) "请输入学号:"
            read(*,*) search_id
            if (search_id<=0) exit
            rewind(unit=10) !从文件头开始
            find_id:do
                read (10,100,iostat=read_status) id,student_name,math_score,english_score,politics_score
                if (read_status/=0) exit
                if (id==search_id) then
                    data_is_finded=.true.
                    write (*,100) id,student_name,math_score,english_score,politics_score
                endif
            end do find_id
            if (.not. data_is_finded) write(*,*) "找不到该学生的记录"

        end do search_data

        close(unit=10)
    endif
    100 format(I5,A10,3F6.2)


end program learn

  • backspace 回退一条记录
请输入文件名:
test.dat
 请输入数据(学号,姓名,数学成绩,英语成绩,政治成绩):
1,张三,66,77,88
 录入成功!
 继续录入吗?(y/n)
y
 请输入数据(学号,姓名,数学成绩,英语成绩,政治成绩):
2,李四,88,99,77
 录入成功!
 继续录入吗?(y/n)
y
 请输入数据(学号,姓名,数学成绩,英语成绩,政治成绩):
3,王五,72,83,91
 录入成功!
 继续录入吗?(y/n)
n
    1张三       66.00 77.00 88.00
    2李四       88.00 99.00 77.00
    3王五       72.00 83.00 91.00
    3王五       72.00 83.00 91.00
    2李四       88.00 99.00 77.00
    1张三       66.00 77.00 88.00

Process returned 0 (0x0)   execution time : 40.113 s
Press any key to continue.
program learn
    implicit none
    character(10)::student_name
    character(20)::file_name
    character(1)::is_continue_input
    integer::record_count
    integer::id
    integer::io_status
    integer::read_status
    integer::write_status
    character(100)::err_msg
    real::math_score,english_score,politics_score

    record_count=0
    write(*,*) "请输入文件名:"
    read (*,*)  file_name

    open (unit=11,file=file_name,status='replace',action='write',iostat=io_status,iomsg=err_msg)
    if (io_status/=0)  then
        write(*,*) "写入文件",file_name,"异常:",err_msg
    else
        write_file:do
            write(*,*) "请输入数据(学号,姓名,数学成绩,英语成绩,政治成绩):"
            read (*,*,iostat=read_status) id,student_name,math_score,english_score,politics_score
            if (read_status/=0) cycle
            write(11,100,iostat=write_status) id,student_name,math_score,english_score,politics_score
            if (write_status==0) write (*,*) "录入成功!"
            write (*,*) "继续录入吗?(y/n)"
            read(*,*) is_continue_input
            if (is_continue_input=='n' .or. is_continue_input=='N') exit
        end do write_file
        close(unit=11)
    endif


    open (unit=10,file=file_name,status='old',action='read',iostat=io_status,iomsg=err_msg)
    if (io_status/=0)  then
        write(*,*) "读取文件",file_name,"异常:",err_msg
    else
        read_file:do
            read (10,100,iostat=read_status) id,student_name,math_score,english_score,politics_score
            if (read_status/=0) exit
            write (*,100) id,student_name,math_score,english_score,politics_score
            record_count=record_count+1
        end do read_file
        backspace(unit=10)
        back_read_file:do
            backspace(unit=10)
            read (10,100,iostat=read_status) id,student_name,math_score,english_score,politics_score
            if (read_status/=0  .or. record_count<=0) exit
            write (*,100) id,student_name,math_score,english_score,politics_score
            backspace(unit=10)
            record_count=record_count-1
        end do back_read_file
        close(unit=10)
    endif
    100 format(I5,A10,3F6.2)


end program learn

csv

下载鸢尾属植物机器学习数据集
http://archive.ics.uci.edu/dataset/53/iris
数据集包含3类,每个类包含50个实例,每个类表示一种鸢尾属植物。一类与另一类是线性可分离的;后者彼此之间不是线性可分离的。

program learn
    implicit none
    character(20)::class_name
    character(200)::file_name
    integer::record_count=0
    integer::io_status
    integer::read_status
    integer::path_pos
    character(200)::err_msg
    real::sepal_length,sepal_width,petal_length,petal_width

    character(len=255) :: cmd
    call get_command(cmd)
    path_pos=index(cmd, '\' , .true.)
    file_name=trim(cmd(1:path_pos))//"iris\iris.data"

    open (unit=10,file=file_name,status='old',action='read',iostat=io_status,iomsg=err_msg)
    if (io_status/=0)  then
        write(*,90) file_name,err_msg
        90 format ('读取iris文件',A200,'异常:',A200)
    else
        write (*,*) "萼片长度,萼片宽度,花瓣长度,花瓣宽度,分类"
        read_file:do
            read (10,*,iostat=read_status) sepal_length,sepal_width,petal_length,petal_width,class_name
            if (read_status/=0) exit
            write (*,100) sepal_length,sepal_width,petal_length,petal_width,class_name
            record_count=record_count+1
        end do read_file
        close(unit=10)
        write (*,*) "共读取",record_count,"条数据!"
    endif
    100 format(4(F5.1,1X),A50)


end program learn


...
...
  6.7   3.0   5.2   2.3                               Iris-virginica
  6.3   2.5   5.0   1.9                               Iris-virginica
  6.5   3.0   5.2   2.0                               Iris-virginica
  6.2   3.4   5.4   2.3                               Iris-virginica
  5.9   3.0   5.1   1.8                               Iris-virginica
 共读取         150 条数据!

数组

一维数组

program hello
    implicit none
    integer,dimension(5)::x=[1,2,3,4,5]
    integer,dimension(5)::b=[11,22,33,44,55]
    real::a=6.2
    real,dimension(5)::y
    y=x*a+b
    write(*,*) y
end program
   17.2000008       34.4000015       51.5999985       68.8000031       86.0000000

Process returned 0 (0x0)   execution time : 0.032 s
Press any key to continue.

最小二乘法

image
image

program hello
    implicit none
    real,dimension(5)::x=[10,20,30,40,50]
    real,dimension(5)::b=[10,15,12,13,9]
    real::a=16.29
    real,dimension(5)::y
    real::x_mean
    real::y_mean
    real::a_predict2
    real::a_predict1
    real::b_predict,a_predict
    y=a*x+b
    x_mean=sum(x)/5
    y_mean=sum(y)/5
    a_predict2=sum((x-x_mean)**2)
    a_predict1=sum(((x-x_mean)*(y-y_mean)))
    a_predict=a_predict1/a_predict2
    b_predict=y_mean-a*x_mean
    write(*,*) 'a=',a_predict
    write(*,*) 'b=',b_predict
end program


下标

program learn
    implicit none
    real,dimension(5)::a=[10,20,30,40,50]
    integer,dimension(5)::b=[1,2,3,4,5]
    integer::i
    write (*,*) [(i,i=1,3)]
    write (*,*) (i,i=3,20,4)
    write(*,*) a(2:4)
    write(*,*) b(1),b(4)
    write(*,*) b([1,5])
    write(*,*) b([(i,i=1,5,2)])


end program learn

           1           2           3
           3           7          11          15          19
   20.0000000       30.0000000       40.0000000
           1           4
           1           5
           1           3           5

Process returned 0 (0x0)   execution time : 0.034 s
Press any key to continue.

隐式循环

program learn
    implicit none
    integer i,j
    write (*,100) ((i,j,j=1,9),i=1,9)
    100 format (I5,1X,I5)
    write (*,110) ((i,j,i*j,j=1,9),i=1,9)
    110 format (I5,'*',I5,'=',I10)

end program learn

...
    9     4
    9     5
    9     6
    9     7
    9     8
    9     9
    1*    1=         1
    1*    2=         2
    1*    3=         3
    1*    4=         4
    1*    5=         5
    1*    6=         6
    1*    7=         7
    1*    8=         8
	...
	program learn
    implicit none
    integer,dimension(2,3)::a
    integer,dimension(2,3)::b
    a=reshape([1,66,89,2,74,79],[2,3])
    b=reshape([1,76,99,2,84,59],[2,3])
    write (*,100) a
    write (*,100) b
    100 format (3I5)

end program learn

    1   66   89
    2   74   79
    1   76   99
    2   84   59

Process returned 0 (0x0)   execution time : 0.172 s
Press any key to continue.

关系代数基本运算

笛卡尔积

aid a1 a2bid b1 b2
  1 66 74  1 76 84
  2 89 79  1 76 84
  1 66 74  2 99 59
  2 89 79  2 99 59

Process returned 0 (0x0)   execution time : 0.167 s
Press any key to continue.
program learn
    implicit none
    integer,dimension(2,3)::a
    integer,dimension(2,3)::b
    integer::i,j
    a=reshape([1,2,66,89,74,79],[2,3])
    b=reshape([1,2,76,99,84,59],[2,3])
    write (*,120) 'aid','a1','a2','bid','b1','b2'
    write (*,110) ((a(i,:),b(j,:),i=1,2),j=1,2)
    110 format (6(I3))
    120 format (6(A3))
end program learn



aid a1 a2bid b1 b2
  1 66 74  1 76 84
  2 89 79  1 76 84
  1 66 74  2 99 59
  2 89 79  2 99 59
 ---------
  2 89 79  1 76 84
 ---------
 66 89 66 89
program learn
    implicit none
    integer,dimension(2,3)::a
    integer,dimension(2,3)::b
    integer,dimension(24)::c
    integer,dimension(4,6)::d
    integer::i,j
    integer::k
    a=reshape([1,2,66,89,74,79],[2,3],order=[1,2])
    b=reshape([1,2,76,99,84,59],[2,3],order=[1,2])
    write (*,120) 'aid','a1','a2','bid','b1','b2'
    c=[((a(i,:),b(j,:),i=1,2),j=1,2)]
    d=reshape(c,[4,6],order=[2,1])
    write (*,110) c
    write  (*,*) "---------"
    write (*,110) d(2,:)
    write  (*,*) "---------"
    write (*,110) d(:,2)
    110 format (6(I3))
    120 format (6(A3))
end program learn



投影+选择

program learn
    implicit none
    integer,dimension(2,3)::a
    integer,dimension(2,3)::b
    integer,dimension(24)::c
    integer,dimension(4,6)::d
    integer::i,j
    integer::k
    a=reshape([1,2,66,89,74,79],[2,3],order=[1,2])
    b=reshape([1,2,76,99,84,59],[2,3],order=[1,2])
    write (*,120) 'aid','a1','a2','bid','b1','b2'
    c=[((a(i,:),b(j,:),i=1,2),j=1,2)]
    d=reshape(c,[4,6],order=[2,1])
    write (*,110) c
    write  (*,*) "---------"
    write (*,110) d(2,:)
    write  (*,*) "---------"
    write (*,110) d(:,2)
    write  (*,*) "---------"
    write  (*,*) "b1>70 and a2<70"
    write (*,120) 'aid','a1','a2','bid','b1','b2'
    !b1>70 and a2<90 选择
    k=1
    do
       if (d(k,5)>70 .and. d(k,3)<79) write (*,110) d(k,:)
       k=k+1
       if (k>4) exit
    end do
    write (*,120) 'aid','a1','a2','bid','b1','b2'
    !b1>70 and a2<90 选择+投影
    write  (*,*) "b1>70 and a2<70 and aid=bid"
    write (*,120) 'aid','a1','a2','b1','b2'
    k=1
    do
       if (d(k,5)>70 .and. d(k,3)<79 .and. d(k,1)==d(k,4)) write (*,110) d(k,1:3),d(k,5:6)
       k=k+1
       if (k>4) exit
    end do
    110 format (6(I3))
    120 format (6(A3))
end program learn

aid a1 a2bid b1 b2
  1 66 74  1 76 84
  2 89 79  1 76 84
  1 66 74  2 99 59
  2 89 79  2 99 59
 ---------
  2 89 79  1 76 84
 ---------
 66 89 66 89
 ---------
 b1>70 and a2<70
aid a1 a2bid b1 b2
  1 66 74  1 76 84
  1 66 74  2 99 59
aid a1 a2bid b1 b2
 b1>70 and a2<70 and aid=bid
aid a1 a2 b1 b2
  1 66 74 76 84

Process returned 0 (0x0)   execution time : 0.141 s
Press any key to continue.

过程参数

program hello
    implicit none

    integer,dimension(10)::my_nums
    integer::result,avg,i
    my_nums=[(i,i=1,40,4 )]
    call sum_nums(my_nums,10,result,avg)
    write(*,*) result,avg

end program

subroutine sum_nums(nums,n,result,avg)
    integer,dimension(n),intent(in):: nums
    integer,intent(out)::result,avg
    result=0
    do i=1,n
        write (*,*) nums(i)
        result=result+nums(i)
    end do
    avg=result/n
end subroutine

select case 和 过程

 请输入计算的类型:
  1-->三角形           
  2-->平行四边形     
  3-->梯形              
  4-->圆形              
1
      请输入计算的参数1
9.22
      请输入计算的参数2
2.7
      请输入计算的参数3
-1
三角形            面积:     12.45

Process returned 0 (0x0)   execution time : 10.916 s
Press ENTER to continue.


program hello
    implicit none
    integer::g_type,i
    real::p
    real::area
    character(len=20),dimension(4)::g_str
    real,dimension(3)::param
    g_str=[character(len=20)::"三角形","平行四边形","梯形","圆形"]
    write (*,*) "请输入计算的类型:"
    write (*,110) (i,g_str(i),i=1,4)
    110 format (I3,'-->',A20)
    read (*,"(I3)") g_type
    if (g_type>4 .or. g_type<1) stop
    i=1
    do while (i<4)
        write (*,"(A30,I1)") "请输入计算的参数",i
        read (*,"(F6.2)") p
        if (p>=0.) then
           param(i)=p
        else
           exit
        end if
        i=i+1
    end do
    call get_area(param,area,g_type)
    write (*,"(A20,A10,F10.2)") g_str(g_type),"面积:",area
end program

subroutine get_area(param,area,g_type)
  integer,intent(in):: g_type
  real,dimension(3),intent(in)::param
  real,intent(out):: area

  select case (g_type)
        case(1)
            area=(1/2.)*param(1)*param(2)
        case(2)
            area=param(1)*param(2)
        case(3)
            area=(1/2.)*(param(1)+param(2))*param(3)
        case(4)
            area=2.*3.1415*param(1)
  end select
end subroutine get_area


module

 请输入计算的类型:
  1-->平均速度(x1,x2,t1,t2)
  2-->平均速率(s,t1,t2)
2
 请输入s,t1,t2:
38,11,121
  0.345454544

Process returned 0 (0x0)   execution time : 10.317 s
Press any key to continue.

program learn
    use v_compute
    implicit none
    real::t1,t2,x1,x2,s,result
    integer::cpt_type,i
    character(len=40),dimension(2)::cpt_str

    cpt_str=[character(len=40)::"平均速度(x1,x2,t1,t2)","平均速率(s,t1,t2)"]
    write (*,*) "请输入计算的类型:"
    write (*,110) (i,cpt_str(i),i=1,2)
    110 format (I3,'-->',A40)
    read (*,"(I3)") cpt_type
    if (cpt_type>2 .or. cpt_type<1) stop
    select case(cpt_type)
        case(1)
            write (*,*) "请输入x1,x2,t1,t2:"
            read (*,*) x1,x2,t1,t2
            call get_v(x1,x2,t1,t2,result)
        case(2)
            write (*,*) "请输入s,t1,t2:"
            read (*,*) s,t1,t2
            call get_v(s,t1,t2,result)
    end select
    write (*,*) result
end program learn







v_compute.f90

module v_compute
implicit none

    interface get_v
        module procedure get_v1
        module procedure get_v2
    end interface
contains
     subroutine get_v1(x1,x2,t1,t2,result)
        !平均速度
        real,intent(out)::result
        real,intent(out)::x1,x2,t1,t2
        result=(x2-x1)/(t2-t1)
    end subroutine
    subroutine get_v2(s,t1,t2,result)
        !平均速率
        real,intent(out)::result
        real,intent(out)::s,t1,t2
        result=s/(t2-t1)
    end subroutine

end module v_compute

快排

  4
  8
  9
 22
 33
 34
 56
 88
 91
212

Process returned 0 (0x0)   execution time : 0.139 s
Press any key to continue.






program hello
    implicit none

    integer,dimension(10)::my_nums
    integer::i
    my_nums=[9,34,212,91,88,33,8,22,4,56]
    call qsort_nums(my_nums,1,10)
    write(*,"(I3)") [(my_nums(i),i=1,10)]


end program

recursive subroutine qsort_nums(nums,first,last)
    integer,intent(in)::first,last
    integer,dimension(n),intent(inout):: nums
    integer::i,j,key,temp
    if (last<=first) then
        return
    end if
    i=first
    j=last
    key=nums(first)
    sort:do
            i_next:do
                if (nums(i)<=key .and. i<last) then
                    i=i+1
                else
                    exit
                endif
            end do i_next
            j_prev:do
                if (nums(j)>key .and. j>first) then
                    j=j-1
                else
                    exit
                endif
            end do j_prev
            if (i<j) then
                temp=nums(i)
                nums(i)=nums(j)
                nums(j)=temp
            else
                exit sort
            end if
    end do sort
    nums(first)= nums(j);
    nums(j) = key;
    call qsort_nums(nums,first,j-1)
    call qsort_nums(nums,j+1,last)
end subroutine

函数

  • 内部函数
program learn
    implicit none

    real::result,x
    x=11.33
    result=get_num(x)
    write (*,100) x,result
    100 format (2F10.2)
    contains
        real function get_num(x)
            real,intent(in)::x
            get_num=x*2
        end function get_num
end program learn

  • 外部函数
program learn
    implicit none
    real::get_num
    real::result,x
    x=11.33
    result=get_num(x)
    write (*,100) x,result
    100 format (2F10.2)
end program learn


real function get_num(x)
    real,intent(in)::x
    get_num=x*2
end function get_num

  • 二分法求解一元多次方程
    main.f90

program hello
    use bisect
    implicit none
    real::root
    integer::err_flag
    character(len=50)::err_msg
    real,external::fun_root
    call get_root(fun_root,-20.,20.,1.0E-7,root,err_flag,err_msg)
    if (err_flag>0) then
        write (*,*) "error:",err_msg
    else
        write (*,*) root
    end if


end program

real function fun_root(x)
     implicit none
     real,intent(in)::x
     fun_root=5*x**3-3*x**2+111*x+21
end function fun_root

bisect.f90

module bisect
implicit none

contains
     subroutine get_root(func,x_a,x_b,tolerance,root,err_flag,err_msg)
        integer,intent(out)::err_flag
        character(len=50),intent(out)::err_msg
        real,external::func
        real,intent(out)::root
        real,intent(in)::x_a,x_b,tolerance
        real::a
        real::b
        real::fun_a,fun_b,fun_x,x
        a=x_a
        b=x_b
        fun_a=func(a)
        fun_b=func(b)
        if (fun_a*fun_b>=0) then
            err_flag=1
            err_msg="f(a)f(b)>0"
            return
        end if
        write (*,"(A1)",advance='no') "|"
        do while ((b-a)/2>tolerance)
            x=(a+b)/2
            fun_x=func(x)
            if (fun_x==0.) then
                exit
            endif
            if (fun_x*fun_a<0.) then
                b=x
                fun_b=fun_x
            else
                a=x
                fun_a=fun_x
            end if
            write (*,"(A1)",advance='no') "="
        end do
        write (*,"(A1/)",advance='no') ">"
        root=(a+b)/2
        err_flag=0
        err_msg=""
    end subroutine

end module bisect

|============================>
 -0.187935606

Process returned 0 (0x0)   execution time : 0.033 s
Press any key to continue.

  • 正割法
|=============================>
  -1.31523108
.. 找到解
  -1.31523120

Process returned 0 (0x0)   execution time : 0.149 s
Press any key to continue.

program hello
    use equation_root
    implicit none
    real::root
    real::a,b
    integer::err_flag
    character(len=50)::err_msg
    real,external::fun_root
    a=-50.
    b=50.
    call get_root(2,fun_root,a,b,1.0E-7,1.0E-35,root,err_flag,err_msg,50)
    if (err_flag>0) then
        write (*,*) "error:",err_msg
    else
        write (*,*) root
    end if


end program

real function fun_root(x)
     implicit none
     real,intent(in)::x
     fun_root=x**3+9*cos(x)
end function fun_root


自定义类型

program learn
    implicit none
    type::student
        integer::student_id
        character(len=10)::student_name
        integer::age
        integer::class_id
    end type
    type::class
        integer::class_id
        character(len=50)::class_name
        integer::teacher_id!班主任ID
    end type
    type::student_score
        real::english_score
        real::math_score
        real::fortran_score
        real::cpp_score
        real::data_struct_score
        real::politics_score
        real::database_score
    end type
    type::teacher
        integer::teacher_id
        character(len=10)::teacher_name
        character(len=50)::degree!学位
    end type
    type(student) ::st1=student(1,"张三",25,1)
    write(*,*) st1%student_id,st1%student_name,st1%age,st1%class_id

end program learn


           1 张三                25           1

Process returned 0 (0x0)   execution time : 0.167 s
Press any key to continue.




网站公告

今日签到

点亮在社区的每一天
去签到