首页 > 代码 > 常规代码 > 正文

代码

阅读排行

为文本数据增加文本表格框
2014-01-25 11:03:04   来源:pfan.cn   评论:0 点击:

本代码将以行,列排列的文本数据放入文本的表格框内,自动适配列宽,以宋体查看时效果极佳。

输入文件:原始数据.txt

输出文件:vqimwr@163.com.txt 感谢原作者vqimwr
\

源代码如下:
program www_fcode_cn
  implicit none
  integer:: i,j,k    
  integer:: i_ReadingState
  integer:: i_DataRows,i_DataCols
  integer:: i_TableWidth
  integer:: i_Start,i_End    
  logical:: l_exist
  character(len=120):: s_CalvariaLine,s_NeckLine,s_BodyLine,s_SoleLine
  character(len=120):: s_TemplateHeadLine,s_TemplateDataLine
  character(len=120):: s_Temp 
  character(len=80)::s_format    
  character(len=120),allocatable:: s_Data(:,:)
  character,allocatable:: s_TableHead(:)
  integer,allocatable:: i_DataWidth(:,:),i_DataHigh(:,:)    
  integer,allocatable:: i_ColumnWidth(:) 
  integer,allocatable:: i_StartPos(:)    
  integer,allocatable:: i_LeftPos(:),i_RightPos(:)

  inquire(file="原始数据.txt",exist=l_exist)
  if(.not.l_exist) then
      write(*,*) "找不到输入数据的文件——“原始数据.txt”,程序已终止,回车退出DOS界面! "
      read(*,*)
      stop
  end if

  open(1,file="原始数据.txt")    
  open(2,file="vqimwr@163.com.txt")    
  read(1,"(A120)",iostat=i_ReadingState) s_Temp
  i_DataCols=0
  if((s_Temp(1:1)/=" ".and.s_Temp(1:1)/="     ").and.&
      &(s_Temp(2:2)==" ".or.s_Temp(2:2)=="     ".or.s_Temp(2:3)==" ")) then
      i_DataCols=i_DataCols+1
  end if
  do k=2,len(trim(s_Temp))-2
      if((s_Temp(k:k)/=" ".and.s_Temp(k:k)/="     ".and.s_Temp(k-1:k)/=" ").and.&
          &(s_Temp(k+1:k+1)==" ".or.s_Temp(k+1:k+1)=="     ".or.s_Temp(k+1:k+2)==" ")) then
          i_DataCols=i_DataCols+1
      end if
  end do
  i_DataCols=i_DataCols+1
  i_DataRows=1
  do while(.true.)    
      read(1,"(A120)",iostat=i_ReadingState) s_Temp
      if(i_ReadingState/=0) exit
      i_DataRows=i_DataRows+1
  end do        
  allocate(s_data(i_DataRows,i_DataCols),i_DataWidth(i_DataRows,i_DataCols),i_DataHigh(i_DataRows,i_DataCols))
  allocate(i_LeftPos(i_DataCols),i_RightPos(i_DataCols),i_ColumnWidth(i_DataCols),i_StartPos(i_DataCols))    

  rewind(1) 
  do i=1,i_DataRows
      read(1,"(A120)") s_Temp
      if(s_temp(1:1)=="     ")      s_temp(1:1)=" "
      do k=2,len_trim(s_Temp)
          if(s_temp(k:k)=="     ") then
              s_temp(k:k)=" "
          elseif(s_temp(k-1:k)==" ") then
              s_temp(k-1:k)=" "
          end if
      end do
      j=1
      if(s_temp(1:1)/=" ") i_LeftPos(j)=1
      do k=2,Len_trim(s_Temp)
          if(s_temp(k-1:k-1)/=" ".and.s_temp(k:k)==" ") then
              i_RightPos(j)=k-1
              i_DataWidth(i,j)=i_RightPos(j)-i_LeftPos(j)+1    
              s_Data(i,j)=s_Temp(i_LeftPos(j):i_RightPos(j))    
              j=j+1
          elseif(s_temp(k-1:k-1)==" ".and.s_temp(k:k)/=" ") then
              i_LeftPos(j)=k
          end if
      end do
      i_RightPos(j)=Len_trim(s_Temp)
      i_DataWidth(i,j)=i_RightPos(j)-i_LeftPos(j)+1        
      s_Data(i,j)=s_Temp(i_LeftPos(j):i_RightPos(j))
  end do
  s_CalvariaLine(1:2)="┏"
  s_NeckLine(1:2)="┣"
  s_BodyLine(1:2)="┠"
  s_SoleLine(1:2)="┗"
  s_TemplateDataLine(1:2)="┃"
  i_TableWidth=2
  do j=1,i_DataCols 
      i_ColumnWidth(j)=i_DataWidth(1,j)
      do i=2,i_DataRows
          if(i_DataWidth(i,j)>i_ColumnWidth(j)) i_ColumnWidth(j)=i_DataWidth(i,j)
      end do
      if(mod(i_ColumnWidth(j),2)/=0) then
          i_ColumnWidth(j)=i_ColumnWidth(j)+1
      end if
      i_TableWidth=i_TableWidth+i_ColumnWidth(j)+2 
      do k=i_TableWidth-i_ColumnWidth(j)-1,i_TableWidth-2,2
          s_CalvariaLine(k:k+1)="━"
          s_NeckLine(k:k+1)="━"
          s_BodyLine(k:k+1)="─"
          s_SoleLine(k:k+1)="━"
          s_TemplateDataLine(k:k+1)=" "
      end do
      s_CalvariaLine(i_TableWidth-1:i_TableWidth)="┯"
      s_NeckLine(i_TableWidth-1:i_TableWidth)="╇"
      s_BodyLine(i_TableWidth-1:i_TableWidth)="┼"
      s_SoleLine(i_TableWidth-1:i_TableWidth)="┷"
      s_TemplateDataLine(i_TableWidth-1:i_TableWidth)="│"
  end do
  s_CalvariaLine(i_TableWidth-1:i_TableWidth)="┓"
  s_NeckLine(i_TableWidth-1:i_TableWidth)="┫"
  s_BodyLine(i_TableWidth-1:i_TableWidth)="┨"
  s_SoleLine(i_TableWidth-1:i_TableWidth)="┛"
  s_TemplateDataLine(i_TableWidth-1:i_TableWidth)="┃"
  write(s_format,"(A2,A1,i2,A2)") "(","A",i_TableWidth,")"
  i_StartPos(1)=3
  do j=2,i_DataCols    
      i_StartPos(j)=i_StartPos(j-1)+i_ColumnWidth(j-1)+2
  end do
   
  write(2,s_format) Trim(s_CalvariaLine)
  do i=1,i_DataRows
  s_Temp=s_TemplateDataLine
      do j=1,i_DataCols
          i_Start=i_StartPos(j) +(i_ColumnWidth(j)-i_DataWidth(i,j))/2
          i_End=i_Start+i_DataWidth(i,j)-1
          s_Temp(i_Start:i_End)=trim(s_Data(i,j))
              end do
  write(2,s_format) s_Temp
  if( i < i_DataRows ) then
    write( 2 , s_format ) Trim(s_BodyLine)
  end if
  end do
  write(2,s_format) Trim(s_SoleLine)
  close(1)
  close(2)
end program www_fcode_cn

相关热词搜索:文本数据 表格

上一篇:任意表达式求值模块
下一篇:获得文件行数和列数

分享到: 收藏