如何使用OO Fortran从五个图形中获得最小的图形
我怎样才能得到给定五位数中的最小数字。例如如何使用OO Fortran从五个图形中获得最小的图形,fortran,fortran95,Fortran,Fortran95,我怎样才能得到给定五位数中的最小数字。例如23764。如何使2最小 将数字作为一个数字,例如456879,为了从数字4中获得最小值,我实现了以下步骤 program findsmallestFigure implicit none integer:: figure integer:: c,smallest,largest smallest = 9 largest = 0 figure = 23456 do while(figure .GT. 0 ) c = MO
23764
。如何使2
最小
将数字作为一个数字,例如456879
,为了从数字4
中获得最小值,我实现了以下步骤
program findsmallestFigure
implicit none
integer:: figure
integer:: c,smallest,largest
smallest = 9
largest = 0
figure = 23456
do while(figure .GT. 0 )
c = MOD(figure,10)
largest = max(c,largest)
smallest = min(c,smallest)
figure = figure/10
end do
print *,'The smallest digit is',smallest
end
如何在Fortran中使用面向对象的方法实现相同的结果?创建一个具有用户定义类型的模块,该模块包含所有结果,以及用于填充值的子例程
module numstat
! Holds the statistics of a figure
type stat
integer :: smallest, largest, count
end type
! Constructor from a figure. Invoke by 'stat(1234)`
interface stat
module procedure :: calc_stat
end interface
contains
! Fill statistics type from a figure
function calc_stat(fig) result(s)
integer, intent(in) :: fig
type(stat) :: s
integer :: digit, f
! make a copy of the figure because intent(in) arguments
! are immutable (cannot change).
f = fig
s%smallest = 9
s%largest = 0
s%count = 0
do while(f > 0 )
s%count = s%count + 1
digit = mod(f, 10)
s%largest = max(s%largest, digit)
s%smallest = min(s%smallest, digit)
f = f/10
end do
end function
end module
然后使用主程序中的模块
program SONumstat
use numstat
implicit none
type(stat) :: s
integer :: figure
figure = 23456
s = stat(figure)
print *,'The number of digits is ', s%count
print *,'The smallest digit is ',s%smallest
print *,'The largest digit is ',s%largest
end program SONumstat
什么是数字?如何在代码中定义它?是整数吗?这是一个角色。显示您的代码,ir是完全必要的。请务必阅读和阅读。有几种方法可以做到这一点,所以请解释你想如何做到这一点,以及你在哪里挣扎。一种特殊的方法是使用创建一个数组,然后找到最小值。与前面的注释一样,应该显示您的尝试,这样我们就不必编写整个程序。我已经有了答案,希望我可以添加答案。您能解释一下为什么以及如何将面向对象编程模型应用于此问题吗?