变量格式 [英] Variable format

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

问题描述

我写了一个程序来计算一个平方有限差分矩阵,你可以在其中输入行数(等于列数) -> 这存储在变量矩阵中.该程序运行良好:

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

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

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

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

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)

如果我在 Linux 中使用 gfortran 编译,我总是在终端中收到以下错误:

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.

另外,永远忘记 FORMAT() 语句,它们已经过时了.

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

您还可以在每行循环中编写矩阵,然后可以在 Fortran 2008 中使用任意大的计数或 *.

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天全站免登陆