Fortran Coder

查看: 10782|回复: 2
打印 上一主题 下一主题

[输入输出] 关于读取字符串(可能动态)和排序

[复制链接]

3

帖子

2

主题

0

精华

新人

F 币
18 元
贡献
10 点
跳转到指定楼层
楼主
发表于 2014-3-6 11:40:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 abandon 于 2014-3-6 11:48 编辑

Using names.txt (放在附件), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53             = 49714.
What is the total of all the name scores in the file?
就是所有名字按 ABCD 排序,然后得到他在ABCD排序中的位置,然后A是一分,B是两分这样加,然后乘以位置数,求和。
因为不知道里面具体有多少个名字,所以可能用到动态读取。自己是编程 菜鸟,fortran刚开始,希望那位好心的大神可以帮忙读取这个文件,最好是一个字符串数组,每个元素存一个名字。排序的话,要是有大神愿意提供代码更是感激不尽!!

names.txt

45.36 KB, 下载次数: 2

分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

740

帖子

4

主题

0

精华

大师

农村外出务工人员

F 币
718 元
贡献
367 点

新人勋章爱心勋章水王勋章元老勋章热心勋章

沙发
发表于 2014-3-6 12:13:28 | 只看该作者
答案是
共5163个单词
总分   871198282

以下代码,分别使用了网站的两个函数:
GetDataN http://www.fcode.cn/code_gen-34-1.html
HeapSort http://www.fcode.cn/code_prof-2-1.html

[Fortran] 纯文本查看 复制代码
001program www_fcode_cn
002  implicit none
003  Integer , parameter :: LEN_ALL = 50000 !// 总长度
004  Integer , parameter :: LEN_ONE = 16 !// 单个长度
005  Character( Len = LEN_ALL ) :: cStrAll
006  Character( Len = LEN_ONE ) , allocatable :: cStrSplit( : )
007  integer :: n , sum , score !// 单词个数,总分,单个分
008  integer :: i
009  Open( 12 , File = "names.txt" )
010  Read( 12 , '(a50000)' ) cStrAll
011  Close( 12 )
012  n = GetDataN( cStrAll ) !// 获得单词数
013  write(*,'(a,i0,a)') '共',n,'个单词'
014  Allocate( cStrSplit(n) )
015  read( cStrAll , * ) cStrSplit
016  call HeapSort( cStrSplit , comp_f ) !// 排序
017  sum = 0
018  Do i = 1 , n
019    sum = sum + i * GetStringScore( Trim(cStrSplit(i)) )
020  End Do
021  write( * , * ) '总分' , sum
022  Deallocate( cStrSplit )
023   
024contains
025 
026  Integer Function GetStringScore( c )
027    Character( Len = * ) :: c
028    integer :: i
029    GetStringScore = 0
030    Do i = 1 , Len_Trim( c )
031      GetStringScore = GetStringScore + ( ichar(c(i:i)) -  ichar('A') + 1 )
032    End Do
033  End Function GetStringScore
034 
035  Integer Function GetDataN( cStr )
036    Character( Len = * ) , Intent( IN ) :: cStr
037    Integer :: i
038    Logical :: bIsSeparator , bIsQuote
039    GetDataN = 0
040    bIsSeparator = .TRUE.
041    bIsQuote = .FALSE.
042    Do i = 1 , Len_Trim( cStr )
043      Select Case( cStr(i:i) )
044      Case( '"' , "'" ) !// 如果遇到引号
045        If ( .Not.bIsQuote ) GetDataN = GetDataN + 1  !//如果不在引号中,则增加一个数据
046        bIsQuote = .Not.bIsQuote !// 引号结束或开始
047        bIsSeparator = .FALSE.
048      Case( " " , "," , char(9) ) !// 如果遇到分隔符
049        If ( .Not.bIsQuote ) then  !// 分隔符如果不在引号中
050          bIsSeparator = .TRUE.
051        End If
052      Case Default     
053        If ( bIsSeparator ) then
054          GetDataN = GetDataN + 1
055        End If
056        bIsSeparator = .FALSE.
057      End Select
058    End Do
059  End Function GetDataN
060   
061  Subroutine HeapSort( stD , comp_f )
062    Character( Len = * ) , Intent( INOUT ) :: stD( : )
063    Real , External :: comp_f
064    Integer i,ir,j,l,n
065    Character( Len = LEN_ONE ) :: stTemp
066    n = size( stD )
067    If ( n < 2 ) Return
068    l = n / 2 + 1
069    ir = n
070    Do while( .TRUE. )
071      If( l > 1 ) then
072        l = l - 1
073        stTemp = stD( l )
074      Else
075        stTemp = stD( ir )
076        stD( ir ) = stD( 1 )
077        ir = ir - 1
078        If( ir == 1 ) then
079          stD( 1 ) = stTemp
080          return
081        End If
082      End If
083      i = l
084      j = l + l
085      Do while( j<=ir )
086        If( ( j < ir ) ) then
087          If ( comp_f( stD(j) , std(j+1) ) > 0.0 ) then
088            j = j+1
089          End If
090        EndIf
091        If( comp_f( stTemp , stD(j) ) > 0.0 )then
092          stD(i) = stD( j )
093          i = j
094          j = j + j
095        Else
096          j = ir + 1
097        End If
098      EndDo
099      stD( i ) = stTemp
100    End Do
101  End Subroutine HeapSort
102   
103  Real Function comp_f( st1 , st2 )
104    Character( Len = * ) , Intent( IN ) :: st1 , st2
105    if ( Trim(st1) > Trim(st2) ) then
106      comp_f = -1.0
107    else
108      comp_f = 1.0
109    end if     
110  End Function comp_f
111   
112end program www_fcode_cn

13

帖子

6

主题

0

精华

熟手

F 币
114 元
贡献
81 点
板凳
发表于 2014-3-6 12:32:06 来自移动端 | 只看该作者
这个读写文件及文件处理我需要继续努力好好学习
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2025-4-8 00:04

Powered by Discuz! X3.4

© 2013-2025 Comsenz Inc.

快速回复 返回顶部 返回列表