回 帖 发 新 帖 刷新版面

主题:有关数组的的输入和计算(急等)

菜鸟请教:
        给定了一个.TXT文本
内容如下:
         1          12
     0.000       2.536
    15.000       0.161
    30.000      -2.113
    45.000      -6.897
    60.000     -12.240
    90.000     -12.359
   105.000     -11.919
   120.000      -7.954
   135.000      -2.940
   150.000       2.696
   165.000       5.804
   179.739       6.429

         2          11
     0.000       4.542
    15.000       1.990
    30.000      -4.655
    45.000      -7.978
    60.000     -14.123
    75.000     -15.897
    90.000     -10.778
   105.000      -4.027
   120.000       2.171
   135.000       3.855
   145.566       3.971

         3          16
     0.000       6.800
    15.000       6.863
    30.000       4.164
    45.000      -0.099
    60.000      -7.203
    75.000     -14.995
    90.000     -16.900
   105.000     -15.575
   120.000     -14.690
   135.000      -7.389
   150.000      -1.993
   165.000       0.877
   180.000       2.068
   195.000       2.842
   210.000       3.111
   221.449       3.304
.........      ........


每一行第一个数是数组序列数,第二个数是该数组的行数,现在需要一个程序,读入这个.TXT文件,对每个数组按照第二列的数从小到大排列后得到新的数组,然后进行计算(计算部分程序自己努力解决)

回复列表 (共6个回复)

沙发


每一行第一个数是数组序列数,第二个数是该数组的行数?不明白

板凳


每一组的第一行的第一个数字,如“1”指代这是第一组数据,第一行第二个数如“12”表示该数组除了第一行后共有12行。
  例如
         1          12
     0.000       2.536
    15.000       0.161
    30.000      -2.113
    45.000      -6.897
    60.000     -12.240
    90.000     -12.359
   105.000     -11.919
   120.000      -7.954
   135.000      -2.940
   150.000       2.696
   165.000       5.804
   179.739       6.429
所以每组第一行只是起标号作用,关键数据时后面行的数据,排序时也只是针对后面的行

3 楼


program test_1
implicit none
integer::i,j,k,m
real::b(0:20,100,100),c(5),d(100,2)
integer::a(10,100)

open(10,file='data0.txt')
do i=1,42
 read(10,*) d(i,1), d(i,2)
enddo
close(10)

do i=2,13
 b(0,i-1,1)=d(i,1)
 b(0,i-1,2)=d(i,2)
enddo
k=0
do i=15,25
 k=k+1
 b(1,k,1)=d(i,1)
 b(1,k,2)=d(i,2)
enddo
k=0
do i=27,42
 k=k+1
 b(2,k,1)=d(i,1)
 b(2,k,2)=d(i,2)
enddo

do k=2,11 
 do i=12,k,-1
   if(b(0,i,2).lt.b(0,i-1,2))then
    do j=1,2
     c(j)=b(0,i-1,j)
     b(0,i-1,j)=b(0,i,j)
     b(0,i,j)=c(j)
    enddo
   endif
 enddo
enddo

do k=2,10 
 do i=11,k,-1
   if(b(1,i,2).lt.b(1,i-1,2))then
    do j=1,2
     c(j)=b(1,i-1,j)
     b(1,i-1,j)=b(1,i,j)
     b(1,i,j)=c(j)
     enddo
   endif
 enddo
enddo

do k=2,15 
 do i=16,k,-1
   if(b(2,i,2).lt.b(2,i-1,2))then
    do j=1,2
     c(j)=b(2,i-1,j)
     b(2,i-1,j)=b(2,i,j)
     b(2,i,j)=c(j)
     enddo
   endif
 enddo
enddo

open(20,file='data_new.txt')
do i=1,12
write(20,*) b(0,i,1:2)
enddo
write(20,*) '' 
do i=1,11
write(20,*) b(1,i,1:2)
enddo
write(20,*) '' 
do i=1,16
write(20,*) b(2,i,1:2)
enddo
close(20)

end program test_1

4 楼

result:
   90.00000      -12.35900    
   60.00000      -12.24000    
   105.0000      -11.91900    
   120.0000      -7.954000    
   45.00000      -6.897000    
   135.0000      -2.940000    
   30.00000      -2.113000    
   15.00000      0.1610000    
  0.0000000E+00   2.536000    
   150.0000       2.696000    
   165.0000       5.804000    
   179.7390       6.429000    
 
   75.00000      -15.89700    
   60.00000      -14.12300    
   90.00000      -10.77800    
   45.00000      -7.978000    
   30.00000      -4.655000    
   105.0000      -4.027000    
   15.00000       1.990000    
   120.0000       2.171000    
   135.0000       3.855000    
  0.0000000E+00   4.542000    
   145.5660       3.971000    
 
   90.00000      -16.90000    
   105.0000      -15.57500    
   75.00000      -14.99500    
   120.0000      -14.69000    
   135.0000      -7.389000    
   60.00000      -7.203000    
   150.0000      -1.993000    
   45.00000     -9.8999999E-02
   165.0000      0.8770000    
   180.0000       2.068000    
   195.0000       2.842000    
   210.0000       3.111000    
   221.4490       3.304000    
   30.00000       4.164000    
  0.0000000E+00   6.800000    
   15.00000       6.863000    

5 楼

Intel Fortran Compiler 有如下 子例程 可供阁下选择:

SORTQQ

Portability Subroutine: Sorts a one-dimensional array. The array elements cannot be derived types or record structures.

Module
USE IFPORT

Syntax
CALL SORTQQ (adrarray,count,size)

adrarray
 (Input) INTEGER(4) on IA-32 architecture; INTEGER(8) on Intel® 64 architecture and IA-64 architecture. Address of the array (returned by LOC).
 
count
 (Input; output) INTEGER(4) on IA-32 architecture; INTEGER(8) on Intel® 64 architecture and IA-64 architecture. On input, number of elements in the array to be sorted. On output, number of elements actually sorted.

To be certain that SORTQQ is successful, compare the value returned in count to the value you provided. If they are the same, then SORTQQ sorted the correct number of elements.
 
size
 (Input) INTEGER(4). Positive constant less than 32,767 that specifies the kind of array to be sorted. The following constants, defined in IFPORT.F90, specify type and kind for numeric arrays:

Constant
 Type of array
 
SRT$INTEGER1
 INTEGER(1)
 
SRT$INTEGER2
 INTEGER(2) or equivalent
 
SRT$INTEGER4
 INTEGER(4) or equivalent
 
SRT$INTEGER8
 INTEGER(8) or equivalent
 
SRT$REAL4
 REAL(4) or equivalent
 
SRT$REAL8
 REAL(8) or equivalent
 
SRT$REAL16
 REAL(16) or equivalent
 
 

If the value provided in size is not a symbolic constant and is less than 32,767, the array is assumed to be a character array with size characters per element.

Caution
The location of the array must be passed by address using the LOC function. This defeats Fortran type-checking, so you must make certain that the count and size arguments are correct.

If you pass invalid arguments, SORTQQ attempts to sort random parts of memory. If the memory it attempts to sort is allocated to the current process, that memory is sorted; otherwise, the operating system intervenes, the program is halted, and you get a General Protection Violation message.

Compatibility
CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB

Example
 !    Sort a 1-D array ! USE IFPORT INTEGER(2) array(10) INTEGER(2) i DATA ARRAY /143, 99, 612, 61, 712, 9112, 6, 555, 2223, 67/ !    Sort the array Call SORTQQ (LOC(array), 10, SRT$INTEGER2) !    Display the sorted array DO i = 1, 10   WRITE (*, 9000) i, array (i) 9000 FORMAT(1X, ' Array(',I2, '): ', I5) END DO END

6 楼


真的谢谢你的帮助了,不过你这个只是针对所给的3组数,实际上这组数挺多的,不能挨着分段,还是很感谢啊!!

我来回复

您尚未登录,请登录后再回复。点此登录或注册