可变格式 [英] Variable format

查看:220
本文介绍了可变格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个程序来计算一个平方有限差分矩阵,在这里你可以输入行数(等于列数) - >这个存储在变量矩阵中。该程序工作正常:

pre $ $









$ ,dimension(:, :),allocatable :: A
integer :: matrix,i,j

print *,'Enter elements:'
read *,matrix

分配(A(矩阵,矩阵))

A = 0
A(1,1)= 2
A(1,2)= - 1
A(矩阵,矩阵)= 2
A(矩阵,矩阵-1)= -1

do j = 2,矩阵-1
A (j,j + 1)= -1
结束

A(j,j)= 2
(*,1)A

1格式(6i10)

结束程序fin_diff_matrix

对于输出,我希望矩阵被格式化为输出,例如如果用户输入6行,输出也应该如下所示:

  2 -1 0 0 0 0 
-1 2 -1 0 0 0
0 -1 2 -1 0 0
0 0 -1 2 -1 0
0 0 0 -1 -1 -1 b $ b 0 0 0 0 -1 2

格式的输出也应该是可变的,例如如果用户输入10,则输出也应格式化为10列。对互联网的研究给出了带尖括号的格式语句的以下解决方案:

  1格式(< matrix> i< 10 )

如果我在Linux中使用gfortran进行编译,我总是会在终端出现以下错误:

  fin_diff_matrix.f95:37.12:

1格式(< matrix> i10)
1
错误:(1)格式字符串中的意外元素'<'
fin_diff_matrix.f95:35.11:

写(*,1)A
1
错误:FORMAT标签1在(1)未定义

什么不工作和什么是我的错误?

解决方案

你试图使用的语法是非标准的,它只适用于一些编译器,我劝阻使用它。



另外,忘记 FORMAT()语句是好的,它们已经过时了。



你可以在里面找到你自己的号码e格式的字符串,当你自己从几个部分构造

  character(80):: form 
form ='( (i10,1x))'
write(form(2:11),'(i10)')matrix

write(*,form)A

你也可以在每一行循环中写你的矩阵,然后你可以使用一个任意大的数字或一个在Fortran 2008中*


$ b $ $ $ $ $ $ $ $ $ $ $ $ '(999(i10,1x))')A(:,i)
end do

do i = 1,matrix
write(*,'(*(i10 ,1x))')A
end do

只要检查我是否没有转置矩阵无意中。


I wrote a program to calculate a square finite difference matrix, where you can enter the number of rows (equals the number of columns) -> this is stored in the variable matrix. The program works fine:

program fin_diff_matrix

  implicit none

  integer, dimension(:,:), allocatable :: A     
  integer :: matrix,i,j

  print *,'Enter elements:'
  read *, matrix

  allocate(A(matrix,matrix))

  A = 0
  A(1,1) = 2
  A(1,2) = -1
  A(matrix,matrix) = 2
  A(matrix,matrix-1) = -1

  do j=2,matrix-1
    A(j,j-1) = -1
    A(j,j) = 2
    A(j,j+1) = -1
  end do

  print *, 'Matrix A: '
  write(*,1) A

  1 format(6i10)

end program fin_diff_matrix

For the output I want that matrix is formatted for the output, e.g. if the user enters 6 rows the output should also look like:

         2        -1         0         0         0         0
        -1         2        -1         0         0         0
         0        -1         2        -1         0         0
         0         0        -1         2        -1         0
         0         0         0        -1         2        -1
         0         0         0         0        -1         2

The output of the format should also be variable, e.g. if the user enters 10, the output should also be formatted in 10 columns. Research on the Internet gave the following solution for the format statement with angle brackets:

  1 format(<matrix>i<10)

If I compile with gfortran in Linux I always get the following error in the terminal:

       fin_diff_matrix.f95:37.12:

     1 format(<matrix>i10)
            1
   Error: Unexpected element '<' in format string at (1)
   fin_diff_matrix.f95:35.11:

     write(*,1) A
           1
   Error: FORMAT label 1 at (1) not defined

What doesn't that work and what is my mistake?

解决方案

The syntax you are trying to use is non-standard, it works only in some compilers and I discourage using it.

Also, forget the FORMAT() statements for good, they are obsolete.

You can get your own number inside the format string when you construct it yourself from several parts

character(80) :: form
form = '(          (i10,1x))'
write(form(2:11),'(i10)') matrix

write(*,form) A

You can also write your matrix in a loop per row and then you can use an arbitrarily large count number or a * in Fortran 2008.

do i = 1, matrix
  write(*,'(999(i10,1x))') A(:,i)
end do

do i = 1, matrix
  write(*,'(*(i10,1x))') A
end do

Just check if I did not transpose the matrix inadvertently.

这篇关于可变格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆